n, m = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
bs = gets.split.map(&:to_i)

as.sort!
bs.sort!

if as.uniq != as || bs.uniq != bs
  puts(0)
else
  ans = 1
  a_head = as.size - 1
  b_head = bs.size - 1
  (n * m).times do |i|
    val = n * m - i
    can_set = nil
    if val == as[a_head]
      a_head -= 1
      if val == bs[b_head]
        b_head -= 1
        can_set = 1
      else
        can_set = bs.size - b_head - 1
      end
    elsif bs[b_head] == val
      b_head -= 1
      can_set = as.size - a_head - 1
    else
      can_set = (as.size - a_head - 1) * (bs.size - b_head - 1) - i
    end
    ans = (ans * can_set) % 1000000007
  end
  puts(ans)
end
