#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int f[1000005], pre[1000005], suf[1000005];
inline int dec(const int& a, const int& b) {
  return a < b ? a - b + MOD : a - b;
}
inline int qpow(int a, int p) {
  int ans = 1;
  while (p) {
    if (p & 1) ans = (long long)ans * a % MOD;
    a = (long long)a * a % MOD;
    p >>= 1;
  }
  return ans;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  f[0] = 1;
  for (int i = 1; i <= k + 1; i++) f[i] = (long long)f[i - 1] * i % MOD;
  f[k + 1] = qpow(f[k + 1], MOD - 2);
  for (int i = k; i >= 0; i--) f[i] = (long long)f[i + 1] * (i + 1) % MOD;
  pre[0] = 1;
  for (int i = 1; i <= k + 2; i++)
    pre[i] = (long long)pre[i - 1] * dec(n, i) % MOD;
  suf[k + 3] = 1;
  for (int i = k + 2; i >= 0; i--)
    suf[i] = (long long)suf[i + 1] * dec(n, i) % MOD;
  int ans = 0, sum = 0;
  for (int i = 1; i <= k + 2; i++) {
    sum += qpow(i, k);
    if (sum >= MOD) sum -= MOD;
    int mul = sum;
    mul = (long long)mul * pre[i - 1] % MOD * suf[i + 1] % MOD;
    mul = (long long)mul * f[i - 1] % MOD *
          (((k - i) & 1) ? MOD - f[k - i + 2] : f[k - i + 2]) % MOD;
    ans += mul;
    if (ans >= MOD) ans -= MOD;
  }
  printf("%d\n", ans);
  return 0;
}
