#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 10;
int n, k, fac[N];
int fpw(int x, int k, int p) {
  int ret = 1;
  while (k) {
    if (k & 1) ret = 1ll * ret * x % p;
    x = 1ll * x * x % p;
    k >>= 1;
  }
  return ret;
}
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
int main() {
  scanf("%d %d", &n, &k);
  if (n <= k + 2) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      ans = add(ans, fpw(i, k, mod));
    }
    printf("%d\n", ans);
    return 0;
  }
  fac[0] = 1;
  int x = 1;
  for (int i = 1; i <= k + 2; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod, x = 1ll * (n - i) * x % mod;
  x = (x + mod) % mod;
  int ans = 0, p = 0;
  for (int i = 1; i <= k + 2; i++) {
    p = add(p, fpw(i, k, mod));
    int t = 1ll * x * fpw((n - i), mod - 2, mod) % mod;
    t = 1ll * t *
        fpw(1ll * fac[i - 1] % mod * fac[k + 2 - i] % mod, mod - 2, mod) % mod;
    if ((k + 2 - i) & 1) t = mod - t;
    ans = add(ans, 1ll * p * t % mod);
  }
  printf("%d\n", ans);
  return 0;
}
