#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007

ll read(){
   char C=getchar();
   ll N=0 , F=1;
   while(('0' > C || C > '9') && (C != '-')) C=getchar();
   if(C == '-') F=-1 , C=getchar();
   while('0' <= C && C <= '9') N=(N << 1)+(N << 3)+(C - 48) , C=getchar();
   return F*N;
}

ll n,p;

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

int main(){
   n = read();
   p = read();
   p -= 1;
   std::cout<<(p * power(p - 1,n - 1)) % mod<<std::endl;
}

  #include "bits/stdc++.h"
  using namespace std;
  typedef long long ll;
  typedef long double ld;
  typedef pair<int, int> ii;
  #define pb push_back
  #define T ll testx; cin>>testx; while(testx--)
  #define print(name) for(auto t:name){cout<<t<<" ";}cout<<endl;
  #define printii(name) for(auto t:name){cout<<t.first<<" "<<t.second<<endl;}
  #define mod 1000000007
  #define modx 998244353
  #define inf 2000000000000000000
  #define mem(name,val) memset(name,val,sizeof(name))
  #define f(n) for(ll i=0;i<n;i++)
  #define rep(i,a,n) for(ll i=a;i<n;i++)
  #define endl "\n"
  #define popcount(x) __builtin_popcountll(x)
  #define precise cout<<fixed<<setprecision(15)
  const long double pi = acos(-1);
  ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
  ll modexp(ll x,ll n)
  {
      if(n==0)
          {return 1;}
      else if(n%2 == 0) 
          {return modexp((x*x)%mod,n/2);}
      else                            
          {return (x*modexp((x*x)%mod,(n-1)/2))%mod;}
  }
  int main()
  {
     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
     #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
     #endif
     ll n,p;cin>>n>>p;
     ll res=modexp(p-2,n-1);
     res*=(p-1);
     res%=mod;
     cout<<res<<endl;
  }