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
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007

vector <int> ciag;
int n, m;
int el;
long long res = 0;

bool Check() {
	vector <bool> wyk(el + 1);
	bool xd = true;
	for (int i = 0; i < n - 1; i++) {
		if (wyk[ciag[i]])
			xd = true;
		else if (xd) {
			wyk[ciag[i]] = true;
			xd = false;
		}
	}
	if (wyk[ciag[n - 1]]) {
		return true;
	}
	else
		return false;
}

void BackTrack(int maxx) {
	if ((int)ciag.size() == n) {
		if (Check()) {
			long long temp = 1;
			for (int i = m; i > m - maxx; i--) {
				temp *= i;
				temp %= mod;
			}
			res += temp;
			res %= mod;
		}
		return;
	}
	for (int i = 1; i <= min(maxx + 1, el); i++) {
		ciag.emplace_back(i);
		BackTrack(max(maxx, i));
		ciag.pop_back();
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	el = min(n, m);
	BackTrack(0);
	cout << res << "\n";
}