1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <algorithm>

const long long MODULO_NUM = 1000000007;
int n;
long long m;

long long modulo_power(long long base, int exponent)
{
	if (exponent == 0)
		return 1ll;
	else if (exponent == 1)
		return base % MODULO_NUM;
	else
	{
		long long result = (modulo_power(base, exponent / 2) % MODULO_NUM) * (modulo_power(base, exponent / 2) % MODULO_NUM) % MODULO_NUM;
		if (exponent % 2 == 1)
		{
			result = (result * (base % MODULO_NUM)) % MODULO_NUM;
		}
		return result;
	}
}

std::vector<std::vector<int>> memotable;

long long dp(int current_position, long long how_many_intervals_were_before)
{
	int result = 0;
	if (current_position == n)
		return 1;
	if (how_many_intervals_were_before == m)
		return 0;
	if (memotable[current_position][how_many_intervals_were_before] != -1)
		return memotable[current_position][how_many_intervals_were_before];
	for (int i = 1; i < n - current_position; i++)
	{
		result += (modulo_power(m - how_many_intervals_were_before, i) % MODULO_NUM) * (dp(current_position + i + 1, how_many_intervals_were_before + 1) % MODULO_NUM) % MODULO_NUM;
		result %= MODULO_NUM;
	}
	memotable[current_position][how_many_intervals_were_before] = result;
	return result;
}

int main()
{
	scanf("%d %lld", &n, &m);
	memotable.resize(n);
	for (int i = 0; i < n; i++)
	{
		memotable[i].assign(n, -1);
	}
	int result = dp(0, 0);
	printf("%d\n", result);
}