Welcome to ICPCCamp 2017
Time limit1sMemory limit512 MB
Count the distinct team sets that the (X, Y)-rule can produce from n contest scoreboards and the final ranking, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Combinatorics, Greedy, Array
- Solved
- No attempts yet
Problem
ICPCCamp teams are often selected by a mysterious -rule described in a blog (?).
There are selection contests held to choose ICPCCamp team among teams labeled . The number of teams attending the -th contest is . The last contest, the -th, is EasyCamp-Final, which is very important, so always holds. The scoreboard of the -th contest is , which means team has rank in that contest.
The -rule works as follows. First, two non-negative integers and and a permutation of are chosen. Then the first distinct teams in the list are selected as the ICPCCamp team. In other words, the list starts with the first EasyCamp-Final teams, then the top teams of the first contests in the order given by , then the second teams of the first contests in the same order, and so on.
Bobo would like to know the number of possible sets of ICPCCamp team modulo , if he can choose , , and arbitrarily.
Wish you enjoy yourself in the upcoming World Finals!
Input
The input contains zero or more test cases and ends at end-of-file. Each test case is given as follows.
The first line contains two integers and (, ).
The -th of the following lines contains an integer followed by integers ().
The last line contains integers (). For each , the numbers are distinct.
The sum of and the sum of over all test cases do not exceed .
Output
For each test case, output one integer, the number of possible sets modulo .