#include <bits/stdc++.h>
using namespace std;
int const xn = 1e6 + 5, mod = 1e9 + 7;
int n, k, f[xn];
long long fac[xn];
int pw(long long a, int b) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % mod)
    if (b & 1) ret = (ret * a) % mod;
  return ret;
}
int upt(int x) {
  while (x >= mod) x -= mod;
  while (x < 0) x += mod;
  return x;
}
long long get(int i) {
  if (i == k + 2) return fac[i - 1];
  return fac[i - 1] * fac[k + 2 - i] % mod;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k + 2; i++) f[i] = upt(f[i - 1] + pw(i, k));
  if (n <= k + 2) {
    printf("%d\n", f[n]);
    return 0;
  }
  int sum = 0;
  long long s1 = 1;
  fac[0] = 1;
  for (int i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % mod;
  for (int i = 1; i <= k + 2; i++) s1 = s1 * (n - i) % mod;
  for (int i = 1; i <= k + 2; i++) {
    long long t1 = s1 * pw(n - i, mod - 2) % mod;
    long long s2 = pw(get(i), mod - 2);
    if ((k + 2 - i) % 2) s2 = -s2;
    sum = (sum + t1 * s2 % mod * f[i]) % mod;
  }
  printf("%d\n", upt(sum));
  return 0;
}
