#include <bits/stdc++.h>
using namespace std;
long long n, k, ans, tmp;
long long pre[1000100], suf[1000100], fac[1000100];
long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 1) + (s << 3) + ch - '0', ch = getchar();
  return s * w;
}
long long quickpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % 1000000007;
    x = x * x % 1000000007;
    y >>= 1;
  }
  return res;
}
signed main() {
  n = read();
  k = read();
  pre[0] = fac[0] = suf[k + 3] = 1;
  for (long long i = k + 2; i >= 1; i--)
    suf[i] = suf[i + 1] * (n - i) % 1000000007;
  for (long long i = 1; i <= k + 2; i++)
    fac[i] = fac[i - 1] * i % 1000000007,
    pre[i] = pre[i - 1] * (n - i) % 1000000007;
  for (long long i = 1, fir, sec; i <= k + 2; i++) {
    tmp = (tmp + quickpow(i, k)) % 1000000007;
    fir = pre[i - 1] * suf[i + 1] % 1000000007;
    sec = fac[i - 1] * ((k - i) & 1 ? -1 : 1) * fac[k + 2 - i] % 1000000007;
    ans = ((ans + tmp * fir % 1000000007 * quickpow(sec, 1000000007 - 2) %
                      1000000007) %
               1000000007 +
           1000000007) %
          1000000007;
  }
  printf("%lld\n", ans);
  return 0;
}
