#include <bits/stdc++.h>
const int mo = 1e9 + 7;
int fac[1000005], inv[1000005], f[1000005], invfac[1000005];
int qpow(int a, int b) {
  int res = 1, Pow = a;
  while (b) {
    if (b & 1) res = 1LL * res * Pow % mo;
    Pow = 1LL * Pow * Pow % mo;
    b >>= 1;
  }
  return res;
}
int main() {
  int n, k, K;
  scanf("%d%d", &n, &k);
  int ans = 0;
  K = k;
  k += 2;
  if (n <= k) {
    for (int i = 1; i <= n; i++) ans = (ans + qpow(i, K)) % mo;
    printf("%d\n", ans);
    return 0;
  }
  int prod = 1;
  inv[0] = fac[0] = 1;
  for (int i = 1; i <= k; i++) prod = (1LL * prod * (n - i)) % mo;
  for (int i = 1; i <= k; i++) inv[i] = qpow(n - i, mo - 2);
  for (int i = 1; i <= k; i++) fac[i] = 1LL * fac[i - 1] * i % mo;
  invfac[k] = qpow(fac[k], mo - 2);
  for (int i = k - 1; i >= 0; i--)
    invfac[i] = 1LL * invfac[i + 1] * (i + 1) % mo;
  for (int i = 1; i <= k; i++) {
    f[i] = (f[i - 1] + qpow(i, K)) % mo;
    if ((i + k) & 1)
      ans -= 1LL * f[i] * prod % mo * inv[i] % mo * invfac[i - 1] % mo *
             invfac[k - i] % mo;
    else
      ans += 1LL * f[i] * prod % mo * inv[i] % mo * invfac[i - 1] % mo *
             invfac[k - i] % mo;
    ans %= mo;
    ans += mo;
    ans %= mo;
  }
  printf("%d\n", ans);
}
