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

if as.uniq != as || bs.uniq != bs
  puts(0)
else
  ans = 1
  (n * m).times do |i|
    val = n * m - i
    can_set = 0
    if as.include?(val)
      if bs.include?(val)
        can_set = 1
      else
        can_set = bs.count { |b| b >= val }
      end
    elsif bs.include?(val)
      can_set = as.count { |a| a >= val }
    else
      n.times do |y|
        if as[y] >= val
          m.times do |x|
            if bs[x] >= val
              can_set += 1
            end
          end
        end
      end
      can_set -= i
    end
    ans = (ans * can_set) % 1000000007
  end
  puts(ans)
end
