#include <bits/stdc++.h>
using namespace std;
const int N = 1000005, P = 1000000007;
int n, m, ans, yi[N], fac[N], ifac[N];
void Inc(int &x, int y) {
  x += y;
  if (x >= P) x -= P;
}
int Add(int x, int y) { return x + y < P ? x + y : x + y - P; }
int qsm(int x, int y) {
  int w = x, s = 1;
  while (y) {
    if (y & 1) s = (long long)s * w % P;
    w = (long long)w * w % P;
    y >>= 1;
  }
  return s;
}
int main() {
  scanf("%d%d", &n, &m);
  if (!m) return printf("%d\n", n), 0;
  for (int i = (1); i <= (m + 2); i++) yi[i] = Add(yi[i - 1], qsm(i, m));
  if (n <= m + 2) return printf("%d\n", yi[n]), 0;
  int fz = 1;
  for (int i = (1); i <= (m + 2); i++) fz = (long long)fz * (n - i) % P;
  fac[0] = 1;
  for (int i = (1); i <= (m + 2); i++) fac[i] = (long long)fac[i - 1] * i % P;
  ifac[0] = 1;
  for (int i = (1); i <= (m + 2); i++)
    ifac[i] = (long long)ifac[i - 1] * (P - i) % P;
  for (int i = (1); i <= (m + 2); i++) {
    int FZ = (long long)fz * qsm(n - i, P - 2) % P;
    int FM = (long long)fac[i - 1] * ifac[m + 2 - i] % P;
    Inc(ans, (long long)yi[i] * FZ % P * qsm(FM, P - 2) % P);
  }
  printf("%d\n", ans);
  return 0;
}
