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
146
147
148
149
150
//   _|                                                _|
//   _|  _|    _|    _|    _|_|    _|_|_|      _|_|_|  _|  _|      _|_|      _|_|
//   _|_|      _|    _|  _|_|_|_|  _|    _|  _|_|      _|_|      _|_|_|_|  _|_|_|_|
//   _|  _|    _|    _|  _|        _|    _|      _|_|  _|  _|    _|        _|
//   _|    _|    _|_|_|    _|_|_|  _|_|_|    _|_|_|    _|    _|    _|_|_|    _|_|_|
//                   _|            _|
//               _|_|              _|
#include <iostream>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<ll, ll> pll;

int read() {
    int n = 0; bool b = 0; char c = getchar();
    for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
    for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
    if (b) return -n;
    return n;
}

const ll MOD = 1e9+7;

ll modpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res = (res * a) % MOD;
        }
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}

const int N = 3010;
ll step[N][N]; // [k][l]
ll prefstep[N][N];
ll addc[N][N];
ll pows[N][N];

void cpows() {
    for (ll a = 1; a < N; ++a) {
        pows[a][0] = 1;
        for (ll b = 1; b < N; ++b) {
            pows[a][b] = (pows[a][b-1] * a) % MOD;
        }
    }
}

ll getpow(ll a, ll b) {
    if (a >= 0) {
        return pows[a][b];
    }
    return 1;
}

int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    ll n, m;
    cin >> n >> m;
    cpows();
    for (ll k = 0; k <= n; ++k) {
        // FIXME: end faster than n (better constant), it should be maximally n/2
        for (ll l = 2; l <= n; ++l) {
            // FIXME: maybe eliminate modpow in favor of dp
            // step[k][l] = (step[k][l-1]*(k+1) % MOD) + (((m-k) * getpow(m-k-1, l-2) % MOD)*(k+1) % MOD);
            // ll tempa, tempb;
            // if ((tempa = modpow(m-k-1, l-2)) != (tempb = getpow(m-k-1, l-2))) {
            //     // cout << tempa << " " << tempb << " " << m-k-1 << " " <<  endl;
            //     // exit(1);
            // }
            step[k][l] = (step[k][l-1]*(k+1) % MOD) + (((m-k) * modpow(m-k-1, l-2) % MOD)*(k+1) % MOD);
            step[k][l] %= MOD;
        }
    }
    
    foru (i, n+10) prefstep[0][i] = step[0][i];
    for (ll k = 1; k <= n; ++k) {
        // FIXME: correct starting value of l
        for (ll l = 2; l <= n; ++l) {
            addc[k][l] = ((m-k)*prefstep[k-1][l-2] % MOD) + (addc[k][l-1] * (m-k-1) % MOD);
            addc[k][l] %= MOD;
            prefstep[k][l] = (prefstep[k][l-1] + addc[k][l]) % MOD;
            prefstep[k][l] = (k+1) * prefstep[k][l] % MOD;
        }
    }
    
    // for (ll k = 1; k <= n; ++k) {
    //     // FIXME: correct starting value of l
    //     for (ll l = 2; l <= n; ++l) {
    //         for (ll fend = 0; fend <= l; ++fend) {
    //             int l1 = fend, l2 = l-fend;
    //             prefstep[k][l] = ((prefstep[k-1][l1] * step[k][l2]) % MOD) + prefstep[k][l];
    //             prefstep[k][l] %= MOD;
    //         }
    //     }
    // }
#ifdef DEBUG
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            df("%lld ", addc[i][j]);
        }
        df("\n");
    }
    df("------------------------------\n");
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            df("%lld ", step[i][j]);
        }
        df("\n");
    }
    df("------------------------------\n");
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            df("%lld ", prefstep[i][j]);
        }
        df("\n");
    }
#endif
    ll s = 0;
    foru (i, n) {
        s = (s + max(prefstep[i][n], ll(0))) % MOD;
    }
    cout << s << "\n";
    return 0;
}