#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5, mod = 1e9 + 7;
long long n, k, pre[N], suf[N], fac[N], qx[N], qy[N];
long long mul(long long x, long long n, long long mod) {
  long long ans = mod != 1;
  for (x %= mod; n; n >>= 1, x = x * x % mod)
    if (n & 1) ans = ans * x % mod;
  return ans;
}
long long calc(long long n, long long k, long long qx[N], long long qy[N]) {
  fac[0] = pre[0] = suf[n + 2] = 1;
  for (long long i = 1; i <= n + 1; i++)
    pre[i] = pre[i - 1] * (k - qx[i] + mod) % mod,
    fac[i] = fac[i - 1] * i % mod;
  for (long long i = n + 1; i >= 1; i--)
    suf[i] = suf[i + 1] * (k - qx[i] + mod) % mod;
  long long ans = 0;
  for (long long i = 1; i <= n + 1; i++) {
    long long s = fac[i - 1] * ((n + 1 - i) & 1 ? mod - 1 : 1) % mod *
                  fac[n + 1 - i] % mod;
    ans = (ans + qy[i] * (pre[i - 1] * suf[i + 1] % mod) % mod *
                     mul(s, mod - 2, mod) % mod) %
          mod;
  }
  return ans;
}
signed main() {
  scanf("%lld%lld", &n, &k);
  for (long long i = 1; i <= k + 2; i++)
    qx[i] = i, qy[i] = (qy[i - 1] + mul(i, k, mod)) % mod;
  printf("%lld\n", calc(k + 1, n, qx, qy));
  return 0;
}
