#include <bits/stdc++.h>
#define rep(i,begin, end) for (ll i = begin; i < (ll)(end); i++)

using namespace std;
using ll = long long;

template<typename T>
using vec2 = vector<vector<T>>;
template<typename T>
using vec3 = vec2<vector<T>>;
template<typename T>
using vec4 = vec3<vector<T>>;
template<typename T>
using vec5 = vec4<vector<T>>;
template<typename T>
using vec6 = vec5<vector<T>>;

int gcd(int a, int b){
    if(b == 0)return a;
    return gcd(b, a%b);
}
ll extGCD(ll a, ll b, ll &x, ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    ll g = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return g;
}
//ax+by=cをみたす整数x,y
bool solveLinearEquation2(ll a, ll b, ll c, ll &x, ll &y){
    if(c % gcd(a,b) != 0)return false;
    /*
        (a/d)X+(b/d)Y=c/d
        (a/d)x+(b/d)y=1
        (a/d)(c/d)x+(b/d)(c/d)y=c/d
        X=(c/d)x-k(b/d)
        Y=(c/d)y+k(a/d)
        必要に応じてkを決める
    */
    ll d = extGCD(a,b,x,y);
    ll k = 0;
    x = (c/d)*x-k*(b/d);
    y = (c/d)*y+k*(a/d);
    
    return true;
}
ll MOD = 1000000007;

vector<ll> mfact(1,1);

ll minv(ll a){
    ll x;ll y;
    extGCD(a, MOD, x, y);
    if(x<0){
        x+= ((-x)/MOD+1)*MOD;
        x %= MOD;
    }
    return x;
}

ll mpow(ll a,ll b){
    ll res = 1;
    while(b > 0){
        if(b%2==1)res=(res*a)%MOD;
        b/=2;
        a = (a*a)%MOD;
    }
    return res;
}

ll mfactorial(ll a){
    if(mfact.size()-1 >= a)return mfact[a];
    for(ll i = mfact.size();i<=a;i++){
        mfact.push_back((i * mfact[i-1])%MOD);
    }
    return mfact[a];
}

ll mcomb(ll n, ll r){
    return (((mfactorial(n) * minv(mfactorial(r)))%MOD) * minv(mfactorial(n-r)))%MOD;
}
ll ipow(ll a, ll b){
    ll res = 1;
    while(b > 0){
        if(b % 2 == 1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}

void factorize(map<ll,ll>& fac, vector<ll>& sieved, ll a){
    while(a != 1){
        if(fac.find(sieved[a]) == fac.end())fac[sieved[a]] = 0;
        fac[sieved[a]]++;
        a/=sieved[a];

    }
}
/**
 * エラトステネスのふるい
 * res[i]はiの最小の素因数
 */
void primesieve(vector<ll>& res,ll n){
    res = vector<ll>(n+1, -1);
    for(int i = 2;i <= n;i++){
        if(res[i] != -1)continue;
        res[i] = i;
        for(int j = 2;j*i <= n;j++){
            res[j*i] = i;
        }
    }
}
/**
 * ふるい->素数だけ取り出す
 */
vector<ll> primesuntil(ll n){
    vector<ll> res;
    vector<ll> sieved;
    primesieve(sieved, n);
    for(int i = 2;i <= n;i++){
        if(sieved[i] == i)res.push_back(i);
    }
    return res;
}

int main(){
    ll N,P;cin>>N>>P;
    ll ans = P-1;
    ans = ans * mpow(P-2, N-1);
    ans %= MOD;
    cout<<ans<<endl;
}
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define maxn 
ll mod=1e9+7;
inline ll ksm(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1) ans=ans*base%mod;
		base=base*base%mod;
		b/=2;
	}
	return ans;
}

int main(){
	ll n,p;
	cin>>n>>p;
	cout<<(p-1)*ksm(p-2,n-1)%mod;
	return 0;
}