#include <bits/stdc++.h>
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 10;
long long n, k, fz, fm, ans, y, x;
long long fac[maxn], pre[maxn], suf[maxn];
inline long long ksm(long long a, long long b, long long p) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return ret;
}
int main(void) {
  n = read(), k = read();
  pre[0] = 1;
  suf[k + 3] = 1;
  fac[0] = 1;
  for (int i = 1; i <= k + 2; i++) pre[i] = pre[i - 1] * (n - i) % mod;
  for (int i = k + 2; i; i--) suf[i] = suf[i + 1] * (n - i) % mod;
  for (int i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % mod;
  for (int i = 1; i <= k + 2; i++) {
    y = (y + ksm(i, k, mod)) % mod;
    fz = pre[i - 1] * suf[i + 1] % mod;
    fm = fac[i - 1] * fac[k + 2 - i] % mod * (((k + 2 - i) % 2 == 0) ? 1 : -1);
    ans = (ans + y * fz % mod * ksm(fm, mod - 2, mod) % mod) % mod;
    ans = (ans + mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
