output_description: Print the number of ways to write the numbers under the conditions, modulo
  10^9 + 7.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      A_1 A_2 ... A_{N}
      B_1 B_2 ... B_{M}
attempt: import sys
  
  input = sys.stdin.readline
  
  n, m = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  
  
  # combination---
  # nとmodは適宜変える
  
  mod = 10**9 + 7
  fac = [1, 1]
  inv = [1, 1]
  finv = [1, 1]
  for i in range(2, n * m + 3):
      fac.append(fac[i - 1] * i % mod)
      inv.append(mod - inv[mod % i] * (mod // i) % mod)
      finv.append(finv[i - 1] * inv[i] % mod)
  
  
  def nck(n, k):
      if n < k:
          return 0
      if n < 0 or k < 0:
          return 0
      return fac[n] * (finv[k] * finv[n - k] % mod) % mod
  
  
  memo1 = [0] * (n * m + 1)
  memo2 = [0] * (n * m + 1)
  memo3 = [0] * (n * m + 1)
  memo4 = [0] * (n * m + 1)
  
  
  for i in range(n):
      for j in range(m):
          memo1[min(a[i], b[j])] += 1
          memo2[min(a[i] - 1, b[j])] += 1
          memo3[min(a[i], b[j] - 1)] += 1
          memo4[min(a[i] - 1, b[j] - 1)] += 1
  
  
  count = 0
  ans0 = 1
  for i in range(n * m):
      count += 1
      if memo1[i + 1] == 0:
          continue
      if memo1[i + 1] > count:
          ans0 = 0
      else:
          ans0 *= nck(count, memo1[i + 1])
          ans0 %= mod
          count -= memo1[i + 1]
  
  count = 0
  ans1 = 1
  for i in range(n * m):
      count += 1
      if memo2[i + 1] == 0:
          continue
      if memo2[i + 1] > count:
          ans1 = 0
      else:
          ans1 *= nck(count, memo2[i + 1])
          ans1 %= mod
          count -= memo2[i + 1]
  
  count = 0
  ans2 = 1
  for i in range(n * m):
      count += 1
      if memo3[i + 1] == 0:
          continue
      if memo3[i + 1] > count:
          ans2 = 0
      else:
          ans2 *= nck(count, memo3[i + 1])
          ans2 %= mod
          count -= memo3[i + 1]
  
  count = 0
  ans3 = 1
  for i in range(n * m):
      count += 1
      if memo4[i + 1] == 0:
          continue
      if memo4[i + 1] > count:
          ans3 = 0
      else:
          ans3 *= nck(count, memo4[i + 1])
          ans3 %= mod
          count -= memo4[i + 1]
  
  print((ans0 - ans1 - ans2 + ans3) % mod)
  
problem_description: Statement
  
  Consider writing each of the integers from 1 to N \times M in a grid with N
  rows and M columns, without duplicates. Takahashi thinks it is not fun enough,
  and he will write the numbers under the following conditions:
  
    * The largest among the values in the i-th row (1 \leq i \leq N) is A_i.
    * The largest among the values in the j-th column (1 \leq j \leq M) is B_j.
  
  For him, find the number of ways to write the numbers under these conditions,
  modulo 10^9 + 7.
samples: [{"input": "2 2\n    4 3\n    3 4", "output": "2\n    \n\n(A_1, A_2) = (4, 3) and (B_1, B_2) = (3, 4). In this case, there are two ways\nto write the numbers, as follows:\n\n  * 1 in (1, 1), 4 in (1, 2), 3 in (2, 1) and 2 in (2, 2).\n  * 2 in (1, 1), 4 in (1, 2), 3 in (2, 1) and 1 in (2, 2).\n\nHere, (i, j) denotes the square at the i-th row and the j-th column.\n\n* * *"}, {"input": "3 3\n    5 9 7\n    3 6 9", "output": "0\n    \n\nSince there is no way to write the numbers under the condition, 0 should be\nprinted.\n\n* * *"}, {"input": "2 2\n    4 4\n    4 4", "output": "0\n    \n\n* * *"}, {"input": "14 13\n    158 167 181 147 178 151 179 182 176 169 180 129 175 168\n    181 150 178 179 167 180 176 169 182 177 175 159 173", "output": "343772227"}]