#pragma comment(linker, "/STACK:128777216")
#pragma GCC optimize("fast-maths")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <bitset>
#include <stack>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <climits>
#include <ctime>
#include <random>
#include <complex>
#include <functional>
#ifdef ORSET
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree<T, null_type, std::less<T>,
    rb_tree_tag, tree_order_statistics_node_update>;
#endif
using namespace std;

#define forx(_name, _from, _to, _value) for (int _name = _from; _name < _to; _name += _value)
#define rforx(_name, _from, _to, _value) for (int _name = _from; _name > _to; _name -= _value)
#define all(_STL_NAME) _STL_NAME.begin(), _STL_NAME.end()
#define rall(_STL_NAME) _STL_NAME.rbegin(), _STL_NAME.rend()
#define mp(_FIRST,_SECOND) make_pair(_FIRST, _SECOND)

typedef long long ll;
typedef unsigned long long llu;
typedef long double ld;
const ld eps = 1e-7;
mt19937_64 rndm;

void start() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.precision(8); cout.setf(ios::fixed);
    string FILENAME = "\-__-/";
    rndm.seed(time(0));
#ifdef _DEBUG
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#else
    //freopen((FILENAME + ".in").c_str(), "rt", stdin);
    //freopen((FILENAME + ".out").c_str(), "wt", stdout);
#endif
}
#ifdef NEEDFFAST
inline int FastIn() {
    int val = 0;
    char c = ' ';
    while (!isdigit(c))
        c = _getchar_nolock();
    while (isdigit(c)) {
        val *= 10;
        val += c - '0';
        c = _getchar_nolock();
    }
    return val;
}

inline void FastOut(int val) {
    bool fst = true;
    const int degree = 1000000000;
    //const ll degree = 10000000000000000ll;
    for (int i = degree; i > 0; i /= 10) {
        int dl = val / i;
        if (dl || !fst) {
            _putchar_nolock('0' + dl);
            fst = false;
        }
        val %= i;
    }
    if (fst)
        _putchar_nolock('0');
}
#endif

const ll mod = 1000000007;
ll binpow(ll val, ll deg) {
    ll res = 1;
    ll dg = val;
    while (deg) {
        if (deg & 1) res = (res * dg) % mod;
        deg >>= 1;
        dg = (dg * dg) % mod;
    }
    return res;
}

int main() {
    start();
    /* (P - 1) * (P - 2) ^ (N - 1) */
    ll n, p;
    cin >> n >> p;
    cout << (p - 1) * binpow(p - 2, n - 1) % mod;
#ifdef _DEBUG
    cout << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
    return 0;
}
#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <cmath>
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double EPS = 1e-10;
constexpr ll mod = 1000000007;
// constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const char eol = '\n';
// --------------------------------------------------------------------------

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,p;
    cin >> n >> p;
    ll ans = p-1;
    n--;
    ll sum = p-2;
    for(ll k=0; k<40; k++){
        if(n & (1LL<<k)){
            ans *= sum;
            ans %= mod;
        }
        sum = sum*sum;
        sum %= mod;
    }
    cout << ans << endl;
    return 0;
}