#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const long long MOD = 1000000007;
long long n, k;
long long pre[MAXN], suf[MAXN], fac[MAXN], ifac[MAXN];
inline long long quick_pow(long long x, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) (res *= x) %= MOD;
    (x *= x) %= MOD;
    k >>= 1;
  }
  return res;
}
inline void init() {
  pre[0] = suf[k + 3] = fac[0] = 1;
  for (int i = 1; i <= k + 2; i++) pre[i] = pre[i - 1] * (n - i) % MOD;
  for (int i = k + 2; i >= 1; i--) suf[i] = suf[i + 1] * (n - i) % MOD;
  for (int i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % MOD;
  ifac[k + 2] = quick_pow(fac[k + 2], MOD - 2);
  for (int i = k + 2 - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % MOD;
}
inline long long calc(int i, long long tmp) {
  return ((k - i) & 1 ? -1 : 1) * pre[i - 1] * suf[i + 1] % MOD * ifac[i - 1] %
         MOD * ifac[k + 2 - i] % MOD * tmp % MOD;
}
int main() {
  cin >> n >> k;
  init();
  long long ans = 0, tmp = 0;
  for (int i = 1; i <= k + 2; i++) {
    (tmp += quick_pow(i, k)) %= MOD;
    (ans += calc(i, tmp) % MOD) %= MOD;
  }
  cout << (ans + MOD) % MOD;
  return 0;
}
