#include <bits/stdc++.h>
#define DEBUG
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll read(){
	char c=getchar();bool flag=0;ll x=0;
	while(c<'0'||c>'9'){if(c=='-')flag=1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return flag?-x:x;
}
#define N 2010 
const ll p=1e9+7;
ll dp[N][N],fac[N*N],inv[N*N];
ll fpow(ll a,ll x){
	ll ans=1;
	while(x){
		if(x&1) ans=ans*a%p;
		a=a*a%p,x=x/2; 
	}
	return ans;
}
ll Cnt(ll n,ll m){
	return fac[m]*inv[n]%p*inv[m-n]%p;
}
int main(){
	ll n=read(),k=read();
	if(k==1){
		printf("1");
		return 0;
	}
	fac[0]=1;
	for(ll i=1;i<=n*k;i++) fac[i]=fac[i-1]*i%p;
	inv[n*k]=fpow(fac[n*k],p-2);
	for(ll i=n*k-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%p;
	for(ll i=1;i<=n;i++) dp[i][0]=1;
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=i;j++){
			dp[i][j]=dp[i-1][j]+dp[i][j-1]*(n-(j-1))%p*Cnt(k-2,n*k-i-(j-1)*(k-1)-1)%p;
			dp[i][j]%=p;
		}
	}
	printf("%lld",dp[n][n]); 
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

const ll MOD = 1e9+7;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	map<int, int> y;
	map<int, int> x;
	rep(i,0,n) {
		int a;
		cin >> a;
		y[a] = i;
	}
	rep(i,0,m) {
		int b;
		cin >> b;
		x[b] = i;
	}
	ll ret = 1;
	ll A = 0;
	ll B = 0;
	per(i,1,n*m+1) {
		bool flagy = (y.find(i) != y.end());
		bool flagx = (x.find(i) != x.end());
		if(flagy && flagx) {
			A++; B++;
		} else if(flagy) {
			ret = ret * B % MOD;
			A++;
		} else if(flagx) {
			ret = ret * A % MOD;
			B++;
		} else {
			ll loc = A * B - (ll)(n*m - i);
			ret = ret * loc % MOD;
		}
	}
	cout << ret << '\n';
}