分析
$$
\begin{align}
& \sum_{i = 1}^n {n \choose i} i^k \\
= & \sum_{i = 1}^n {n \choose i} \sum_{j = 0}^i {i \choose j} \begin{Bmatrix}k \\ j\end{Bmatrix} j! \\
= & \sum_{j=0}^{\min\{n,k\}} \begin{Bmatrix}k \\ j\end{Bmatrix} j! \sum_{i = j}^n {n \choose i} {i \choose j} \\
= & \sum_{j=0}^{\min\{n,k\}} n ^ {\underline{j}}\begin{Bmatrix}k \\ j\end{Bmatrix} \sum_{i = j}^n {n-j \choose i-j} \\
= & \sum_{j=0}^{\min\{n,k\}} n ^ {\underline{j}}\begin{Bmatrix}k \\ j\end{Bmatrix} 2 ^ {n-j} \\
\end{align}
$$
$\mathcal{O}(k ^ 2)$ 预处理第二类斯特林数即可。
代码
// ====================================
// author: M_sea
// website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
int read() {
int X = 0, w = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
return X * w;
}
const int N = 5000 + 10;
const int mod = 1e9 + 7, inv2 = 500000004;
int qpow(int a, int b) { int c = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) c = 1ll * c * a % mod;
return c;
}
int n, k, S[N][N];
int main() {
n = read(), k = read();
for (int i = S[0][0] = 1; i <= k; ++i)
for (int j = 1; j <= i; ++j)
S[i][j] = (S[i - 1][j - 1] + 1ll * j * S[i - 1][j]) % mod;
int ans = 0, a = 1, b = qpow(2, n);
for (int i = 0; i <= min(n, k); ++i) {
ans = (ans + 1ll * S[k][i] * a % mod * b) % mod;
a = 1ll * a * (n - i) % mod, b = 1ll * b * inv2 % mod;
}
printf("%d\n", ans);
return 0;
}