#include <bits/stdc++.h>
const int MOD = 1e9 + 7, MAXK = 1e6 + 10;
int n, k, pre[MAXK], suc[MAXK], fac[MAXK], qwq[MAXK];
int ksm(int _, int __) {
  int ans = 1;
  while (__) {
    if (__ & 1) ans = 1LL * ans * _ % MOD;
    _ = 1LL * _ * _ % MOD;
    __ >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &k);
  pre[0] = n, suc[k + 2] = 1, fac[1] = 1;
  for (int i = 1; i <= k + 1; i++) pre[i] = 1LL * pre[i - 1] * (n - i) % MOD;
  for (int i = k + 1; i >= 0; i--) suc[i] = 1LL * suc[i + 1] * (n - i) % MOD;
  for (int i = 2; i <= k + 1; i++)
    fac[i] = 1LL * (MOD - MOD / i) * fac[MOD % i] % MOD;
  for (int i = 2; i <= k + 1; i++) fac[i] = 1LL * fac[i] * fac[i - 1] % MOD;
  int now = 0, ans = 0;
  fac[0] = 1;
  for (int i = 0; i <= k + 1; i++) {
    now = (now + (i ? ksm(i, k) : 0)) % MOD;
    ans =
        (ans + 1LL * ksm(-1, k + 1 - i) * now * (i > 0 ? pre[i - 1] : 1) % MOD *
                   (i <= k ? suc[i + 1] : 1) % MOD * (i > 0 ? fac[i] : 1) %
                   MOD * (k + 1 - i > 0 ? fac[k + 1 - i] : 1) % MOD) %
        MOD;
  }
  printf("%d\n", (ans + MOD) % MOD);
  return 0;
}
