Language: C++

#pragma GCC optimize ("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<int,int> P; typedef pair<Int,Int> Pl;
const int mod = 1e9+7;
const Int Inf = 1e17;
#define END {cout<<ans<<'\n'; return 0;}
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define Pr(t) priority_queue<t>
#define gPr(t) priority_queue<t,vector<t>,greater<t>>
#define V(t) vector<t>
#define rep(i,n) for(int(i)=0; i<n; i++)
#define rer(i,l,r) for(int i=l; i<=r; i++)
#define gnr(i,l,r) for(int i=r; i>=l; i--)
#define eb emplace_back
#define pri1(a) cout<<(a)<<'\n'
#define pri2(a,n) {rep(pri2i,n-1)cout<<a[pri2i]<<' '; cout<<a[n-1]<<'\n';}
#define prip(p) cout<<p.fi<<' '<<p.se<<'\n'
template<class T> inline bool cmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool cmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt() : x(0) {}
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt &operator%=(ModInt that) { x %= that.x; return *this; }

	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt operator%(ModInt that) const { return ModInt(*this) %= that; }

	ModInt inverse() const {
		signed a = x, b = MOD, u = 1, v = 0;
		while(b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if(u < 0) u += Mod;
		ModInt res; res.x = (unsigned)u;
		return res;
	}

	bool operator==(ModInt that) const { return x == that.x; }
	bool operator!=(ModInt that) const { return x != that.x; }
	ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
};
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
	ModInt<MOD> r = 1;
	while(k) {
		if(k & 1) r *= a;
		a *= a;
		k >>= 1;
	}
	return r;
}

typedef ModInt<mod> mint;

int n,m,_,x,y,q;
string s,sb;
bool ok;
char cf,cb;

int main(){
  cin.tie(nullptr); ios::sync_with_stdio(0);

  cin>>n>>m; int a[n],b[m]; rep(i,n)cin>>a[i]; rep(i,m)cin>>b[i];
  sort(a,a+n); sort(b,b+m); int c[n*m+1]={};
  rep(i,n)c[a[i]]++; rep(i,m)c[b[i]]+=2;
  mint ans=1;
  gnr(i,1,n*m){
    if(c[i]==3)continue;
    else if(c[i]==1){
      ans*=(m-distance(b,lower_bound(b,b+m,i)));
    }else if(c[i]==2){
      ans*=(n-distance(a,lower_bound(a,a+n,i)));
    }else if(!c[i]){
      ans*=(m-distance(b,lower_bound(b,b+m,i)))*(n-distance(a,lower_bound(a,a+n,i)))-(n*m-i);
    }else{ans=0; END}
  }
  END
}

Compiler output:
a.cc: In function 'int main()':
a.cc:102:14: error: conversion from 'std::iterator_traits<int*>::difference_type' {aka 'long int'} to 'ModInt<1000000007>' is ambiguous
  102 |       ans*=(m-distance(b,lower_bound(b,b+m,i)));
      |            ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:33:9: note: candidate: 'ModInt<MOD>::ModInt(long long int) [with int MOD = 1000000007]'
   33 |         ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:32:9: note: candidate: 'ModInt<MOD>::ModInt(int) [with int MOD = 1000000007]'
   32 |         ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:38:35: note:   initializing argument 1 of 'ModInt<MOD>& ModInt<MOD>::operator*=(ModInt<MOD>) [with int MOD = 1000000007]'
   38 |         ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
      |                            ~~~~~~~^~~~
a.cc:104:14: error: conversion from 'std::iterator_traits<int*>::difference_type' {aka 'long int'} to 'ModInt<1000000007>' is ambiguous
  104 |       ans*=(n-distance(a,lower_bound(a,a+n,i)));
      |            ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:33:9: note: candidate: 'ModInt<MOD>::ModInt(long long int) [with int MOD = 1000000007]'
   33 |         ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:32:9: note: candidate: 'ModInt<MOD>::ModInt(int) [with int MOD = 1000000007]'
   32 |         ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:38:35: note:   initializing argument 1 of 'ModInt<MOD>& ModInt<MOD>::operator*=(ModInt<MOD>) [with int MOD = 1000000007]'
   38 |         ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
      |                            ~~~~~~~^~~~
a.cc:106:85: error: conversion from 'std::iterator_traits<int*>::difference_type' {aka 'long int'} to 'ModInt<1000000007>' is ambiguous
  106 |       ans*=(m-distance(b,lower_bound(b,b+m,i)))*(n-distance(a,lower_bound(a,a+n,i)))-(n*m-i);
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
a.cc:33:9: note: candidate: 'ModInt<MOD>::ModInt(long long int) [with int MOD = 1000000007]'
   33 |         ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:32:9: note: candidate: 'ModInt<MOD>::ModInt(int) [with int MOD = 1000000007]'
   32 |         ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
      |         ^~~~~~
a.cc:38:35: note:   initializing argument 1 of 'ModInt<MOD>& ModInt<MOD>::operator*=(ModInt<MOD>) [with int MOD = 1000000007]'
   38 |         ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
      |                            ~~~~~~~^~~~