#include using namespace std; using i64 = long long; #define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++) #define all(x) x.begin(),x.end() long long inv_mod(long long a, long long m) { long long b, x, u, q, abs_m, tmp; abs_m = (m < 0) ? -m : m; b = m; x = 1; u = 0; while (b > 0) { q = a / b; tmp = u; u = x - q * u; x = tmp; tmp = b; b = a - q * b; a = tmp; } return (x < 0) ? abs_m + x : x; } template struct ModInt{ long long value; ModInt(long long n = 0) { if(n >= 0) value = n % MOD; else value = (MOD + (n % MOD)); } operator long long() const noexcept {return value % MOD;} ModInt operator++(int){ ModInt before = *this; value = (value + 1) % MOD; return before; } ModInt operator--(int){ ModInt before = *this; value = (value - 1 + MOD) % MOD; return before; } ModInt& operator++(){ value = (value + 1) % MOD; return *this; } ModInt& operator--(){ value = (value - 1 + MOD) % MOD; return *this; } bool operator!() const noexcept{ return !static_cast(value); } ModInt operator+() const{ return value; } ModInt operator-() const{ return (-value + MOD) % MOD; } ModInt& operator*=(const ModInt& m){ return *this = ModInt((this->value * m.value) % MOD); } ModInt& operator+=(const ModInt& m){ return *this = ModInt((this->value + m.value) % MOD); } ModInt& operator-=(const ModInt& m){ return *this = ModInt((this->value - m.value + MOD) % MOD); } ModInt& operator/=(const ModInt& m){ return *this = ModInt((this->value * inv_mod(m.value,MOD)) % MOD); } }; template ModInt operator*(const ModInt& m1,const ModInt& m2){return ModInt(m1) *= m2;} template ModInt operator+(const ModInt& m1,const ModInt& m2){return ModInt(m1) += m2;} template ModInt operator-(const ModInt& m1,const ModInt& m2){return ModInt(m1) -= m2;} template ModInt operator/(const ModInt& m1,const ModInt& m2){return ModInt(m1) /= m2;} template ModInt operator*(const long long& m1,const ModInt& m2){return ModInt(m1) *= m2;} template ModInt operator+(const long long& m1,const ModInt& m2){return ModInt(m1) += m2;} template ModInt operator-(const long long& m1,const ModInt& m2){return ModInt(m1) -= m2;} template ModInt operator/(const long long& m1,const ModInt& m2){return ModInt(m1) /= m2;} template struct Fact_Node{ ModInt fact; ModInt inv_fact; ModInt operator+() const{ return fact; } ModInt operator-() const{ return -fact; } Fact_Node& operator*=(const ModInt& m){ fact *= m; inv_fact *= m; return *this; } Fact_Node& operator/=(const ModInt& m){ fact /= m; inv_fact /= m; return *this; } Fact_Node& operator*=(const Fact_Node& f){ fact *= f.fact; inv_fact *= f.inv_fact; return *this; } Fact_Node& operator/=(const Fact_Node& f){ fact *= f.inv_fact; inv_fact *= f.fact; return *this; } }; template Fact_Node operator*(const Fact_Node& m1,const ModInt& m2){return Fact_Node(m1) *= m2;} template Fact_Node operator/(const Fact_Node& m1,const ModInt& m2){return Fact_Node(m1) /= m2;} template Fact_Node operator*(const ModInt& m1,const Fact_Node& m2){return Fact_Node(m2) *= m1;} template Fact_Node operator/(const ModInt& m1,const Fact_Node& m2){return {m1 * m2.inv_fact , m2.fact / m1};} template Fact_Node operator*(const Fact_Node& m1,const Fact_Node& m2){return Fact_Node(m1) *= m2;} template Fact_Node operator/(const Fact_Node& m1,const Fact_Node& m2){return Fact_Node(m1) /= m2;} #include #include using namespace std; template struct Facts{ vector> facts; vector> inv_facts; using Node = Fact_Node; Facts(int N) : facts(N,1) , inv_facts(N){ for(int i = 1;i < N;i++){ facts[i] = facts[i - 1] * i; } inv_facts[N - 1] = 1LL / facts[N - 1]; for(int i = N - 1;i >= 1;i--){ inv_facts[i - 1] = inv_facts[i] * i; } } Node operator[](int n){ return (Fact_Node){facts[n],inv_facts[n]}; } Node nCr(int n,int r){ return (*this)[n] / (*this)[r] / (*this)[n - r]; } Node nHr(int n,int r){ return this->nCr(n + r - 1,r); } }; template ModInt mod_pow(ModInt a, i64 b){ ModInt ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b >>= 1; } return ans; } int main(){ i64 N,M; cin >> N >> M; const i64 MMM = 1e9 + 7; Facts fact(M + 100); using Int = ModInt; Int ans = mod_pow(Int(M),N); for(int i = 1;i <= M;i++){ i64 re = (fact.nCr(M,i).fact * mod_pow(Int(M - i) , N)); if(i & 1) ans -= re; else ans += re; } cout << ans << endl; }