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

constexpr long long mod = 1e9 + 7;
constexpr int MAXN = 3e3 + 7;

int n;
long long m;

long long dp[MAXN][MAXN];
long long dps[MAXN][MAXN];
long long dpss[MAXN][MAXN];

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;

    dp[1][1] = m;
    
    for (int i = 1; i <= n; i++)
    {
        for (int k = 1; k * 2 <= n + 4; k++){
            if (i > 1 || k > 1)
                dp[i][k] = (dpss[i - 1][k - 1] * (m - k + 1)) % mod;
            dps[i][k] = (dps[i - 1][k] * (m - k) + dp[i][k]) % mod;
            dpss[i][k] = ((dpss[i - 1][k] + dps[i - 1][k]) * (long long)k) % mod;
        }
    }

    long long res = 0;
    for (long long k = 1; k < n; k++)
        res = (res + k * (dps[n - 1][k] + dpss[n - 1][k])) % mod;
    
    cout << res << "\n";
    return 0;
}