#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 1010
#define P 1000000007
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,a[N],b[N],posa[N*N],posb[N*N],ans;
int row,line;
int main()
{
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	n=read(),m=read();
	for (int i=1;i<=n;i++)
	{
		a[i]=read();
		if (posa[a[i]]) {cout<<0;return 0;}
		posa[a[i]]=i;
	}
	for (int i=1;i<=m;i++)
	{
		b[i]=read();
		if (posb[b[i]]) {cout<<0;return 0;}
		posb[b[i]]=i;
	}
	row=0,line=0;ans=1;
	for (int i=n*m;i>=1;i--)
	{
		if (posa[i]&&posb[i]) {row++,line++;continue;}
		if (!posa[i]&&!posb[i])
		{
			if (row*line-(n*m-i)<=0) {cout<<0;return 0;}
			ans=1ll*ans*(row*line-(n*m-i))%P;
		}
		if (posa[i]) ans=1ll*ans*line%P,row++;
		if (posb[i]) ans=1ll*ans*row%P,line++;
	}
	cout<<ans;
}