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

const long long mod = 1000000007;

long long zz[3003][3003], un[3003][3003], sil[10004], resil[10004], sufsil[10004], web[3003][3003], zxc[3003][3003];

long long fpow(long long pod, long long wyk)
{
    long long w = 1;
    while (wyk)
    {
        if (wyk & 1)
        {
            w *= pod;
            w %= mod;
        }
        pod *= pod;
        pod %= mod;
        wyk /= 2;
    }
    return w;
}

long long dwu(long long a, long long b)
{
    if (a <= 10000)
    {
        long long rw = (sil[a] * resil[b]) % mod;
        rw *= resil[a - b];
        rw %= mod;
        return rw;
    }
    else
    {
        long long rw = (sufsil[b] * resil[b]) % mod;
        return rw;
    }
}


int main()
{
    ios_base::sync_with_stdio(0);
    long long n, m;
    cin >> n >> m;
    if (n == 1)
    {
        cout << 0;
        return 0;
    }
    if (n == 2)
    {
        cout << m << '\n';
        return 0;
    }
    if (n == 3)
    {
        cout << (m * (m)) % mod;
        return 0;
    }
    sil[0] = 1;
    sufsil[0] = 1;
    resil[0]=1;
    for (long long i = 1; i <= 10000; i++)
    {
        sil[i] = (sil[i - 1] * i) % mod;
        resil[i] = fpow(sil[i], mod - 2);
        sufsil[i] = sufsil[i - 1] * (m - i + 1) % mod;
    }
    for (int i = 1; i <= n; i++)
    {
        zz[i][1] = 0;
        zz[1][i] = 0;
        un[i][1] = 1;
        un[1][i] = 1;
        zxc[1][i] = 1;
        zxc[i][1] = 1;
    }
    long long zb = 0;
    zxc[0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (long long j = 1; j <= n; j++)
        {
            zxc[i][j] = (zxc[i - 1][j] * j + zxc[i - 1][j - 1]) % mod;
            un[i][j] = (zxc[i - 1][j] * sil[j]) % mod;
        }
    }

    for (int j = 2; j < n; j++)
    {

        // cout<<i<<' '<<j<<' '<<zxc[i-1][j]<<'\n';
        // zxc[i][j] = ((tot * dwu(m, j)) % mod) * fpow(sufsil[j], mod - 2) % mod;
        // cout << tot << ' ' << zxc[i][j] << ' ' << i << ' ' << j << '\n';
        // cout << tot << ' ';
        if (j <= m)
        {
            zb += un[n][j] * dwu(m, j);
            zb %= mod;
        }
    }
    // cout;
    long long gw = m;
    for (int i = 2; i <= n; i++)
    {
        // cout << i << ": ";
        for (long long j = 2; j + 1 < i; j++)
        {
            if (j > m)
                break;
            zz[i][j] = zz[i - 1][j] * (j - 1);
            zz[i][j] %= mod;
            // cout << zz[i][j] << ' ' << ' ' << un[i][j] << ' ';
            zz[i][j] += un[i][j] * resil[j];
            zz[i][j] %= mod;
            // cout << zz[i][j] << ' ';
            web[i][j] = zz[i][j] * sil[j];
            web[i][j] %= mod;
            if (i == n)
            {
                gw += web[i][j] * dwu(m, j);
                gw %= mod;
            }
        }
        // cout << '\n';
        zz[i][i - 1] = 1;
        web[i][i - 1] = sil[i - 1];
        if ((i == n) && ((i - 1) <= m))
        {
            gw += web[i][i - 1] * dwu(m, i - 1);
            gw %= mod;
        }
    }
    cout << gw << '\n';
}