#include<bits/stdc++.h> #define re register #define int long long #define Add(a,b) ((((a) % mod + (b) % mod) % mod + mod) % mod) #define Mul(a,b) ((((a) % mod) * ((b) % mod) % mod + mod) % mod) #define Div(a,b) (Mul(a,qmi(((b) % mod + mod) % mod,mod - 2)))
usingnamespace std;
constint mod = 1e9 + 7; int n,k,ans; int mul = 1,g = 1,y;
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inlineintqmi(int a,int b){ int res = 1; while (b){ if (b & 1) res = Mul(res,a); a = Mul(a,a),b >>= 1; } return res; }
inlinevoidsolve1(){ for (re int i = 1;i <= n;i++) ans = Add(ans,qmi(i,k)); }
inlinevoidsolve2(){ for (re int i = 1;i <= k + 2;i++) mul = Mul(mul,n - i); for (re int i = 1;i <= k + 2;i++){ y = Add(y,qmi(i,k)); if (i == 1){ for (re int j = -1;j >= -k - 1;j--) g = Mul(g,j); } else g = Mul(g,Div(i - 1,i - k - 3)); int a = Div(mul,n - i); ans = Add(ans,Mul(y,Div(a,g))); } }
signedmain(){ n = read(),k = read(); if (k + 2 >= n) solve1(); elsesolve2(); printf("%lld",ans); return0; }