如果有-1,且函数合法,那么就有(N!)^(x-1),x是-1的个数
否则只需要判断函数映射是否会x= f(f(f....f(x))即可
函数合法的条件是:y = f(x) y包含1到n的每个数字
#include
#include
#include
#include
using namespace std;
int f[101][101];
#define ll long long
int jie[101];
int mod = 1000000007;
int ans[101];
int main(){
int n,m;
jie[0] = 1;
for(int i =1; i <= 100;i++)
jie[i] =1ll*jie[i-1]*i%mod;
while(scanf("%d%d",&n,&m)!=EOF){
int res = 0;
for(int i = 0;i < m; i++){
scanf("%d",&f[i][1]);
if(f[i][1] == -1) {
res++;
continue;
}
for(int j = 2;j <= n; j++)
scanf("%d",&f[i][j]);
}
if(res == 0){
for(int i = 1;i <= n; i++)
ans[i] = i;
for(int i = m-1; i >= 0; i--)
for(int j = 1;j <= n; j++)
ans[j] = f[i][ans[j]];
res = 1;
for(int i = 1;i <= n; i++)
if(ans[i] != i ) res = 0;
if(res)puts("1");
else puts("0");
}
else {
int flag = 1;
for(int i = 0;i < m && flag; i++){
if(f[i][1] == -1) continue;
memset(ans,0,sizeof(ans));
for(int j = 1;j <= n;j++)
ans[f[i][j]] = 1;
for(int j = 1;j <= n;j++)
if(ans[j] == 0)
flag = 0;
}
if(flag == 0) puts("0");
else {
ll t = 1;
for(int i = 1;i < res; i++){
t = t*jie[n]%mod;
}
cout<