#include <bits/stdc++.h>
#include <iostream>
#include <random>
using namespace std;
using ull = long long;
ull modmul(ull x, ull y, ull mod){
	ull ret = 0;
	while(y){
		if(y & 1){
			ret = (ret + x) % mod;
		}
		x = x * 2 % mod;
		y >>= 1;
	}
	return ret;
}

ull modpow(ull x, ull n, ull mod){
	ull ret = 1;
	while(n){
		if(n & 1){
			ret = modmul(ret, x, mod);
		}
		x = modmul(x, x, mod);
		n >>= 1;
	}
	return ret;
}
int main(){
  ull a,b;
 cin>>b>>a;
  ull mod=1000000007;
  a=a%mod;
  ull s=(a*(a+3)%mod);
  ull k=(a*(a+1)%mod);
  cout<<modpow(s,b,mod)%mod-modpow(k,b,mod)%mod<<endl;
}