h,w = gets.split.map(&:to_i)
lim1 = gets.split.map(&:to_i)
lim2 = gets.split.map(&:to_i)
lim1.sort!
lim1.reverse!
lim2.sort!
lim2.reverse!
ans = 1
sum = h*w
num = Array.new(h*w, 0)
if lim1[0] != lim2[0] && lim1[0] != h*w
  puts 0
  exit
end
num[-1] = 3
for i in 0..h-2
  if lim1[i] == lim1[i+1]
    puts 0
    exit
  else
    num[lim1[i+1]-1] += 1
  end
end
for i in 0..w-2
  if lim2[i] == lim2[i+1]
    puts 0
    exit
  else
    num[lim2[i+1]-1] += 2
  end
end

count1 = 0
count2 = 0
while sum > 0
  if num[sum-1] == 3
    count1 += 1
    count2 += 1
  elsif num[sum-1] == 2
    ans *= count1
    count2 += 1
  elsif num[sum-1] == 1
    ans *= count2
    count1 += 1
  else
    ans *= (count1*count2)-(h*w-sum)
  end
  if ans <= 0
    puts 0
    exit
  end
  ans %= (10**9)+7
  sum -= 1
end

p ans
