#include <bits/stdc++.h>
using namespace std;
const int K = 1000000 + 7;
const long long MOD = 1000000007;
long long n, k;
long long y[K];
long long pre[K], suf[K];
long long ifac[K];
long long f;
long long pow(long long a, long long k);
int main() {
  scanf("%lld%lld", &n, &k);
  pre[0] = ifac[0] = 1;
  for (long long i = 1; i <= k + 2; ++i) {
    y[i] = (y[i - 1] + pow(i, k)) % MOD;
    pre[i] = pre[i - 1] * (n - i + MOD) % MOD;
    ifac[i] = ifac[i - 1] * pow(i, MOD - 2) % MOD;
  }
  suf[k + 3] = 1;
  for (long long i = k + 2; i; --i) suf[i] = suf[i + 1] * (n - i + MOD) % MOD;
  for (long long i = 1, t; i <= k + 2; ++i) {
    t = y[i];
    if ((k + 2 - i) & 1) t = MOD - t;
    (t *= pre[i - 1] * suf[i + 1] % MOD) %= MOD;
    (t *= ifac[i - 1] * ifac[k + 2 - i] % MOD) %= MOD;
    (f += t) %= MOD;
  }
  printf("%lld", f);
  return 0;
}
long long pow(long long a, long long k) {
  long long t = 1;
  for (; k; a = a * a % MOD, k >>= 1)
    if (k & 1) t = t * a % MOD;
  return t;
}
