Keep It Cool
Time limit1sMemory limit512 MB
Count permutations of all pairs (a, b) with a < b that satisfy the betweenness rule for every triple and m given order constraints, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Consider the set of all pairs where and satisfy . A permutation of all these pairs is called balanced if, for any , , and with , the pair lies between the pairs and in the permutation. That is, the three pairs appear either in the order , or in the order .
There are also additional restrictions. The -th restriction requires the pair to appear before the pair in the permutation. Count the balanced permutations that satisfy all restrictions.
Input
The first line contains two integers and (, ). Each of the following lines contains four integers , , , and (, ). The pairs and are distinct.
Output
Print the number of balanced permutations where appears before for every , modulo the prime .
Hint
In the second sample, the valid permutations are and .