#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000LL;
long long qpowmod(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
const int MAXN = 1000003;
const int MOD = 1000000007;
long long rev[MAXN];
long long Y[MAXN];
long long X[MAXN];
long long numProdPref[MAXN];
long long numProdSuff[MAXN];
long long revProd[MAXN];
long long getRev(long long x) {
  assert(llabs(x) < MAXN);
  if (x < 0)
    return (MOD - rev[-x]);
  else
    return rev[x];
}
int main() {
  int k, n;
  scanf("%d%d", &n, &k);
  rev[1] = 1;
  for (int i = 2; i < MAXN; i++)
    rev[i] = (MOD - (MOD / i) * rev[MOD % i] % MOD);
  revProd[0] = 1;
  for (int i = 1; i < MAXN; i++) revProd[i] = revProd[i - 1] * rev[i] % MOD;
  for (int i = 1; i <= k + 1; i++) Y[i] = (Y[i - 1] + qpowmod(i, k, MOD)) % MOD;
  for (int j = 0; j <= k + 1; j++) X[j] = n - j;
  numProdPref[0] = 1;
  numProdSuff[k + 2] = 1;
  for (int i = 0; i <= k + 1; i++)
    numProdPref[i + 1] = numProdPref[i] * X[i] % MOD;
  for (int i = k + 1; i >= 0; i--)
    numProdSuff[i] = numProdSuff[i + 1] * X[i] % MOD;
  long long res = 0;
  for (int i = 1; i <= k + 1; i++) {
    long long inc = Y[i] * numProdPref[i] % MOD * numProdSuff[i + 1] % MOD *
                    revProd[i] % MOD * revProd[k + 1 - i] % MOD;
    if ((k + 1 - i) % 2 == 1) inc = (MOD - inc) % MOD;
    res = (res + inc) % MOD;
  }
  printf("%lld\n", res);
  return 0;
}
