
#include"bits/stdc++.h"
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(x) x.begin(),x.end()
#define BACK(x) x.rbegin(),x.rend()
#define MOD 1000000007
#define INF INT_MAX
#define FLOAT_ANS setprecision(30)
#define TORAD(x) (x*acos(-1)/180.0)
#define TODEG(x) (x*180/acos(-1))
#define elif else if

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

template<typename T>
bool chmin(T& a,T b){
	if(a>b){
		a=b;
		return true;
	}
	return false;
}

template<typename T>
bool chmax(T& a,T b){
	if(a<b){
		a=b;
		return true;
	}
	return false;
}

ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * (a%mod)) % mod;
        a = ((a%mod) * (a%mod)) % mod;
        n >>= 1;
    }
    return res;
}

// MAIN PROGRAM -------------

int main(){
	ll n,p;
	cin>>n>>p;
	--p;
	ll ans=modpow(p-1,n-1,MOD);
	cout<<(ans*p)%MOD;
}

/*

*/
#include<algorithm>
#include<iostream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<bitset>
#include<cstdio>
#include<string>
#include<time.h>
#include<vector>
#include<cmath>
#include<deque>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int M=1e9+7;
int ksm(int a,int b)
{
	int c=1;
	while(b)
	{
		if(b&1)
			c=1ll*c*a%M;
		a=1ll*a*a%M;
		b>>=1;
	}
	return c;
}
int main()
{
	int n,p,a=1;
	scanf("%d%d",&n,&p);
	printf("%lld",1ll*ksm(p-2,n-1)*(p-1)%M);
}