mod = 10**9+7

N,M=gets.split(" ").map{|i| i.to_i}
A=gets.split(" ").map{|i| i.to_i}
B=gets.split(" ").map{|i| i.to_i}
cells = N*M
iA = Array.new(cells){false}
iB = Array.new(cells){false}
sumA = Array.new(cells){0}
sumB = Array.new(cells){0}
for i in 0..(N-1)
	if(iA[A[i]-1])
		puts 0
		exit(0)
	end
	iA[A[i]-1] = true
	sumA[A[i]-1] = 1
end
(cells-2).downto(0){|i|
	sumA[i] += sumA[i+1]
}
for j in 0..(M-1)
	if(iB[B[j]-1])
		puts 0
		exit(0)
	end
		iB[B[j]-1] = true
	sumB[B[j]-1] = 1
end
(cells-2).downto(0){|j|
	sumB[j] += sumB[j+1]
}
# p iA
# p iB
# p sumA
# p sumB
comb = 1
filled = 0
(cells).downto(0){|n|
	i = n-1
	if(iA[i] && iB[i])
		comb = comb
	elsif(iA[i])
		comb = comb * sumB[i] % mod
	elsif(iB[i])
		comb = comb * sumA[i] % mod
	else
		poss = sumA[i] * sumB[i] - filled
		poss = 0 if poss < 0
		comb = comb * poss % mod
	end
	filled += 1
}
puts comb


