#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;
}

#include<bits/stdc++.h>
#define fasterthanlight ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
#define int long long
#define vi vector<int>
#define pb push_back
#define p 1000000007
#define MAX 1e9
#define MIN -1e9
#define hi "visited\n" //sasta debugging
using namespace std;

//author::@whohet-->Het Patel
int power(long long x, unsigned int y)
{
	int res = 1;     // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	if (x == 0) return 0; // In case x is divisible by p;

	while (y > 0)
	{
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}
void solve()
{
	int n, px;
	cin >> n >> px;
	if (n == 1 and px == 2)
	{
		cout << 1 << endl;
		return;
	}
	int ans = power(px - 2, n - 1);
	ans = ((ans % p) * (px - 1) % p) % p;
	cout << ans;
}

signed main()
{
	fasterthanlight;
	int T; T = 1;
	//cin >> T;
	while (T--)
	{
		solve();
	}

	//cout << "\n\n"<<"Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
	return 0;
}