n,m=gets.split.map(&:to_i)
as=gets.split.map(&:to_i).sort_by {|a| -a}
bs=gets.split.map(&:to_i).sort_by {|b| -b}


if as.uniq != as || bs.uniq != bs
  p 0
  exit 0
end

i = 0
j = 0
t = n*m
count = 0
ret = 1
while 0 < t
  i += 1 while i < as.length && t < as[i]
  j += 1 while j < bs.length && t < bs[j]

  if as[i] == t && bs[j] == t
    # nop
  elsif as[i] == t
    ret *= j
  elsif bs[j] == t
    ret *= i
  else
    pat = i*j - count
    if pat <= 0
      p 0
      exit 0
    end
    ret *= pat
  end

  ret %= 1_000_000_007
  count += 1
  t -= 1
end

p ret