#include <bits/stdc++.h>
using namespace std;
const int N = 1e9 + 3, K = 1e6 + 3, mod = 1e9 + 7;
int n, k, l = 1, val[K], fac[K], ifac[K], ans;
int qpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = (long long)ret * a % mod;
    a = (long long)a * a % mod;
    b >>= 1;
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &k);
  if (k == 0) return printf("%d\n", n), 0;
  for (int i = 1; i <= k + 2; i++) val[i] = (val[i - 1] + qpow(i, k)) % mod;
  if (n <= k + 2) return printf("%d\n", val[n]), 0;
  for (int i = 1; i <= k + 2; i++) l = (long long)l * (n - i) % mod;
  fac[0] = 1;
  for (int i = 1; i <= k + 2; i++) fac[i] = (long long)fac[i - 1] * i % mod;
  ifac[0] = 1;
  for (int i = 1; i <= k + 2; i++) ifac[i] = (long long)ifac[i - 1] * -i % mod;
  for (int i = 1; i <= k + 2; i++) {
    ans = (ans + (long long)val[i] * l % mod * qpow(n - i, mod - 2) % mod *
                     qpow(fac[i - 1], mod - 2) % mod *
                     qpow(abs(ifac[k + 2 - i]), mod - 2) % mod *
                     (ifac[k + 2 - i] < 0 ? -1 : 1)) %
          mod;
  }
  printf("%d\n", (ans % mod + mod) % mod);
  return 0;
}
