#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
const int mod1 = 1234567891;
long long inv[maxn];
long long qpow(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y % 2) ans = ans * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
inline void Init(int n) {
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; i++) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  for (int i = 2; i <= n; i++) inv[i] = inv[i] * inv[i - 1] % mod;
}
long long fac[maxn];
long long f[maxn];
long long fst[maxn];
long long lst[maxn];
int main() {
  Init(maxn - 1);
  int i, j;
  long long n, k;
  fac[0] = 1;
  for (i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (i = 1; i <= k + 2; i++) {
    f[i] = (f[i - 1] + qpow(i, k)) % mod;
  }
  long long ans = 0;
  if (n <= k + 2) {
    cout << f[n] << endl;
    return 0;
  }
  fst[0] = 1;
  lst[k + 3] = 1;
  for (i = 1; i <= k + 2; i++) {
    fst[i] = fst[i - 1] * (n - i) % mod;
  }
  for (i = k + 2; i >= 1; i--) {
    lst[i] = lst[i + 1] * (n - i) % mod;
  }
  for (i = 1; i <= k + 2; i++) {
    long long now = ((fst[i - 1] * lst[i + 1] % mod)) * f[i] % mod;
    now = now * qpow(fac[i - 1] * fac[k + 2 - i] % mod, mod - 2) % mod;
    if ((k + 2 - i) % 2) now = now * -1;
    ans = (ans + now) % mod;
  }
  ans = (ans + mod) % mod;
  cout << ans << endl;
  return 0;
}
