#include <bits/stdc++.h>
using namespace std;
long long qpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % ((long long)1e9 + 7);
    a = a * a % ((long long)1e9 + 7);
    n >>= 1;
  }
  return res;
}
long long n, m, k;
long long y[1000005], fac[1000005], inv[1000005];
void init() {
  fac[0] = 1;
  for (int i = 1; i < 1000005; i++)
    fac[i] = fac[i - 1] * i % ((long long)1e9 + 7);
  inv[1000005 - 1] = qpow(fac[1000005 - 1], ((long long)1e9 + 7) - 2);
  for (int i = 1000005 - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % ((long long)1e9 + 7);
}
int main() {
  init();
  cin >> k >> m;
  n = m + 2;
  long long g = 1;
  for (int i = 1; i <= n; i++) {
    y[i] = (y[i - 1] + qpow(i, m)) % ((long long)1e9 + 7);
    g = g * (k - i) % ((long long)1e9 + 7);
  }
  if (k <= n) {
    cout << y[k] << endl;
    return 0;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long t = inv[i - 1] * inv[n - i] % ((long long)1e9 + 7);
    if ((n - i) & 1) t = -t;
    ans = (ans + y[i] * t % ((long long)1e9 + 7) *
                     qpow(k - i, ((long long)1e9 + 7) - 2) %
                     ((long long)1e9 + 7)) %
          ((long long)1e9 + 7);
  }
  ans = (ans * g % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %
        ((long long)1e9 + 7);
  cout << ans << endl;
  return 0;
}
