#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 1;
const int mod = 1e9 + 7;
long long y[MAX], fac[MAX], b[MAX];
long long quick(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  long long n, k;
  scanf("%lld%lld", &n, &k);
  fac[0] = 1;
  for (long long i = 1; i <= k + 2; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }
  if (k == 0) {
    cout << n << endl;
    return 0;
  }
  y[0] = 0;
  for (long long i = 1; i <= k + 2; i++) {
    y[i] = (y[i - 1] + quick(i, k)) % mod;
  }
  if (n <= k + 2) {
    cout << y[n] << endl;
    return 0;
  }
  long long ans1 = 1;
  for (long long i = 1; i <= k + 2; i++) ans1 = (ans1 * (n - i)) % mod;
  long long ans = 0;
  for (long long i = 1; i <= k + 2; i++) {
    long long p1 = quick(n - i, mod - 2) % mod;
    long long p2 = quick(fac[i - 1] * fac[k + 2 - i] % mod, mod - 2) % mod;
    long long fu = ((k + 2 - i) % 2 == 1 ? -1 : 1);
    long long cur = y[i] * p1 % mod * p2 % mod * ans1 % mod;
    cur *= fu;
    if (cur < 0) cur = (cur + mod) % mod;
    ans = (ans + cur) % mod;
  }
  cout << ans << endl;
  return 0;
}
