#include <bits/stdc++.h>
const int mod = 1e9 + 7;
int y[1000005], pri[1000005], cnt, n, k;
int fac[1000005], inv[1000005], pre[1000005], suf[1000005];
bool f[1000005];
inline int qpow(int x, int y, int ret = 1) {
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1) ret = 1ll * ret * x % mod;
  return ret;
}
inline void reduce(int& x) { x -= mod, x += x >> 31 & mod; }
inline int lagrange(int n, int now) {
  y[1] = 1;
  for (int i = 2, iend = n; i <= iend; ++i) {
    if (!f[i]) pri[++cnt] = i, y[i] = qpow(i, k);
    for (int j = 1; j <= cnt && i * pri[j] <= n; ++j) {
      f[i * pri[j]] = 1, y[i * pri[j]] = 1ll * y[i] * y[pri[j]] % mod;
      if (i % pri[j] == 0) break;
    }
  }
  for (int i = 1, iend = n; i <= iend; ++i) reduce(y[i] += y[i - 1]);
  fac[0] = 1;
  for (int i = 1, iend = n; i <= iend; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = qpow(fac[n], mod - 2);
  for (int i = n - 1, iend = 0; i >= iend; --i)
    inv[i] = inv[i + 1] * (i + 1ll) % mod;
  pre[0] = suf[n + 1] = 1;
  for (int i = 1, iend = n; i <= iend; ++i)
    pre[i] = 1ll * pre[i - 1] * (now - i) % mod;
  for (int i = n, iend = 1; i >= iend; --i)
    suf[i] = 1ll * suf[i + 1] * (now - i) % mod;
  long long ans = 0;
  for (int i = 1, iend = n; i <= iend; ++i) {
    int tmp = 1ll * pre[i - 1] * suf[i + 1] % mod * inv[i - 1] % mod *
              inv[n - i] % mod;
    if (n - i & 1) reduce(tmp = mod - tmp);
    (ans += 1ll * y[i] * tmp) %= mod;
  }
  return ans;
}
int main() {
  std::cin >> n >> k;
  std::cout << lagrange(k + 2, n) << '\n';
  return 0;
}
