#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007


// 2020/12/06 rechecked
long long modpow(long long n,long long r,const int mod = 1000000007){
  long long res = 1;
  while(r){
    if(r & 1)res = res*n%mod;
    n = n*n%mod;
    r >>= 1;
  }
  return res;
}

int main(){
  
  ll n,p;
  cin >> n >> p;
  cout << (p-1)*modpow(p-2,n-1)%MOD << "\n";
  

  return 0;
}
#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int M = (1e9)+7;
const int N = 400005;
const int INF = 1e12;

int mpower(int x, int y)
{
    int res = 1;
    while(y)
    {
        if(y&1) res *= x, res %= M;
        y = y/2;
        x = x*x, x %= M;
    }
    return res;
}

signed main()
{
    FAST
    int tc=1;
    // cin>>tc;
    for(int ti=1;ti<=tc;ti++)
    {
        int n,p;
        cin>>n>>p;
        int ans = (p-1)%M;
        ans *= mpower(p-2, n-1);
        ans %= M;
        cout<<ans<<endl;
    }
    return 0;
}