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

using namespace std;

using LL = long long;

const int N = 3007;
const LL MOD = 1e9+7;

LL gownoDP[N][2];
LL tempDP[N][2];

LL fastpow(LL a, LL exp)
{
    LL res = 1;

    while (exp)
    {
        if (exp & 1)
        {
            res = (res * a) % MOD;
        }

        a = (a * a) % MOD;
        exp >>= 1;
    }

    return res;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    LL n, m;
    cin >> n >> m;

    if (n == 1)
    {
        cout << 0 << "\n";
        return 0;
    }

    if (m == 1)
    {
        cout << 1 << "\n";
        return 0;
    }

    gownoDP[1][0] = m;

    for (int i = 2; i <= n; ++i)
    {
        for (int j = 0; j <= i; ++j)
        {   
            if (j < m)
                tempDP[j][0] = (tempDP[j][0] + gownoDP[j][0] * (m - j)) % MOD;
            tempDP[j][1] = (tempDP[j][1] + gownoDP[j][0] * j) % MOD;

            if (j < m)
                tempDP[j + 1][0] = (tempDP[j + 1][0] + gownoDP[j][1] * (m - j)) % MOD;
            tempDP[j][1] = (tempDP[j][1] + gownoDP[j][1] * j) % MOD;
        }

        for (int j = 0; j <= i; ++j)
        {
            gownoDP[j][0] = tempDP[j][0];
            gownoDP[j][1] = tempDP[j][1];

            tempDP[j][0] = tempDP[j][1] = 0;
        }
    }

    LL res = 0;

    for (int i = 0; i <= n; ++i)
    {
        res += gownoDP[i][1];
        if (res > MOD)
            res -= MOD;
    }

    cout << res << "\n";

    return 0;
}