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

An_hash = Hash.new {0}
An.each do |a|
  An_hash[a] += 1
end

Bm_hash = Hash.new {0}
Bm.each do |b|
  Bm_hash[b] += 1
end

if An == Bm
  puts 0
else
  pattern = values.each_with_index.map do |v, i|
    if An_hash[v] != 0 && Bm_hash[v] != 0
      1
    elsif An_hash[v] != 0
      count = 0
      (N*M).downto(v) do |t|
        count += Bm_hash[t]
      end
      count
    elsif Bm_hash[v] != 0
      count = 0
      (N*M).downto(v) do |t|
        count += An_hash[t]
      end
      count
    else
      count_b = 0
      (N*M).downto(v) do |t|
        count_b += Bm_hash[t]
      end
      count_a = 0
      (N*M).downto(v) do |t|
        count_a += An_hash[t]
      end
      count_a * count_b - i
    end
  end

  p pattern.inject(1) {|product, v| product * v} % MOD
end
