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

long long int MOD = 1000000007;

long long int modpow(long long int a, long long int n) {
  long long int res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

int main() {
  long long int N, P;
  cin >> N >> P;
  long long int ans = (P - 1) * modpow(P - 2, N - 1) % MOD;
  if (ans < 0) ans += MOD;
  cout << ans << endl;
}
#include<iostream>
#include<iterator>
#include<algorithm>
#include<climits>
#include<map>
#include<set>
#include<numeric>
#include<string>
#include<sstream>
#include<cstdio>
#define ll long long
using namespace std;




int main()
{
    int i, j;
    cin >> i >> j;
    cout << (abs(i-j)<3?"Yes":"No");
}