#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
static const ll mod=1000000007;


int pow_mod(long long n,long long m,long long p){
  if(p==0) return 1;
  else if(p%2==1){
    return pow_mod(n,m,p-1)*n%m;
  }
  else{
    long long t=pow_mod(n,m,p/2);
    return t*t%m;
  }
}

signed main() {
  ll na,pa;
  cin>>na>>pa;
  ll ans=pa-1;
  ans*=pow_mod(pa-2,mod,na-1);
  ans%=mod;
  cout<<ans<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define DONTSYNC ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL) //dont use stdio with iostream functions //input and output are out of order now!
#define TEST unsigned long long T; cin>>T; while(T--)  //loop over each testcase
#define endl "\n"
#define fori(a,start,end) for(int a=start;a<end;a++)
#define forll(a,start,end) for(long long a=start;a<end;a++)
#define forull(a,start,end) for(unsigned long long a=start;a<end;a++)
#define fi first
#define se second
typedef long long     ll;
typedef long double   ld;
typedef pair<int,int> pii;
typedef pair<ll,ll>   pll;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vii;
typedef vector<pll>   vll;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
const double PI = acos(-1);
long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
ll mod=1e9+7;
void solve(){
    /* code */
    ll n,p; cin>>n>>p;
    cout<< (p-1)*binpow(p-2,n-1,mod)%mod;
}

int main()
{
    DONTSYNC;
    // TEST
    solve();
    return 0;
}
