N, M = map(int, input().split()) def mcomb(n, k): a = 1 b = 1 for i in range(k): a *= n - i a %= 1000000007 b *= i + 1 b %= 1000000007 return a * pow(b, 1000000005, 1000000007) % 1000000007 result = pow(M, N, 1000000007) for i in range(1, M): result -= pow(i, N, 1000000007) * mcomb(N, i) result %= 1000000007 print(result)