
MOD = 10**9 + 7
N,M = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
b = gets.split.map(&:to_i).sort
ans = (a.uniq == a || b.uniq == b) ? 1 : 0
row,col = 0,0
(N*M).downto(1) do |i|
  fa,fb = a[-1-row] == i, b[-1-col] == i
  if fa && fb
    row += 1
    col += 1
  elsif !fa && fb
    col += 1
    ans = (ans*row) % MOD
  elsif fa && !fb
    row += 1
    ans = (ans*col) % MOD
  else
    ans = (ans*(row*col-(N*M-i))) % MOD
  end
  #p [i,fa,fb,row,col,ans]
end
p ans