#include <bits/stdc++.h>
using namespace std;
int n, k;
int summ[1000005], fact[1000005], inv[1000005], ans;
int lp[1000005], rp[1000005];
int po(int a, int b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  int cur = po(a, b >> 1);
  cur = 1LL * cur * cur % 1000000007;
  return 1LL * cur * po(a, b & 1) % 1000000007;
}
int main() {
  scanf("%d %d", &n, &k);
  summ[0] = 0;
  for (int i = (1); i <= (k + 1); i++)
    summ[i] = (summ[i - 1] + po(i, k)) % 1000000007;
  fact[0] = 1;
  for (int i = (1); i <= (k + 1); i++)
    fact[i] = 1LL * fact[i - 1] * i % 1000000007;
  for (int i = (0); i <= (k + 1); i++) inv[i] = po(fact[i], 1000000007 - 2);
  if (n <= k + 1)
    printf("%d\n", summ[n]);
  else {
    lp[0] = 1;
    for (int i = (0); i <= (k + 1); i++)
      lp[i + 1] = 1LL * lp[i] * (n - i) % 1000000007;
    rp[k + 2] = 1;
    for (int i = (k + 1); i >= (0); i--)
      rp[i] = 1LL * rp[i + 1] * (n - i) % 1000000007;
    for (int i = (0); i <= (k + 1); i++) {
      int cur = 1LL * lp[i] * rp[i + 1] % 1000000007;
      if ((k + 1 - i) & 1) cur *= -1;
      cur = 1LL * cur * inv[i] % 1000000007;
      cur = 1LL * cur * inv[k + 1 - i] % 1000000007;
      ans = (ans + 1LL * summ[i] * cur) % 1000000007;
    }
    printf("%d\n", (ans + 1000000007) % 1000000007);
  }
  return 0;
}
