#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long f[1000010], fac[1000010];
long long Pow(long long x, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = ans * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}
long long n_k(int n, int k) {
  f[0] = 0;
  for (int i = 1; i <= k + 2; ++i) f[i] = (f[i - 1] + Pow(i, k)) % mod;
  if (n <= k + 2) return f[n];
  fac[0] = 1;
  for (int i = 1; i <= k + 2; ++i) fac[i] = fac[i - 1] * i % mod;
  long long cur = 1, ans = 0;
  for (int i = 1; i <= k + 2; ++i) cur = cur * (n - i) % mod;
  for (int i = 1; i <= k + 2; ++i) {
    long long inv1 = Pow(n - i, mod - 2);
    long long inv2 = Pow(fac[i - 1] % mod * fac[k + 2 - i] % mod, mod - 2);
    int sign = (k + 2 - i) % 2 ? -1 : 1;
    ans = (ans + sign * inv1 * inv2 % mod * f[i] % mod * cur % mod) % mod;
  }
  ans = ans + mod;
  if (ans >= mod) ans -= mod;
  return ans;
}
int main() {
  int n, k;
  cin >> n >> k;
  cout << n_k(n, k) << endl;
  return 0;
}
