#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 5;
const long long MOD = 1e9 + 7;
long long quick_pow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    b /= 2;
    a = (a * a) % MOD;
  }
  return res;
}
long long fact[maxn], p[maxn];
int main() {
  int i, j, n, k;
  fact[0] = 1;
  p[0] = 0;
  for (i = 1; i <= 1000004; i++) fact[i] = (fact[i - 1] * i) % MOD;
  while (scanf("%d%d", &n, &k) != EOF) {
    for (i = 1; i <= k + 2; i++) {
      p[i] = (p[i - 1] + quick_pow(i, k)) % MOD;
    }
    if (n <= k + 2) {
      printf("%I64d\n", p[n]);
      continue;
    }
    long long res = 0, now = 1;
    for (i = 1; i <= k + 2; i++) now = (now * (n - i)) % MOD;
    for (i = 1; i <= k + 2; i++) {
      long long inv = quick_pow(n - i, MOD - 2);
      long long temp = now * inv % MOD;
      long long t = fact[i - 1] * fact[k + 2 - i] % MOD;
      long long sign = (k + 2 - i) % 2 == 1 ? -1 : 1;
      inv = quick_pow(t, MOD - 2);
      res += temp * inv * sign % MOD * p[i];
      res %= MOD;
    }
    if (res < 0) res += MOD;
    printf("%I64d\n", res);
  }
  return 0;
}
