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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native")
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define For(i,s,a) for(int i = (int)s; i < (int)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define sz size()
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
#define popcnt(x) __builtin_popcount(x)

//using namespace std::chrono;
//#define time_since duration_cast<nanoseconds>(system_clock::now().time_since_epoch())

template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const lld mod = 1e9 + 7;
const lld N = 3001;

lld pot(lld a, lld b) {
	return !b ? 1 : (pot((a * a) % mod, b >> 1) * (b & 1 ? a : 1)) % mod;
}

lld pom[4][N];

lld f(lld len, lld m, lld p, lld k) {
	For(i, 0, len + 1)
	For(j, 0, 4)
		pom[j][i] = 0;
	bool debugall = 0;
	if(debugall) 
		puts("start");
	For(j, 0, 4) {
		pom[j][1] = (j == 0) ? m - p : (j == 1 ? p - k : j == 2 ? k - 1 : 1);
		if(debugall)
			cout << pom[j][1] << " ";
	}
	if(debugall)
		puts("");	
	For(i, 2, len + 1) {
		For(j, 0, 4)
			pom[0][i] += (pom[j][i - 1]) % mod;
		pom[0][i] %= mod;
		pom[3][i] = (pom[0][i] - pom[1][i - 1] + (k != p ? ((pom[1][i - 1] * pot(p - k, mod - 2)) % mod) : 0) + mod) % mod;
		pom[1][i] = (pom[0][i] * (p - k)) % mod;
		(pom[0][i] *= (m - p)) %= mod;
		pom[2][i] = ((k - 1) * ((pom[0][i - 1] + pom[2][i - 1]) % mod)) % mod;
		if(debugall) {
			For(j, 0, 4)
				cout << pom[j][i] << " ";
			puts("");
		}
	} 
	lld ret = 0;
	For(i, 0, 4)
		ret += pom[i][len];
	ret = max(ret, 1ll);
	if(debugall)
		printf("%lld end\n", ret);
	return ret % mod;
}

#define smalln 601
lld dp[11][smalln][smalln];

int ile[201][201][201];
int c[smalln];

bool dfs(int x, int tar) {
	if(x > tar)	
		return 1;
	for(int i = tar; i > x; --i)
		if(c[x] == c[i] && dfs(i + 1, tar))
			return 1;
	return 0;
}

bool check(int n) {
	return dfs(0, n - 1);
}

int32_t main() {
	lld n, m;
	scanf("%lld%lld", &n, &m);
	if(m < 4) {
		lld wyn = 0;
		For(p, 1, min(m, n / 2) + 1) {
			memset(dp, 0, 11 * smalln * smalln * sizeof(lld));
			dp[0][0][0] = 1;
			For(k, 1, p + 1)
			For(i, 2 * k, n - (p - k) * 2 + 1) {
				For(l, 2, i + 1 - 2 * (k - 1)) {// da się polepszyć tutaj 
					lld xd = 0;
					For(j, 0, i - l + 1)
						(xd += dp[k - 1][i - l][j]) %= mod;
					(dp[k][i][l] += ((f(l - 2, m, p, k) * (m - k + 1)) % mod) * xd) %= mod;
		//			cout << dp[k][i][l] << "   " << p << " " << k << " " << i << " " << l << "   " << xd << " " << f(l - 2, m, p, k) << endl;
					if(p == k && i == n)
						(wyn += dp[k][i][l]) %= mod;
				}
			}
		}
		printf("%lld\n", wyn);
	} else {
		lld pot = 1;
		lld k = m;
		For(i, 0, n)
			pot *= k;
		int wyn = 0;
		For(mask, 0, pot) {
			//if(mask != 9663900)
			//	continue;
			int iter = k;
			For(i, 0, n) {
				//
				//cout << iter << " " << i << " " << pot << " " << mask % iter << endl;
				c[i] = (mask % iter) / (iter / k);
				iter *= k;
			}
			/*For(i, 0, n)
				cout << c[i] << " ";
			puts("");*/
			wyn += check(n);
			/*if(wyn != wynn) {
				upd(n);
			}*/
		}
		cout << wyn<<endl;
	}
}