n,m=gets.split.map &:to_i;
a=gets.split.map &:to_i;
b=gets.split.map &:to_i;
a=a.sort.reverse;
b=b.sort.reverse;
s=1;
ia=ib=0;
(n*m).downto(1){|i|
	d=e=0;
	(d+=1;ia+=1)while ia<n&&a[ia]==i;
	(e+=1;ib+=1)while ib<m&&b[ib]==i;
	s*=(d>1 ? 0 : [ia,1][d])*(e>1 ? 0 : [ib,1][e])-[n*m-i,0][d+e>0 ? 1 : 0];
	s%=10**9+7;
}
p s