#include <bits/stdc++.h>
using namespace std;
const int pp = (1e9) + 7;
long long P[1000010];
long long ans, now;
int N, K;
long long quick(int x, int y) {
  long long s = 1, t = x;
  for (; y; y >>= 1) {
    if (y & 1) s = s * t % pp;
    t = t * t % pp;
  }
  return s;
}
long long get0() {
  long long t = 1;
  for (int i = N - K - 1; i < N; i++) t = t * i % pp;
  for (int i = 1; i <= K + 1; i++) t = t * quick(pp - i, pp - 2) % pp;
  return t;
}
int main() {
  scanf("%d%d", &N, &K);
  if (!K) {
    printf("%d\n", N);
    return 0;
  }
  for (int i = 1; i <= K + 2; i++) P[i] = (P[i - 1] + quick(i, K)) % pp;
  if (N <= K + 2) {
    printf("%I64d\n", P[N]);
    return 0;
  }
  ans = 0;
  now = get0();
  for (int i = 1; i <= K + 1; i++) {
    now = now * quick(i, pp - 2) % pp * (N - i + 1) % pp *
          quick(N - i, pp - 2) % pp * (pp - (K - i + 2)) % pp;
    ans = (ans + P[i] * now) % pp;
  }
  printf("%I64d", ans);
}
