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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

long n; long m;
LL q = 1000000007;

LL A[3010][3010];
LL B[3010][3010];

LL policz()
{
    FOR(d,0,n) FOR(k,0,n) { A[d][k]=0; B[d][k]=0; };
    A[1][1]=0; B[1][1]=m;
    FOR(d,2,n)
    {
        LL mp = d; if (m<d) mp=m;
        FOR(k,1,mp)
        {
            A[d][k] = ( k * (A[d-1][k] + B[d-1][k]) ) % q;
            B[d][k] = ( (m-k+1)*A[d-1][k-1] + (m-k)*B[d-1][k] ) % q;
        }
    }
    LL res=0;
    LL mp = n; if (m<mp) mp=m;
    FOR(k,1,mp) res = (res + A[n][k]) % q;
    return res;
}

int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	cin >> n; cin >> m;

	cout << policz() << "\n";

    return 0;
};