Skip to content

Instantly share code, notes, and snippets.

@asSqr
Created June 22, 2019 15:40
Show Gist options
  • Save asSqr/73291a37ec0ca27f63aa98df8829b1e8 to your computer and use it in GitHub Desktop.
Save asSqr/73291a37ec0ca27f63aa98df8829b1e8 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <algorithm>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()
using ll = long long;
constexpr ll mod = 10000007;
ll N, M;
ll A[1010], B[1010];
int main()
{
scanf( "%lld%lld", &N, &M );
rep( i, N )
scanf( "%lld", A+i );
rep( i, M )
scanf( "%lld", B+i );
std::sort( A, A+N );
std::sort( B, B+M );
ll prod = 1;
for( ll i = N*M; i > 0; --i )
{
ll p = std::lower_bound( A, A+N, i )-A;
ll q = std::lower_bound( B, B+M, i )-B;
if( A[p] == i && B[q] == i )
{
if( (p+1 < N && A[p+1] == A[p]) || (q+1 < M && B[q+1] == B[q]) )
prod = 0;
}
else if( A[p] == i )
prod = (prod*((M-q)-(N*M-i))) % mod;
else if( B[q] == i )
prod = (prod*((N-p)-(N*M-i))) % mod;
else
prod = (prod*((N-p)*(M-q)-(N*M-i))) % mod;
}
printf( "%lld\n", prod );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment