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

#define ll long long
#define tej ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

#define ve vector<int>
#define vl vector<long long int>
#define vp vector<pair<long long int,long long int>>
#define pb push_back
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define bp pop_back
#define fp pop_front
#define mp make_pair

#define fo(i,n) for(i=0;i<n;i++)
#define unq(v)      unique(v.begin(), v.end())-v.begin()
#define fnd(v,a)    find(v.begin(), v.end(), a)
#define uprb(v,a)   upper_bound(v.begin(), v.end(), a)
#define upra(v,n,a) upper_bound(v, v+n, a)
#define lowb(v,a)   lower_bound(v.begin(), v.end(), a)
#define lowa(v,n,a) lower_bound(v, v+n, a)
#define newuv(v)    v.erase(unique(v.begin(),v.end()),v.end())
#define rev(v)      reverse(v.begin(),v.end())
#define rndshf(v)   random_shuffle(v.begin(),v.end())
#define all(v)      v.begin(),v.end()

ll po(ll a,ll b,ll m)
{
ll t=1;
while(b>0)
{
    if(b%2!=0)
    {
        t=(t*a)%m;
    }
    a=(a*a)%m;
    b=b/2;
}
return t;
}
// ll inv(ll a,ll m)
// {
//   return po(a,m-2,m);
// }
// ll vis[100002]={0},x=0;
// vl v[100002];
// ll dfs(ll s)
// {
//   ll c=1,i;
//   for(i=0;i<v[s].size();i++)
//   {
//     if(vis[v[s][i]]==0)
//     {
//       vis[v[s][i]]=1;
//       c+=dfs(v[s][i]);
//     }
//   }
//   x+=c-1;
//   //cout<<x<<endl;
//   return c;
// }

void solve()
{
  ll n,p,s=1,x,y;
  cin>>n>>p;
  if(p==2)
  {
    if(n==1)
      cout<<"1";
    else
      cout<<"0";
    return;
  }
  cout<<((p-1)*po(p-2,n-1,1000000007))%1000000007;
}
int main()
{
    tej
    ll t=1;
    //cin>>t;
    while(t--)
    solve();
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;

const int m = 1e9+7;
ll n,p;
ll f(ll n){
  if(n==0) return 1;
  ll a = f(n>>1);
  a = a*a%m;
  if(n&1) a = a*(p-2)%m;
  return a;
}
int main() {
  cin >> n >> p;
  ll ans = f(n-1);
  ans = ans*(p-1)%m;
  cout << ans << endl;
}