#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long fastPow(long long a, long long pw, long long mod) {
  long long res = 1;
  a %= mod;
  while (pw > 0) {
    if (pw & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    pw >>= 1;
  }
  return res;
}
int faulhaber(long long n, int k) {
  vector<long long> sum(k + 5, 0);
  for (long long i = 1; i <= k + 2; i++)
    sum[i] = (sum[i - 1] + fastPow(i, k, mod)) % mod;
  if (n <= k + 2) return sum[n];
  long long t = 1;
  vector<long long> fac(k + 5, 1);
  for (long long i = 1; i <= k + 2; i++) {
    t = (n - i) * t % mod;
    fac[i] = fac[i - 1] * i % mod;
  }
  long long ans = 0;
  for (long long i = 1; i <= k + 2; i++) {
    long long t1 = fastPow(fac[i - 1] * fac[k - i + 2] % mod, mod - 2, mod);
    long long t2 = fastPow(n - i, mod - 2, mod);
    if ((k - i + 2) & 1) t1 = -t1;
    ans = (ans + sum[i] * t % mod * t1 % mod * t2 % mod) % mod;
  }
  return (ans + mod) % mod;
}
int main() {
  int n, k;
  cin >> n >> k;
  cout << faulhaber(n, k) << '\n';
  return 0;
}
