#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;
int n, k, y[maxn], prod[maxn], f[maxn], g[maxn], h[maxn];
int pow(int x, int p) {
  if (!p) return 1;
  int t = x;
  p--;
  while (p) {
    if (p & 1) t = ((long long)t * x) % mod;
    x = ((long long)x * x) % mod;
    p >>= 1;
  }
  return t;
}
void solve() {
  scanf("%d%d", &n, &k);
  f[0] = g[0] = 1;
  int ret = 1, ans = 0;
  for (int i = 1; i <= k + 2; ++i) y[i] = (y[i - 1] + pow(i, k)) % mod;
  if (n <= k + 2) {
    printf("%d\n", y[n]);
    return;
  }
  for (int i = 1; i <= k + 2; ++i) f[i] = (long long)f[i - 1] * (mod - i) % mod;
  for (int i = 1; i <= k + 2; ++i) g[i] = (long long)g[i - 1] * i % mod;
  for (int i = 1; i <= k + 2; ++i)
    h[i] = (long long)f[k + 2 - i] * g[i - 1] % mod;
  for (int i = 1; i <= k + 2; ++i) ret = ((long long)ret * (n - i)) % mod;
  for (int i = 1; i <= k + 2; ++i)
    prod[i] =
        (long long)ret * pow(n - i, mod - 2) % mod * pow(h[i], mod - 2) % mod;
  for (int i = 1; i <= k + 2; ++i)
    ans = (ans + (long long)prod[i] * y[i]) % mod;
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}
