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

#pragma GCC optimize(-O3)
#pragma GCC optimize(Ofast)
#pragma GCC optimize("unroll-loops")

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << (x))
#define sz(x) (int)x.size()
#define fout(x) {cout << x << "\n"; return; }

using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = 1e5;
const int M = pw(16);
const long long mod = 1e9 + 7;
const int N = 2e5;
const int inf = 1e9;

template <typename T> void vout(T s){cout << s << endl;exit(0);}

ll dp[3100][3100];
ll r[3100];

void u(ll &a, ll b){
    a += b;
    if(a >= mod) a -= mod;
}

ll mul(ll a, ll b){
    return (a * b) % mod;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    ll n, m;
    cin >> n >> m;

    if(n == 1) vout(0);

    dp[1][1] = m;

    for(int i = 2; i < n; i++){
        for(int j = 1; j < i; j++){
            u(dp[i][j], mul(dp[i - 1][j], m - j));
            u(dp[i][j + 1], mul(r[j], m - j));
            r[j] = mul(r[j], j);
            u(r[j], mul(dp[i - 1][j], j));            
        }
    }

    ll ans = 0;

    for(int j = 0; j <= n; j++){
        u(ans, mul(dp[n - 1][j], j));
        u(ans, mul(r[j], j));
    }        

    cout << ans << "\n";

    return 0;
}