#define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<climits>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<list>
#include<functional>


using namespace std;

#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#define LP(I,S,G) for (long long int I = S; I < G; I++)
#define IN(X) 	for (int i = 0; i < X.size(); i++)cin >> X[i]
#define OUT(X) 	for (int i = 0; i < X.size(); i++)out << X[i]
#define SORT(X) sort(X.begin(), X.end())
#define CSORT(X,Y) sort(X.begin(), X.end(),Y)
#define COPY(X,Y) copy(X.begin(), X.end(), Y.begin())
#define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++)

template<class I1, class I2>
istream& operator>>(istream& s, pair<I1, I2>& in) {
	s >> in.first >> in.second;
	return s;
}

long long int M = pow(10, 9) + 7;


bool comp(pair<int, int>& a, pair<int, int>& b) {
	return (a.second < b.second);
}

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(void) {
	long long int n, p;
	cin >> n>>p;
	if (p == 2) {
		cout << (n==1);
		return 0;
	}
	long long int ans = p - 1;
	ans = (ans*modpow(p - 2, n - 1, M)) % M;
	cout << ans;
	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <climits>
#include <set>
#include <cstring>
#include <unordered_map>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <stdlib.h>
#include <chrono>
 
///////////////////////////////////////////////////////////////////////////////////////////////////
#define fio  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll  long long int
#define ull unsigned long long int
#define cinll(x) ll x;cin >> x;
#define cini(x) int x;cin >> x;
#define cins(x) string x;cin >> x;
#define vect(x) vector<ll> x
#define vect1(x) vector<ll> x;x.push_back(0);
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
///////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX 9223372036854775807
#define MIN -9223372036854775800
#define MOD 1000000007
#define f first
#define s second
///////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
using u64 = uint64_t;
//Safe_hashing for minimising collisions 
//https://codeforces.com/blog/entry/62393
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};


ll fastExponentiation(ll base,ll exp,ll mod){
    if(exp==0)  return 1;
    else if(exp==1) return base%mod;
    else{
        ll r = fastExponentiation(base,exp/2,mod);
        if(exp%2==0){
            return ( (r%mod) * (r%mod) )%mod;
        }else{
            return ( ( ( (r%mod)*(r%mod) )%mod ) * (base%mod)  )%mod;
        }
    }
}


void solve() {
    cinll(n);cinll(p);
    if(n==1 && p==2) {
        cout<<1;
        return;
    }
    if(p==3) {
        cout<<2;
        return;
    }
    cout<< (((p-1) % MOD) * (fastExponentiation(p-2,n-1,MOD) % MOD))%MOD;
}

int main(){
    fio;
///////////////////////////////////////////    
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
///////////////////////////////////////////
    // cinll(t);
    // while(t--) {
        solve();
    // }
  return 0;
}