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
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

vector< vector<long long int> > p; 
long long int dv;

int main()
{
    long long int n, m;
    scanf("%lld%lld", &n, &m);
    dv = 1000000007;

    p.resize(n+1, vector<long long int>(n+1, 0));

    vector< vector<long long int> > pws(n+1, vector<long long int>(n+1, 1)); 
    for(int mi=0; mi<=n; ++mi) {
        pws[0][mi] = 1;
        for(int ni=1; ni<=n; ++ni)
            pws[ni][mi] = (pws[ni-1][mi]*mi)%dv;
    }

    for(int i=0; i<=n; ++i) { p[0][i]=1; p[1][i]=0; }
    for(int ni=2; ni<=n; ++ni) {
        for(int mi=1; mi<=m; ++mi) {
            for(int k=0; k<=ni-2; ++k) { 
                // p[ni][mi] = (p[ni][mi] + mi^(ni-k-1)*p[k][mi-1])%dv;                
                p[ni][mi] = (p[ni][mi] + pws[ni-k-1][mi]*p[k][mi-1])%dv;      
                // p[ni][mi] = (p[ni][mi] + pw(mi, ni-k-1)*p[k][mi-1])%dv;      
                // printf("> %d %d %d: %lld\n", ni, mi, k, p[ni][mi]);        
            }  
        }
        for(int mi=ni+1; mi<=n; ++mi) 
            p[ni][mi]=p[ni][ni];
    }

    long long int ret;
    if(m>n) {
        printf("%lld\n", p[n][n]%dv);
    }
    else
        printf("%lld\n", p[n][m]%dv);

    return 0;
}