#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
#include <iomanip>
#include <time.h>
using namespace std;

int64_t mod = 998244353 ;

int64_t min(int64_t a,int64_t b) {
    if (a > b)
    {
        return b ;
    }else
    {
        return a ;
    }
}


vector<int> getprime(int n){
    vector<int> p(0) ;
    vector<bool> table(n+1,0) ;
    for (int i = 2; i <= n; i++)
    {
        if (table.at(i) == 0)
        {
            p.push_back(i) ;
            for (size_t j = 2; j < n/i ; j++)
            {
                table.at(i*j) = 1 ;
            }
            
        }
        
    }
    return p ;
}

int getnum(vector<int> &array, vector<char> &c, char a){
    for (int i = 0; i < c.size(); i++)
    {
        if (a == c.at(i))
        {
            return array.at(i) ;
        }
        
    }
    
}

int64_t repeatmod(int64_t n, int64_t pow, int64_t p){
    if (pow == 1)
    {
        return n%p ;
    }
    
    if (pow % 2)
    {
        return (repeatmod(n, pow-1,p) * n) % p ;
    }else
    {
        return (repeatmod(n,pow/2,p) * repeatmod(n,pow/2,p)) % p ;
    }
    
    
}

int main(){
    int64_t n,p ;
    cin >> n >> p ;
    if (n == 1)
    {
        cout << p-1 << endl ;
        return 0 ;
    }
    
    int64_t mod = 1000000007 ;
    int64_t ans = ( (p-1) * repeatmod(p-2,n-1,mod) ) % mod ;
    cout << ans << endl ;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MOD = (ll)1e9 + 7;
ll n, p;

ll pow_(ll high)
{
    if (high==0)return 1;
    ll val = pow_(high / 2);
    if (high % 2==0)return val * val % MOD;
    return val * val % MOD * (p-2)% MOD;
}

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> p;
    ll val = (p-1)*pow_(n-1)%MOD;
    cout << val;

    return 0;
}
