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

template <int64_t mod>
class modint {
    int64_t x;

public:
    modint(int64_t x = 0) : x((x % mod + mod) % mod) {}
    modint operator-() const { return modint(-x); }
    modint& operator+=(const modint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator-=(const modint& a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator*=(const modint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    modint operator+(const modint& a) const {
        return modint(*this) += a;
    }
    modint operator-(const modint& a) const {
        return modint(*this) -= a;
    }
    modint operator*(const modint& a) const {
        return modint(*this) *= a;
    }
    modint pow(int64_t t) const {
        if (not t) return 1;
        modint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    // for prime mod
    modint inv() const { return pow(mod - 2); }
    modint& operator/=(const modint& a) {
        return (*this) *= a.inv();
    }
    modint operator/(const modint& a) const {
        return modint(*this) /= a;
    }
    friend ostream& operator<<(ostream& os,
                               const modint& m) {
        os << m.x;
        return os;
    }
};

const int64_t mod = 1e9 + 7;
using mint = modint<mod>;

int main() {
    int64_t n, p;
    cin >> n >> p;
    mint ans = mint(p - 1) * mint(p - 2).pow(n - 1);
    cout << ans << endl;
    return 0;
}

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author NikuKyabe
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001l
#define MOD 1000000007
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

/**
 * x^a mod MODを求める O(log a)
 * @return x^a mod MOD
 */
int mod_pow(int x, int a) {
    int res = 1;
    while(a > 0){
        if(a & 1) res = res * x % MOD;
        x = x * x % MOD;
        a >>= 1;
    }
    return res;
}

/**
 * xのmod Mでの逆元を求める O(log MOD)
 * mod MODの中でのa / b はa * mod_inv(b)をする
 * @return xの逆元(mod MOD)
 */
int mod_inv(int x){
    return mod_pow(x, MOD - 2);
}

class DNowhereP {
public:
	void solve(std::istream& cin, std::ostream& cout) {
        int n, p;
        cin >> n >> p;
        int res = mod_pow(p-2, n-1);
        res *= ((p-1) % MOD);
        res %= MOD;
        cout << res << endl;
	}
};

signed main() {
	DNowhereP solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
