#include<bits/stdc++.h>
using namespace std;
long long n,p,ans,bs;
long long MOD=1000000007;
int main()
{
	cin>>n>>p;
	if(p==2)
	{
		if(n==1)cout<<"1";
		else cout<<"0";
	}
	else if(p==3)cout<<"2";
	else
	{
		ans=(p-1)%MOD;
		bs=(p-2)%MOD;
		n--;
		while(n)
		{
			if(n&1==1)ans=(ans*bs)%MOD;
			bs=(bs*bs)%MOD;
			n>>=1;
		}
		cout<<ans%MOD;
	}
    return 0;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<int, int> P;
// typedef modint1000000007 mint;

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/*--------------------------------------------------*/
const int INF = 1e9;
// const ll INF = 1e18;
const int MAX = 1e5;

ll power_mod(ll n, ll p, ll mod) {
    if (p == 0) return 1;
    if (p % 2 == 0) {
        ll t = power_mod(n, p / 2, mod);
        return t * t % mod;
    }
    return n * power_mod(n, p - 1, mod) % mod;
}

const ll mod = 1000000007;

void solve() {
    ll n, p;
    cin >> n >> p;
    ll ans = p - 1;
    ans = ans * power_mod(p - 2, n - 1, mod) % mod;
    cout << ans << "\n";
}

int main() {
    speedUpIO();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        // cout << solve() << "\n";
        // cout << fixed << setprecision(15) << solve() << "\n";
    }
    return 0;
}
