#include<iostream>
using namespace std;
template<long long Mod>struct modInt
{
	long long x;
	constexpr modInt()noexcept:x(){}
	template<typename T>
	constexpr modInt(T v=0)noexcept:x(v%Mod){if(x<0)x+=Mod;}
	constexpr long long getval()const noexcept{return x;}
	constexpr modInt operator-()const noexcept{return x?Mod-x:0;}
	constexpr modInt operator+(const modInt&r)const noexcept{return modInt(*this)+=r;}
	constexpr modInt operator-(const modInt&r)const noexcept{return modInt(*this)-=r;}
	constexpr modInt operator*(const modInt&r)const noexcept{return modInt(*this)*=r;}
	constexpr modInt operator/(const modInt&r)const noexcept{return modInt(*this)/=r;}
	constexpr modInt&operator+=(const modInt&r)noexcept{x+=r.x;if(x>=Mod)x-=Mod;return*this;}
	constexpr modInt&operator-=(const modInt&r)noexcept{x-=r.x;if(x<0)x+=Mod;return*this;}
	constexpr modInt&operator*=(const modInt&r)noexcept{x=x*r.x%Mod;return*this;}
	constexpr modInt&operator/=(const modInt&r)noexcept{x=x*r.inv();return*this;}
	constexpr modInt powm(long long n)noexcept
	{
		if(n<0)return powm(-n).inv();
		modInt x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modInt inv()const noexcept
	{
		long long a=x,b=Mod,u=1,v=0;
		while(b)
		{
			long long t=a/b;
			a-=t*b;swap(a,b);
			u-=t*v;swap(u,v);
		}
		return modInt(u);
	}
	constexpr bool operator==(const modInt&r){return this->x==r.x;}
	constexpr bool operator!=(const modInt&r){return this->x!=r.x;}
	friend ostream&operator<<(ostream&os,const modInt<Mod>&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modInt<Mod>&a){long long v;is>>v;a=modInt<Mod>(v);return is;} 
};

const long long mod=1000000007;
using mint=modInt<mod>;

main()
{
	int n;mint p;cin>>n>>p;
	cout<<(p-1)*(p-2).powm(n-1)<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define PI 3.14159265
#define double long double
#define DB
#ifdef DB
#define el cerr << "\n";
#define db(...) cerr << " [" << #__VA_ARGS__ << " : " << __VA_ARGS__ << "] ";
#else
#define el
#define db(...)
#endif // DB
#define ll long long

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int L, R;
    cin >> L >> R;
    vector<ll> dp(R + 1);
    ll res = 0;
    int x;
    for (int i = R; i >= 2; --i) {
        // db(i);
        x = (R / i) - ((L - 1) / i);
        dp[i] = 1LL * x * x;
        // db(dp[i]);
        for (int j = i + i; j <= R; j += i) {
            dp[i] -= dp[j];
            // db(j) db(dp[j]) db(dp[i]);
        }
        res += dp[i];
        // db(i) db(dp[i]) db(x) db(res) el;
    }
    for (int i = max(2, L); i <= R; ++i) {
        x = (R / i) - ((L - 1) / i);
        res -= x * 2 - 1;
    }
    cout << res;
    return 0;
}
