MOD = 10 ** 9 + 7
N, M = gets.split.map(&:to_i)
L = N * M
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)

counterA = Hash.new(0)
counterB = Hash.new(0)

A.each do |a|
  counterA[a] += 1
end

B.each do |b|
  counterB[b] += 1
end

if counterA.values.max > 1 || counterB.values.max > 1
  puts 0
else
  ans = 1

  L.downto(1) do |l|
    next if counterA[l] > 0 && counterB[l] > 0

    if counterA[l] > 0
      ans *= B.count { |b| b >= l }
    elsif counterB[l] > 0
      ans *= A.count { |a| a >= l }
    else
      ans *= A.count { |a| a >= l } * B.count { |b| b >= l } - (L - l)
    end

    ans %= MOD
  end

  puts ans
end
