#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, k, pre[N], suf[N], fact[N];
inline int quick_pow(int x, int p = mod - 2, int mul = 1) {
  for (; p; p >>= 1, x = 1LL * x * x % mod)
    if (p & 1) mul = 1LL * mul * x % mod;
  return mul;
}
inline int Lagrange(const int& n, const int& k) {
  register int i;
  for (pre[0] = i = 1; i <= n; ++i) pre[i] = 1LL * pre[i - 1] * (k - i) % mod;
  for (suf[n + 1] = 1, i = n; i; --i) suf[i] = 1LL * suf[i + 1] * (k - i) % mod;
  for (fact[0] = i = 1; i <= n; ++i) fact[i] = 1LL * fact[i - 1] * i % mod;
  int ret = 0, y = 0;
  for (i = 1; i <= n; ++i) {
    (y += quick_pow(i, n - 2)) %= mod;
    int val = 1LL * pre[i - 1] * suf[i + 1] % mod *
              quick_pow(1LL * fact[i - 1] * fact[n - i] % mod) % mod;
    if ((n - i) & 1)
      (ret -= 1LL * y * val % mod) %= mod;
    else
      (ret += 1LL * y * val % mod) %= mod;
  }
  return (ret + mod) % mod;
}
int main() {
  return scanf("%d%d", &n, &k), printf("%d", Lagrange(k + 2, n)), 0;
}
