#include <bits/stdc++.h>
const long long K = 1e6 + 100;
const long long MOD = 1e9 + 7;
long long n, k;
long long pre[K];
long long suf[K];
long long fac[K];
long long inv[K];
long long pow(long long x, long long k) {
  if (x == 0) return 0;
  long long res = 1;
  while (k) {
    if (k & 1) res *= x, res %= MOD;
    x *= x, x %= MOD;
    k >>= 1;
  }
  return res;
}
signed main() {
  scanf("%lld%lld", &n, &k);
  pre[0] = n - k - 1, suf[k + 1] = n, inv[0] = inv[1] = 1, fac[0] = 1;
  for (long long i = 1; i <= k + 1; ++i)
    pre[i] = pre[i - 1] * (n - k - 1 + i), pre[i] %= MOD;
  for (long long i = k; i >= 0; --i)
    suf[i] = suf[i + 1] * (n - k - 1 + i), suf[i] %= MOD;
  for (long long i = 2; i <= k + 1; ++i)
    inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  for (long long i = 1; i <= k + 1; ++i) fac[i] = inv[i] * fac[i - 1] % MOD;
  long long f = 0, ans = 0;
  for (long long i = 0; i <= k + 1; ++i) {
    f += pow(i, k), f %= MOD;
    long long add;
    if (i == 0)
      add = f * pre[k - 1] % MOD;
    else if (i == k + 1)
      add = f * suf[1] % MOD;
    else
      add = f * suf[k + 1 - i + 1] % MOD * pre[k + 1 - i - 1] % MOD;
    ans +=
        add * ((k + 1 - i) & 1 ? -1 : 1) * fac[i] % MOD * fac[k + 1 - i] % MOD,
        ans %= MOD;
  }
  printf("%lld", (ans % MOD + MOD) % MOD);
}
