DBG = true
P = 1000000007
n,m = gets.split.map{|x| x.to_i}
a = gets.split.map{|x| x.to_i}
b = gets.split.map{|x| x.to_i}
a.sort_by!{|x| -x}
b.sort_by!{|x| -x}

r = n
c = m
rs = 0
cs = 0
rcs = 0
x = 1

(n*m).downto(1) { |i|
  ron = a.include?(i)
  con = b.include?(i)
  if ron && con
    rcs += (n-r)+(m-c)   # r,c uses the old value
    r -= 1
    c -= 1
    rs += c  # NOTE c,r uses the new (updated) value
    cs += r    # no change for x
  elsif ron
    r -= 1
    rs += c
    x = (x * cs) % P
    cs -= m-c  # c, rcs, stay
    rcs += m-c-1
  elsif con
    c -= 1
    cs += r
    x = (x * rs) % P
    rs -= n-r  # c, rcs, stay
    rcs += n-r-1
  else
    x = (x * rcs) % P
    rcs -= 1
  end
  if DBG
    puts "i #{i} ron #{ron} con #{con} r #{r} c #{c} rs #{rs} cs #{cs} " +
         "rcs #{rcs} x #{x}"
  end
}
print x
