#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;


    const static long long MOD = 1000000007;


    long long norm(long long a) {
        a %= MOD;
        if (a < 0) {
            a += MOD;
        }
        return a;
    }

    long long inverse(long long a) {
        long long b = MOD;
        long long u = 0, v = 1;
        a = norm(a);
        while (a != 0) {
            long long d = b / a;
            b -= d * a;
            u -= d * v;
            swap(a, b);
            swap(u, v);
        }
        return norm(u);
    }

    long long powM(long long x, long long e) {
        long long res = 1;
        while (e > 0) {
            if (e & 1) {
                res = res * x % MOD;
            }
            x = x * x % MOD;
            e >>= 1;
        }
        return res;
    }

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);

    int n, m;
    cin >> n >> m;
    --m;
    long long res = m * 1LL * powM(m - 1, n - 1) % MOD;
    cout << res << '\n';

    return 0;
}
#include <bits/stdc++.h>
#define pb push_back
#define sz size()

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <long long> vll;

const int MOD = 1e9 + 7;
const int maxn = 1e6 + 7;


ll inv[maxn+3];
ll fac[maxn+3];
ll qpow(ll x,ll n)
{
    ll res = 1;
    while(n > 0)
    {
        if(n & 1) res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}
inline ll A(ll n, ll m){
    return fac[n] * inv[n - m] % MOD;
}
inline ll C(ll n, ll m){
    return A(n, m) * inv[m] % MOD;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    fac[0] = fac[1] = 1; 
    for(ll i = 1; i < maxn; i++){
        fac[i] = fac[i - 1] * i % MOD;
    }
    inv[0] = 1;
    inv[1] = 1;
    for(ll i = 2; i < maxn; i++){
        inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
    }
    for(ll i = 2; i < maxn; i++){
        inv[i] = inv[i] * inv[i - 1] % MOD;
    }
    ll n, p;
    cin >> n >> p;;
    cin >> n >> p;
    ll ans = (p - 1) * qpow(p - 2, n - 1) % MOD;
    ans %= MOD;
    cout << ans;
    return 0;
}