#include <bits/stdc++.h>
using namespace std;
int getint() {
  char ch = getchar();
  int f = 1, x = 0;
  while (!isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
const long long MOD = 1000000007;
int n, k;
long long val[1000100];
long long qpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    (b & 1) ? res = res * a % MOD : 0;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  long long tx, ty;
  exgcd(b, a % b, tx, ty);
  x = ty;
  y = tx - a / b * ty;
}
long long rev(long long x) {
  x = (x + MOD) % MOD;
  long long tmp, res;
  exgcd(x, MOD, res, tmp);
  res %= MOD;
  res += MOD;
  res %= MOD;
  return res;
}
void init() {
  n = getint(), k = getint();
  val[1] = 1;
  for (int i = 2; i <= k + 2; i++) {
    val[i] = (val[i - 1] + qpow(i, k)) % MOD;
  }
}
int main() {
  init();
  if (n <= k + 2) {
    printf("%I64d\n", val[n]);
  } else {
    long long ans = 0;
    long long tmp = 1;
    for (int i = 2; i <= k + 2; i++) {
      tmp = tmp * (n - i) % MOD;
      tmp = tmp * rev(1 - i) % MOD;
    }
    ans += tmp * val[1];
    ans %= MOD;
    for (int i = 1; i < k + 2; i++) {
      tmp = tmp * (n - i) % MOD;
      tmp = tmp * rev(n - i - 1) % MOD;
      tmp = tmp * rev(i) % MOD;
      tmp = tmp * (MOD + i - k - 2) % MOD;
      ans += tmp * val[i + 1];
      ans %= MOD;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
