题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2115
题目大意:


思路:
这里有序节点的穿插,就相当于有重复元素的全排列。s=n!/(n1!*n2!*n3! ***)

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod=1000000007;

vector<LL> G[40005];
LL sum[40005];
LL p[40005];
LL kpow(LL a, LL b){
    LL ans=1;
    while(b){
        if(b&1){
            ans=ans*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

LL dfs(LL u){
    LL s=0;
    for(LL i=0; i<G[u].size(); i++){
        s+=dfs(G[u][i]);
    }

    return sum[u]=s+1;
}

int main()
{
    LL t;
    p[0]=1;
    for(LL i=1; i<=40000; i++){
        p[i]=p[i-1]*i%mod;
    }
    scanf("%lld", &t);
    while(t--){
        LL n, m, u, v;
        scanf("%lld%lld", &n, &m);
        for(LL i=0; i<=n; i++){
            G[i].clear();
            sum[i]=0;
        }
        for(LL i=1; i<=m; i++){
            scanf("%lld%lld", &u, &v);
            G[v].push_back(u);
        }
        for(LL i=1; i<=n; i++){
            if(sum[i]==0){
                dfs(i);
            }
        }
        LL f=1;
        for(LL i=1; i<=n; i++){
            f=f*sum[i]%mod;
        }
        printf("%lld\n", p[n]*kpow(f, mod-2)%mod);
    }

    return 0;
}