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: N, M = list(map(int, input().split()))
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  grid = [[0 for i in range(M)] for j in range(N)]
  maxnum = N * M
  count = 1
  gcount = 0
  tmp = 0
  tmp2 = 0
  while maxnum > 0:
      gcount = 0
      if maxnum in A and maxnum in B:
          for i in range(M):
              if grid[A.index(maxnum)][i] < 2:
                  grid[A.index(maxnum)][i] += 1
          for i in range(N):
              if grid[i][B.index(maxnum)] < 2:
                  grid[i][B.index(maxnum)] += 1
          grid[A.index(maxnum)][B.index(maxnum)] = 3
          maxnum -= 1
      elif A.count(maxnum) > 0:
          for i in range(M):
              if grid[A.index(maxnum)][i] == 1:
                  gcount += 1
                  tmp = i
          if gcount == 0:
              count = 0
              print(maxnum)
              break
          else:
              for i in range(M):
                  if grid[A.index(maxnum)][i] < 2:
                      grid[A.index(maxnum)][i] += 1
              grid[A.index(maxnum)][tmp] = 3
              count *= gcount
              maxnum -= 1
      elif B.count(maxnum) > 0:
          for i in range(N):
              if grid[i][B.index(maxnum)] == 1:
                  gcount += 1
                  tmp = i
          if gcount == 0:
              count = 0
              print(maxnum)
              break
          else:
              for i in range(N):
                  if grid[i][B.index(maxnum)] < 2:
                      grid[i][B.index(maxnum)] += 1
              count *= gcount
              grid[tmp][B.index(maxnum)] = 3
              maxnum -= 1
      else:
          for i in range(N):
              for j in range(M):
                  if grid[i][j] == 2:
                      gcount += 1
                      tmp = i
                      tmp2 = j
          if gcount == 0:
              count = 0
              print(maxnum)
              break
          else:
              count *= gcount
              grid[tmp][tmp2] = 3
              maxnum -= 1
  print(count % 1000000007)
  
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"}]