local mod = 1000000007
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local n, m = io.read("*n", "*n")
local a, b = {}, {}
for i = 1, n do a[i] = io.read("*n") end
for i = 1, m do b[i] = io.read("*n") end
table.sort(a, function(x, y) return x > y end)
table.sort(b, function(x, y) return x > y end)
local ret = 1
for i = 1, n - 1 do if a[i] == a[i + 1] then ret = 0 break end end
for i = 1, m - 1 do if b[i] == b[i + 1] then ret = 0 break end end
local big_n, big_m = 0, 0
local bigused = 0
local cur_a, cur_b = 1, 1
for i = n * m, 1, -1 do
  if a[cur_a] == i and b[cur_b] == i then
    big_n, big_m = big_n + 1, big_m + 1
    bigused = bigused + 1
    cur_a = cur_a + 1
    cur_b = cur_b + 1
  elseif a[cur_a] == i then
    if big_m == 0 then
      ret = 0
      break
    else
      ret = bmul(ret, big_m)
      big_n = big_n + 1
      bigused = bigused + 1
      cur_a = cur_a + 1
    end
  elseif b[cur_b] == i then
    if big_n == 0 then
      ret = 0
      break
    else
      ret = bmul(ret, big_n)
      big_m = big_m + 1
      bigused = bigused + 1
      cur_b = cur_b + 1
    end
  else
    if big_n * big_m <= bigused then
      ret = 0
      break
    else
      ret = bmul(ret, big_n * big_m - bigused)
      bigused = bigused + 1
    end
  end
end
print(ret)
