Lost Table

시간 제한2초메모리 제한2048 MB

요약
주어진 각 행의 최댓값과 각 열의 최댓값을 만족하는 n×m 양의 정수 표의 개수를 10^9+7로 나눈 나머지를 구하고, 불가능하면 0을 출력한다.
난이도

보통10점 중 7점

유형
조합론, 정렬, 수학, 구현
정답자
아직 제출이 없습니다

문제

Er-Tostik had a table of size n×mn \times m with positive integers. Aldar-Kose decided to prank Er-Tostik and stole the table, but told Er-Tostik the maximum value in each row and column. Aldar-Kose will only return the table if Er-Tostik can tell how many different tables can have these maximum values. As their number can be very large, Aldar-Kose only asks to find this value modulo 109+710^9 + 7. Help Er-Tostik to get his table back.

입력

The first line of input contains two integers nn and mm (1≤n,m≤2⋅1051 \leq n, m \leq 2 \cdot 10^5): the dimensions of the table.

The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1091 \leq a\_i \leq 10^9): the maximum values in each row.

The third line contains mm integers b_1,b_2,…,b_mb\_1, b\_2, \ldots, b\_m (1≤b_j≤1091 \leq b\_j \leq 10^9): the maximum values in each column.

출력

Output a line with a single integer: the number of different tables satisfying the conditions. Since the answer can be very large, output it modulo 109+710^9 + 7.

Note that, as Aldar-Kose is mischievous, the input might not be consistent with any table at all. In such case, naturally, the correct answer is 00.

예제5

  1. 예제 1

    입력
    3 3
    2 2 3
    2 3 3
    
    예상 출력
    89
    
  2. 예제 2

    입력
    1 1
    1
    2
    
    예상 출력
    0
    
  3. 예제 3

    입력
    5 5
    2 2 3 3 3
    2 2 2 3 3
    
    예상 출력
    49049891
    
  4. 예제 4

    입력
    12 13
    2 2 2 3 3 4 4 4 4 5 5 5
    2 3 3 3 3 4 5 5 5 5 5 5 5
    
    예상 출력
    808346164
    
  5. 예제 5

    입력
    2 3
    2 3
    3 1 5
    
    예상 출력
    0