n, m = gets.chomp.split(' ').map(&:to_i)
ar = gets.chomp.split(' ').map(&:to_i).sort.reverse
br = gets.chomp.split(' ').map(&:to_i).sort.reverse

size = n*m

if ar.uniq.size != ar.size || br.uniq.size != br.size
  puts 0
  exit 0
end

def get_bigger_count(x, array)
  count = 0
  array.each do |n|
    if x < n
      count += 1
    else
      break
    end
  end
  count
end

ans = 1
div = 10**9 + 7
(1..size).to_a.reverse.each do |x|
  ar_i = ar.include?(x)
  br_i = br.include?(x)
  next if ar_i && br_i

  count = if ar_i
            get_bigger_count(x, br)
          elsif br_i
            get_bigger_count(x, ar)
          else
            get_bigger_count(x, ar) * get_bigger_count(x, br) - (size - x)
          end

  ans = (ans * count) % div
end

puts ans



