import std; int readInt() { int x; readf!" %d"(x); return x; } int[] readArray(int n) { auto a = new int[](n); return a.map!(i => readInt).array; } immutable long P = (1e9 + 7).to!long; struct Combinatorics(long P) { long[] inv, fac, ifac; this(int n) { inv = new long[](n + 1); fac = new long[](n + 1); ifac = new long[](n + 1); inv[1] = fac[0] = ifac[0] = 1; for(int i = 2; i <= n; i ++) inv[i] = (P - P / i) * inv[P % i] % P; for(int i = 1; i <= n; i ++) { fac[i] = i * fac[i - 1] % P; ifac[i] = inv[i] * ifac[i - 1] % P; } } long binom(int n, int m) { if(n < m || m < 0) return 0; // n >= 0 return fac[n] * ifac[m] % P * ifac[n - m] % P; } long qpow(long a, long b) { long r = 1, t = a % P; for(; b; b /= 2) { if(b & 1) r = r * t % P; t = t * t % P; } return r; } }; void main() { int n = readInt, m = readInt; auto comb = new Combinatorics!P(n); long ans = comb.qpow(m, n); foreach(i; 0 .. m) { int sgn = ((m - i) & 1) ? -1 : 1; ans += sgn * comb.qpow(i, n) * comb.binom(m, i); ans %= P; } ans %= P; if(ans < 0) ans += P; writeln(ans); }