#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long f[1000010], fac_inv[1000010], inv[1000010];
void init() {
  inv[1] = 1;
  for (int i = 2; i < 1000010; i++) {
    if (i >= mod) break;
    inv[i] = (mod - mod / i) * inv[mod % i] % mod;
  }
  fac_inv[0] = 1;
  for (int i = 1; i < 1000010; ++i) fac_inv[i] = fac_inv[i - 1] * inv[i] % mod;
}
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];
  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 = (n - i < 1000010) ? inv[n - i] : Pow(n - i, mod - 2);
    long long inv2 = fac_inv[i - 1] * fac_inv[k + 2 - i] % mod;
    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() {
  init();
  int n, k;
  cin >> n >> k;
  cout << n_k(n, k) << endl;
  return 0;
}
