#include <bits/stdc++.h>
const long long mod = 1000000007;
using namespace std;
int k;
int n;
long long fac[1000006];
long long y[1000006];
long long pi = 1;
long long q_mod(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  cin >> n >> k;
  y[0] = 0;
  for (int i = 1; i <= k + 2; i++) {
    y[i] = (y[i - 1] + q_mod(i, k, mod)) % mod;
  }
  if (n <= k + 2) {
    cout << y[n] << endl;
    return 0;
  }
  fac[0] = 1;
  for (int i = 1; i <= 1000006; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }
  for (int i = 1; i <= k + 2; i++) {
    pi = (pi * (n - i)) % mod;
  }
  long long ans = 0;
  for (int i = 1; i <= k + 2; i++) {
    long long f = ((k + 2 - i) % 2) ? -1 : 1;
    long long inv =
        q_mod(fac[k + 2 - i] % mod * fac[i - 1] % mod, mod - 2, mod);
    long long inv2 = q_mod(n - i, mod - 2, mod);
    ans = (ans + f * y[i] * pi % mod * inv % mod * inv2 % mod) % mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
