#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int N, M, A[1010], B[1010], K[1010][1010];
	cin >> N >> M;
	for(int i=0; i<N; ++i){
	    cin >> A[i];
	    for(int j=0; j<M; ++j) K[i][j]=0;
	}
	for(int i=0; i<M; ++i) cin >> B[i];
	sort(A, A+N, greater<int>()); A[N]=0;
	sort(B, B+M, greater<int>()); B[M]=0;
	int ans=1, h=0, w=0;
	for(int i=N*M; i>0; --i){
	    if(A[h]==i){
	        ++h;
	        if(B[w]==i) ++w;
	        else ans=((long) ans*w)%1000000007;
	    }
	    else if(B[w]==i){
	        ++w;
	        ans=((long) ans*h)%1000000007;
	    }
	    else ans=((long) ans*(h*w-(N*M-i)))%1000000007;
	    if(ans==0) break;
	}
	cout << ans << endl;
	return 0;
}
