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
#include <iostream>
#include <vector>
#define ll long long
#define pb push_back
using namespace std;

const int MAXW = 3000;
const int mod = 1e9 + 7;


int n, m;
ll d;
ll dp[MAXW][MAXW/2];
ll w;
/*
 d = x - 2
 if d >= 1:
    dp[x][1] = m * (((m-1)^(d)) + (d * (m-1)^(d-1)))
 else dp[x][1] = m
 dp[n][k] - n to liczba miejsc, k to liczba par
 dp[n][k] = for i = 1 to i = n:
     += dp[i][k-1] + dp[n-i][k-1]

 */
ll pot(int a, int b){
    if (b == 0) return 1;
    if (b % 2 == 0){
        ll c = pot(a, b/2);
        c %= mod;
        return (c*c) % mod;
    }
    else{
        ll c = pot(a, b/2);
        c %= mod;
        return (((c*c) % mod) * a) % mod;
    }
    
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    dp[2][1] = m;
    for (int i = 3; i <= n; i++){
        d = i - 2;
        // rozpatrujemy osobno jesli istnieje takie samo i jesli nei isntieje
        // pot(m-1, d) + d * pot(m-1, d-1)
        dp[i][1] = pot(m-1, d) + d * pot(m-1, d-1);
        dp[i][1] %= mod;
        dp[i][1] *= m;
        
    }
    for (int i = 2; i <= n; i++){
        for (int j = 2; j <= n/2; j++){
//            cout << i << " " << j << endl;
            ll x = 0;
            /*
             dp[n][k] = for i = 1 to i = n:
                 += dp[i][k-1] + dp[n-i][k-1]
             */
            for (int k = 2; k < i - 1; k++){
                ll a = dp[k][1];
                ll b = dp[n-k][j-1];
                x += a * b;
                x %= mod;
            }
            dp[i][j] = x;
        }
    }
//    cout << dp[4][1] << " " << dp[4][2] << " | " << dp[2][1] << endl;
    for (int i = 1; i <= n/2; i++){
//        cout << dp[n][i] << endl;
        w += dp[n][i];
        w %= mod;
    }
    cout << w;
}