//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp> 
//using namespace __gnu_pbds; 
#define int long long 
#define ll int
#define all(a) a.begin(),a.end()    
#define rev(a) a.rbegin(),a.rend()    
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);

int n,p;
int ans=0;
void rec(vector<int> a)
{
    if(a.size()==n)
    {
        bool ok=true;
        int sum=0;
        fi(0,n)
        {
            sum+=a[i];
            if(sum%p==0) ok=false;
        }
        if(ok)
        {
            ans++;
            for(auto x:a)
            {
                cout<<x<<' ';
            }
            cout<<'\n';
        }
        return ;        
    }
    fi(1,p)
    {
        a.pb(i);
        rec(a);
        a.pop_back();
    }
}

const int mod=1e9+7;
const int mxn=1e6+3;

ll binpow(ll a,ll b)
{
    ll res=1; 
    while(b)
    {
        if(b&1){
            res=((res%mod)*(a%mod))%mod;
        }
        a=((a%mod)*(a%mod))%mod;
        b=b>>1;
    }
    return res%mod;
}

void solve()
{
    cin>>n>>p;
    // rec({});
    // cout<<ans<<'\n';
    cout<<((p-1)*binpow((p-2),n-1))%mod<<'\n';


    
    
}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;    
    while(tt--)
    {
        solve();
    }    
}










//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1).
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

long long pow(long long x, long long n, long long MM) {
    long long ret = 1;
    while (n > 0) {
        if (n & 1) ret = ret * x % MM;  // n の最下位bitが 1 ならば x^(2^i) をかける
        x = x * x % MM;
        n >>= 1;  // n を1bit 左にずらす
    }
    return ret;
}

int main(){
  ll N,P;
  cin>>N>>P;
  cout << (P-1)*pow(P-2,N-1,MOD)%MOD << endl;
  return 0;
  
}