N, M = STDIN.gets.split.map(&:to_i)
An = STDIN.gets.split.map(&:to_i).sort.reverse
Bm = STDIN.gets.split.map(&:to_i).sort.reverse

MOD = 10**9 + 7

values = (1..N*M).to_a.reverse

# p An, Bm, values

if An == Bm
  puts 0
else
  res = 1
  values.each_with_index.map do |v, i|
    if An.include?(v) && Bm.include?(v)
      # do nothing
    elsif An.include?(v)
      res *= Bm.count {|b| b >= v}
    elsif Bm.include?(v)
      res *= An.count {|a| a >= v}
    else
      res *= An.count {|a| a >= v} * Bm.count {|b| b >= v} - i
    end
  end

  p res % MOD
end
