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

x = 0 # number of An bigger than i
y = 0 # number of Bm bigger than i
res = 1
(N*M).downto(1) do |i|
  if An[x] == i && Bm[y] == i
    x += 1
    y += 1
    res *= 1
  elsif An[x] == i
    x += 1
    res *= y
  elsif Bm[y] == i
    y += 1
    res *= x
  else
    res *= x * y - (N*M - i)
  end
  
  res %= MOD
end

p res
