#KEYENCE2019D. Double Landscape

Double Landscape

Score : 500500 points

Problem Statement

Consider writing each of the integers from 11 to N×MN \times M in a grid with NN rows and MM 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 ii-th row (1iN)(1 \leq i \leq N) is AiA_i.
  • The largest among the values in the jj-th column (1jM)(1 \leq j \leq M) is BjB_j.

For him, find the number of ways to write the numbers under these conditions, modulo 109+710^9 + 7.

Constraints

  • 1N10001 \leq N \leq 1000
  • 1M10001 \leq M \leq 1000
  • 1AiN×M1 \leq A_i \leq N \times M
  • 1BjN×M1 \leq B_j \leq N \times M
  • AiA_i and BjB_j are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 A2A_2 ...... ANA_{N}

B1B_1 B2B_2 ...... BMB_{M}

Output

Print the number of ways to write the numbers under the conditions, modulo 109+710^9 + 7.

2 2
4 3
3 4
2

(A1,A2)=(4,3)(A_1, A_2) = (4, 3) and (B1,B2)=(3,4)(B_1, B_2) = (3, 4). In this case, there are two ways to write the numbers, as follows:

  • 11 in (1,1)(1, 1), 44 in (1,2)(1, 2), 33 in (2,1)(2, 1) and 22 in (2,2)(2, 2).
  • 22 in (1,1)(1, 1), 44 in (1,2)(1, 2), 33 in (2,1)(2, 1) and 11 in (2,2)(2, 2).

Here, (i,j)(i, j) denotes the square at the ii-th row and the jj-th column.

3 3
5 9 7
3 6 9
0

Since there is no way to write the numbers under the condition, 00 should be printed.

2 2
4 4
4 4
0
14 13
158 167 181 147 178 151 179 182 176 169 180 129 175 168
181 150 178 179 167 180 176 169 182 177 175 159 173
343772227