#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
const long long mod = 1e9 + 7;
int n, k;
long long a[N], b[N], c[N], S[N], pre[N], bck[N];
long long jc[N], ans;
long long fpow(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y & 1) ans = 1LL * ans * x % mod;
    x = 1LL * x * x % mod;
    y >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &k);
  k += 2;
  for (int i = 1; i <= k; i++) S[i] = (S[i - 1] + fpow(i, k - 2)) % mod;
  if (n <= k) {
    printf("%lld", S[n]);
    return 0;
  }
  jc[0] = 1;
  for (int i = 1; i <= k; i++) jc[i] = jc[i - 1] * i % mod;
  pre[0] = 1;
  bck[k + 1] = 1;
  for (int i = 1; i <= k; i++) pre[i] = ((n - i) * pre[i - 1]) % mod;
  for (int i = k; i >= 1; i--) bck[i] = ((n - i) * bck[i + 1]) % mod;
  for (int i = 1; i <= k; i++) a[i] = pre[i - 1] * bck[i + 1] % mod;
  for (int i = 1; i <= k; i++) b[i] = (((jc[i - 1] % mod) * jc[k - i]) % mod);
  for (int i = 1; i <= k; i++)
    ans = (ans +
           fpow(-1, k - i) * ((S[i] * a[i] % mod) * fpow(b[i], mod - 2) % mod) +
           mod) %
          mod;
  printf("%lld", ans);
  return 0;
}
