#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<functional>
#include <random>
#include<iostream>
#include<iomanip>

using namespace std;



int main() {
	/*(P-1)*(P-2)^(N-1)を出力すればよい。*/

	long long N, P;
	cin >> N >> P;

	long long ans = P - 1;
	long long b = P - 2;
	N = N - 1;
	while (N > 0) {
		if (N % 2 == 1) {
			ans *= b;
			ans %= 1000000007;
		}
		b = b * b % 1000000007;
		N = N / 2;
	}

	cout << ans;
}
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()

long long MOD = 1000000007;

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

int main() {
    long long n,p;
    cin>>n>>p;
    
    cout<<(modpow(p-2,n-1,MOD)*(p-1))%MOD;
	return 0;
}
