#include <bits/stdc++.h>
using namespace std;
const long long N = 2e6;
const long long mod = 1e9 + 7;
long long fac[N];
long long na_fac[N];
long long re_fac[N];
long long POW[N];
long long qpow(long long a, long long k, long long mod) {
  long long ret = 1;
  long long x = a;
  while (k) {
    if (k & 1) (ret *= x) %= mod;
    (x *= x) %= mod;
    k >>= 1;
  }
  return ret;
}
long long inv(long long a) { return (qpow(a, mod - 2, mod) % mod + mod) % mod; }
long long work(long long n, long long k) {
  if (k == 0) return n;
  fac[1] = 1;
  fac[0] = 1;
  na_fac[0] = 1;
  POW[0] = 1;
  na_fac[1] = -1;
  POW[1] = 1;
  for (long long i = 2; i <= k + 2; i++) {
    fac[i] = fac[i - 1] * i % mod;
    na_fac[i] = na_fac[i - 1] * (-1 * i) % mod;
    POW[i] = qpow(i, k, mod) + POW[i - 1];
    POW[i] %= mod;
  }
  long long ffz = 1;
  long long flag = -1;
  for (long long i = 1; i <= k + 2; i++) {
    if (n - i == 0) {
      flag = i;
      continue;
    }
    ffz = ffz * (n - i) % mod;
  }
  long long FZZ = ffz;
  long long ans = 0;
  for (long long i = 1; i <= k + 2; i++) {
    long long fz = POW[i] * ffz % mod * inv(n - i) % mod;
    if (flag != -1) {
      if (flag == i)
        fz = POW[i] * FZZ % mod;
      else
        fz = 0;
    }
    long long fm = fac[i - 1] * na_fac[k + 2 - i] % mod;
    fz = (fz + mod) % mod;
    fm = (fm + mod) % mod;
    ans += fz * inv(fm) % mod;
    ans %= mod;
  }
  return ans;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  cout << work(n, k) << endl;
}
signed main() {
  long long t = 1;
  while (t--) {
    solve();
  }
}
