#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int n,m,ans,sum;
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch>'9'||ch<'0')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch<='9'&&ch>='0')
	{
		x=x*10+ch-48;
		ch=getchar();
	}
	return x*f;
}
signed main()
{
	freopen("barrack.in","r",stdin);
	freopen("barrack.out","w",stdout);
	cin>>n>>m;
	if(m==n-1)
	{
		ans=1;sum=1;
		for(int i=1;i<n;i++)ans=(ans*2)%mod;
		for(int i=1;i<n-1;i++)sum=(sum*2)%mod;
		ans=(ans*n)%mod;
		if(n-2!=0)sum=(sum*(n-2))%mod;
		cout<<(ans+sum)%mod<<endl;
	}
	return 0;
}
