#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 107
using namespace std;
typedef long long LL;
bool flag;
int f[MAX][MAX],n,m;
int ans[MAX];
LL fac[MAX];
const LL mod = 1e9+7;
int main ( )
{
fac[0] = 1LL;
for ( LL i = 1 ; i < MAX ; i++ )
{
fac[i] = fac[i-1]*i;
fac[i] %= mod;
}
while ( ~scanf ( "%d%d" , &n , &m ) )
{
flag = true;
for ( int i = 0 ; i < m ; i++ )
{
scanf ( "%d" , &f[i][0] );
if ( f[i][0] == -1 )
{
flag = false;
continue;
}
for ( int j = 1; j < n ; j++ )
scanf ( "%d" , &f[i][j] );
}
if ( flag )
{
for ( int i = 0 ; i < n ; i++ )
ans[i] = i+1;
for ( int i = m-1 ; i >= 0 ; i-- )
for ( int j = n-1 ; j >= 0 ; j-- )
ans[j] = f[i][ans[j]-1];
flag = true;
for ( int i = 0 ; i < n ; i++ )
if ( ans[i] != i+1 ) flag = false;
if (flag ) puts ( "1" );
else puts ( "0" );
}
else
{
int used[MAX];
bool mark = true;
for ( int i = 0 ; i < m ; i++ )
{
if ( f[i][0] == -1 ) continue;
memset ( used , 0 , sizeof ( used ));
for ( int j = 0 ; j < n ; j++ )
used[f[i][j]-1] = 1;
for ( int j = 0 ; j < n ; j++ )
if ( !used[j] ) mark = false;
}
if ( !mark )
{
puts ( "0" );
continue;
}
LL ans = 1LL;
LL num = 0;
for ( int i = 0 ; i < m ; i++ )
{
if( f[i][0] == -1 )
{
if ( num )
{
ans *= fac[n];
ans %= mod;
}
num++;
}
}
printf ( "%I64d\n" , ans );
}
}
return 0;
}
多校第九场 1004 hdu 5399 Too Simple(贪心)
最新推荐文章于 2020-08-05 10:01:35 发布