DIV = 10 ** 9 + 7

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
ans = 0

if a.uniq.size == n || b.uniq.size == m
  ans = 1
  fixes = a & b
  maxes = (a + b).uniq - fixes
  (n*m).downto(1).each do |i|
    if fixes.include?(i)
      # nothing
    elsif maxes.include?(i)
      if j = a.index(i)
        ans = ans * b.count{ |k| k > i } % DIV
      else
        j = b.index(i)
        ans = ans * a.count{ |k| k > i } % DIV
      end
    else
      count = b.count{ |k| k > i } * a.count{ |k| k > i }
      ans = ans * (count - n * m + i) % DIV
    end
  end
end
puts ans