#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
const int mod = 1e9 + 7;
int n, k, a[maxn], f[maxn], g[maxn];
int fast_pow(int a, int b) {
  int ret = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) ret = 1ll * ret * a % mod;
  return ret;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= min(k + 2, n); i++)
    a[i] = (a[i - 1] + fast_pow(i, k)) % mod;
  if (n <= k + 2) {
    printf("%d\n", a[n]);
    return 0;
  }
  int now, ans = 0;
  now = f[0] = g[0] = 1;
  for (int i = 1; i <= k + 2; i++) {
    now = 1ll * now * (n - i) % mod;
    f[i] = 1ll * f[i - 1] * i % mod;
    g[i] = 1ll * -g[i - 1] * i % mod;
  }
  for (int i = 1; i <= k + 2; i++)
    ans = (ans + 1ll * a[i] * now % mod * fast_pow(n - i, mod - 2) % mod *
                     fast_pow(1ll * f[i - 1] * g[k + 2 - i] % mod, mod - 2) %
                     mod) %
          mod;
  ans = (ans + mod) % mod;
  printf("%d\n", ans);
  return 0;
}
