#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9+7;

// xのn乗
long long powmod(long long x, long long n) {
  if (n == 0) return 1;
  if (n == 1) return x % MOD;
  if (n % 2 == 1) return (x * powmod(x, n - 1)) % MOD;
  long long t = powmod(x, n / 2);
  return (t * t) % MOD;
}

int main() {
  long long  n, p;
  cin >> n >> p;
  long long re = p - 1;

  re *= powmod(p-2, n-1);
  re %= MOD;

  cout << re << endl;
}
  
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using db = double;
using ldb = long double;

#define _for(i, l, r) for (auto i = (l); i <= (r); ++i)
#define _rfor(i, r, l) for (auto i = (r); i >= (l); --i)

const int N = 5e3 + 5;
const int MOD = 1e9 + 7;

i64 qpow(i64 a, i64 b, i64 mod = MOD) {
    i64 res = 1;
    for (; b; b >>= 1, (a *= a) %= mod)
        if (b & 1) (res *= a) %= mod;
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    clock_t _CLOCK_ST = clock();
#endif
    //======================================
    i64 n, p;
    cin >> n >> p;
    cout << (p - 1) * qpow(p - 2, n - 1) % MOD;
    //=====================================
#ifndef ONLINE_JUDGE
    std::cerr
        << "\n---\n"
        << "Time used: " << clock() - _CLOCK_ST << std::endl;
#endif
    return 0;
}