#include using namespace std; typedef long long ll; ll mod = 1000000007; ll pw(ll a,ll x){ ll ret = 1; a %= mod; while(x){ if(x&1) (ret *= a) %= mod; x /= 2; (a *= a) %= mod; } return ret; } int main(){ ll n,k,inv = (mod + 1)/2; cin >> n >> k; ll ans = (pw((k + 3)%mod*k%mod*inv%mod,n) - pw((k + 1)%mod*k%mod*inv%mod,n) + mod)%mod; while(ans<0) ans += mod; cout << ans << endl; }