#include<bits/stdc++.h>

using namespace std;

long long f[1010];
const long long p=10000007;
int n,m;
int main()
{
	scanf("%d%d",&n,&m);
	f[1]=m;
	f[2]=m%p*(m-1)%p;
	f[3]=m%p*(m-1)%p*(m-2)%p;
	for (int i=4;i<=n;i++)
		f[i]=(m-2)%p*f[i-1]%p+(m-1)%p*f[i-2]%p;
	printf("%lld\n",f[n]%p);
	return 0;
 } 
