#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') t = -1;
    c = getchar();
  }
  while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
  return s * t;
}
const int maxn = 1e6 + 5, p = 1e9 + 7;
long long rev[maxn] = {1, 1}, L[maxn], R[maxn], f[maxn];
inline int Pow(long long x, int t) {
  int ret = 1;
  while (t) {
    if (t & 1) ret = ret * x % p;
    x = x * x % p, t >>= 1;
  }
  return ret;
}
int main() {
  int n = read(), k = read();
  if (!k) return printf("%d\n", n), 0;
  for (register int i = 1; i <= k + 2; i++) f[i] = (Pow(i, k) + f[i - 1]) % p;
  L[0] = R[k + 3] = 1;
  for (register int i = 1; i <= k + 2; i++) L[i] = L[i - 1] * (n - i) % p;
  for (register int i = k + 2; i >= 1; i--) R[i] = R[i + 1] * (n - i) % p;
  for (register int i = 2; i <= k + 2; i++)
    rev[i] = (p - p / i) * rev[p % i] % p;
  for (register int i = 2; i <= k + 2; i++) rev[i] = rev[i - 1] * rev[i] % p;
  long long ans = 0, tmp;
  for (register int i = 1; i <= k + 2; i++) {
    tmp = L[i - 1] * R[i + 1] % p;
    tmp = f[i] * tmp % p * rev[i - 1] % p * rev[k + 2 - i] % p;
    if ((k + 2 - i) & 1) tmp = -tmp;
    ans = ((ans + tmp) % p + p) % p;
  }
  cout << ans << endl;
  return 0;
}
