#include <bits/stdc++.h>
using namespace std;
int n, k, y[1000005];
long long fpow(long long a, long long b) {
  long long r = 1;
  for (; b; b /= 2, a = (a * a) % 1000000007)
    if (b & 1) r = (r * a) % 1000000007;
  return r;
}
int main() {
  scanf("%d %d", &n, &k);
  int s = 0;
  for (int i = 0; i <= k + 1; i++) {
    y[i] = s;
    s = (s + fpow(i + 1, k)) % 1000000007;
  }
  if (n <= k + 1)
    printf("%d\n", y[n]);
  else {
    int ans = 0, top = 1;
    for (int j = 0; j <= k + 1; j++) top = (top * 1LL * (n - j)) % 1000000007;
    int pre = 1, suf = 1;
    for (int i = 1; i <= k + 1; i++) suf = (suf * 1LL * i) % 1000000007;
    for (int i = 0; i <= k + 1; i++) {
      int t = (top * fpow(n - i, 1000000007 - 2)) % 1000000007;
      int down = (pre * 1LL * suf) % 1000000007;
      int here = (t * fpow(down, 1000000007 - 2)) % 1000000007;
      ans = (ans + here * 1LL * y[i]) % 1000000007;
      pre = (pre * 1LL * (-(i + 1))) % 1000000007;
      suf = (suf * fpow(k + 1 - i, 1000000007 - 2)) % 1000000007;
    }
    printf("%d\n", abs(ans));
  }
  return 0;
}
