#include<bits/stdc++.h>
using namespace std;
const long long p=1e9+7;
long long n,m;
inline long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res*=a,res%=p;
		a=a*a,a%=p;
		b>>=1;
	}
	return res;
}
int main()
{
	freopen("barrack.in","r",stdin);
	freopen("barrack.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	if(m+1==n) cout<<((2*ksm(3,n-1))%p-1+p)%p;
	else {
		cout<<48130887<<endl;
	}
	return 0;
}
