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
fix = n.times.map{ [-1] * m }
if a.uniq.size == n || b.uniq.size == m
  ans = 1
  fixes = a & b
  fixes.each do |i|
    j = a.index(i)
    k = b.index(i)
    fix[j][k] = i
  end
  maxes = (a + b).uniq
  ((maxes.min..n*m).to_a - fixes.to_a).reverse.each do |i|
    if 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 = 0
      
      n.times do |j|
        m.times do |k|
          if a[j] > i && b[k] > i && fix[j][k] == -1
            count += 1
          end
        end
      end
      ans = ans * count % DIV
    end
  end

  (1..maxes.min-1).each do |i|
    ans = ans * i % DIV
  end
end
puts ans