#include<cstdio>
#include<algorithm>

using int64=long long;
using uchar=unsigned char;

constexpr int maxn(1000);
constexpr int p(1000000007);

namespace IOManager{
	constexpr int FILESZ(131072);
	char buf[FILESZ];
	const char*ibuf=buf,*tbuf=buf;

	struct IOManager{
		inline char gc()
			{return(ibuf==tbuf)&&(tbuf=(ibuf=buf)+fread(buf,1,FILESZ,stdin),ibuf==tbuf)?EOF:*ibuf++;}

		template<class _Tp>
			inline operator _Tp(){
				_Tp s=0u;char c=gc();
				for(;c<48;c=gc());
				for(;c>47;c=gc())
					s=(_Tp)(s*10u+c-48u);
				return s;
			}
	};
}IOManager::IOManager io;

int r[maxn*maxn+1],
	c[maxn*maxn+1];

int main(){
	const int n=io,m=io,tot=n*m;
	for(int i=1;i<=n;++i)
		r[(int)io]=i;
	for(int i=1;i<=m;++i)
		c[(int)io]=i;

	int ans=1,lr=0,lc=0,cnt=0;
	for(int i=tot;i;--i){
		if(r[i]&&c[i])
			cnt+=lr+lc,
			++lr,++lc;
		else if(r[i])
			ans=(int64)ans*lc%p,
			++lr,cnt+=lc-1;
		else if(c[i])
			ans=(int64)ans*lr%p,
			++lc,cnt+=lr-1;
		else ans=(int64)ans*cnt%p,--cnt;
	}printf("%d",ans);

	return 0;
}
