Number of good permutations
Time limit2sMemory limit512 MB
Count permutations of 1..N that respect given fixed positions and contain at least one pair i<j with P[i]>j and P[j]>i, modulo 2000000011.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
You are given two arrays and of size and an integer . Write a program that counts the permutations of length that satisfy all three conditions below. Arrays and permutations are numbered from 1.
- is a sequence in which each of the numbers 1 through appears exactly once.
- At least one pair satisfies , and .
- holds for every .
The same value appears in more than once in some inputs. If no permutation meets all three conditions, the answer is 0.
Input
The first line contains the number of test cases ().
The first line of each test case contains and (, ). The -th of the next lines contains and ().
Output
For each test case, print the number of permutations that satisfy the conditions, modulo , on its own line.
Hint
For with no fixed value, is the only permutation that satisfies the conditions.