You are playing a game called "Remove Stones". There are piles of stones lined up in a row, the -th pile has stones in it, your task is to remove all the stones through the following operations:
- Select a pile of stones and remove at least stones;
- Select a contiguous interval () which satisfies , remove exactly 1 stone from each pile in the interval.
You can perform the above two operations any number of times in any order until you can no longer perform the operations. If you can remove all the stones, you win.
You have stones secretly hidden in the beginning, you must put these stones into a certain pile or in some piles of stones before the game starts. You can choose any configuration of putting in the extra stones. For each pile, there is a range that the number of stones in it must lie in, specifically, each can be chosen from any integer in the range . Find the number of winning configurations modulo .
Two configurations are different if and only if there exists at least one () such that differs in the two initial configurations. Here, "initial configurations" refer to the configuration before putting the stones into the piles.
Input Specifications
The first line is an integer , the number of subcases in the data.
For each test case, there are two integers and in the first line, representing the number of piles of stones and the number of stones added, respectively, and the next lines, each with two non-negative integers and , represent the range of the initial number of stones in each pile of stones.
Output Specifications
For each test case, output a line with an integer denoting the number of winning initial positions modulo .
Sample inputs and outputs can be found here.
Sample Input 1
1
4 1
0 1
0 1
0 1
0 1
Sample Output 1
14
Explanation for Sample 1
There are possible initial positions, there are only 2 losing configurations,
Examples 2-4
See stone/stone(2-4).in and stone/stone(2-4).out
Constraints
- For all test cases, , , , .
- Test case 1-3: , , .
- Test case 4-5: , .
- Test case 6-8: .
- Test case 9-11: .
- Test case 12-13: .
- Test case 14-15: .
- Test case 16-20: no additional constraints.
Comments