import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) def comb(n, k): return fac[n] * pow(fac[k], mod - 2, mod) * pow(fac[n - k], mod - 2, mod) n, m = map(int, readline().split()) mod = 10 ** 9 + 7 fac = [1] * (n + 1) ans = 0 for i in range(2, n + 1): fac[i] = fac[i - 1] * i fac[i] %= mod for i in range(m + 1): v = 1 if i % 2 == 1: v = -1 ans += v * comb(m, i) * pow(m - i, n, mod) ans %= mod print(ans)