#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') w = -1;
  for (; isdigit(ch); ch = getchar()) c = (c << 3) + (c << 1) + (ch ^ 48);
  return w * c;
}
const int mod = 1e9 + 7, M = 1e6 + 10;
long long fast(long long x, long long p) {
  long long ret = 1;
  for (; p; p >>= 1, x = x * x % mod)
    if (p & 1) ret = ret * x % mod;
  return ret;
}
int n, k;
long long f[M], pr[M], sf[M], Y[M], inv[M];
int main() {
  n = read();
  k = read();
  f[0] = 1;
  for (int i = 1; i <= k + 1; ++i)
    Y[i] = (Y[i - 1] + fast(i, k)) % mod, f[i] = f[i - 1] * i % mod;
  inv[k + 1] = fast(f[k + 1], mod - 2);
  for (int i = k + 1; i >= 1; --i) inv[i - 1] = inv[i] * i % mod;
  sf[k + 3] = pr[0] = 1;
  for (int i = 1; i <= k + 2; ++i) pr[i] = pr[i - 1] * (n - i + 1) % mod;
  for (int i = k + 2; i >= 1; --i) sf[i] = sf[i + 1] * (n - i + 1) % mod;
  long long ans = 0;
  for (int i = 0; i <= k + 1; ++i) {
    if (n <= k + 1) {
      if (i == n) ans = (ans + Y[i]) % mod;
    } else {
      long long o = Y[i] * pr[i] % mod * sf[i + 2] % mod * inv[i] % mod *
                    inv[k + 1 - i] % mod;
      if ((k + 1 - i) & 1)
        ans = (ans + mod - o) % mod;
      else
        ans = (ans + o) % mod;
    }
  }
  cout << ans << "\n";
  return 0;
}
