#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
long long q_pow(long long a, int b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) ret = ret * a % mod;
  return ret;
}
long long f[maxn], invF[maxn], inv[maxn];
int n, k;
long long M = 1;
void init() {
  M = n;
  for (int i = 1; i <= k + 1; i++) f[i] = (f[i - 1] + q_pow(i, k)) % mod;
  invF[0] = 1;
  for (int i = 1; i <= k + 1; i++)
    invF[i] = invF[i - 1] * i % mod, M = M * (n - i) % mod;
  invF[k + 1] = q_pow(invF[k + 1], mod - 2);
  for (int i = k; i > 0; i--) invF[i] = invF[i + 1] * (i + 1) % mod;
  for (int i = 1; i <= k + 1; i++) inv[i] = q_pow(n - i, mod - 2);
}
long long cal() {
  long long ret = 0, t, ty;
  for (int i = 1; i <= k + 1; i++) {
    t = M * inv[i] % mod;
    ty = invF[i] * invF[k + 1 - i] % mod;
    if (~(k - i) & 1) ty = (mod - ty) % mod;
    ret = (ret + t * ty % mod * f[i]) % mod;
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &k);
  if (!k) return 0 * printf("%d\n", n);
  init();
  if (n <= k + 1) return 0 * printf("%lld\n", f[n]);
  printf("%lld\n", cal());
  return 0;
}
