#include <bits/stdc++.h>
template <class T>
T read() {
  T s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return w * s;
}
int dbg = 0;
using namespace std;
template <class T>
T cmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T cmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
void cswp(T &a, T &b) {
  T t = a;
  a = b, b = t;
}
struct OI {
  int rp, score;
} CSPS, CSPJ;
const int P = 1e9 + 7;
int n, k;
long long ls[1000010], rs[1000010], fa[1000010];
long long qp(long long a, long long k) {
  long long ret = 1;
  while (k) {
    if (k & 1) ret = ret * a % P;
    a = a * a % P;
    k >>= 1;
  }
  return ret;
}
signed main() {
  ++CSPJ.rp, ++CSPJ.score, ++CSPS.rp, ++CSPS.score, 1004535809;
  n = read<long long>(), k = read<long long>();
  ls[0] = rs[k + 3] = fa[0] = 1;
  for (register int i = 1; i <= k + 2; ++i) ls[i] = ls[i - 1] * (n - i) % P;
  for (register int i = k + 2; i >= 1; --i) rs[i] = rs[i + 1] * (n - i) % P;
  for (register int i = 1; i <= k + 2; ++i) fa[i] = fa[i - 1] * i % P;
  long long sum = 0, ans = 0;
  for (register int i = 1; i <= k + 2; ++i) {
    sum = (sum + qp(i, k)) % P;
    long long x = ls[i - 1] * rs[i + 1] % P;
    long long y = fa[i - 1] * fa[k + 2 - i] * (((k - i) & 1) ? -1 : 1) % P;
    ans = (ans + sum * x % P * qp(y, P - 2) % P) % P;
  }
  return !printf("%lld\n", (ans + P) % P);
}
