#P5320. [BJOI2019] 勘破神机
[BJOI2019] 勘破神机
Description
The strategist of the Geocast Legion, Black Robe, learned information about the Divine Staff from spies infiltrating the upper ranks of the elves. He is very interested in the ancient mysterious power contained in the Arcane Gems. He seized several Arcane Gems and ordered you, the chief scientist of the Geocast Legion, to lead your researchers to crack them with full effort. After a month of hard work, your team finally deciphered the internal energy structures of the “2-type” Arcane Gem and the “3-type” Arcane Gem.
These two kinds of structures share some similarities. Each gem contains reaction cores. Each core of a “2-type” Arcane Gem can be seen as a grid, and each core of a “3-type” Arcane Gem can be seen as a grid. (Note that and may be different for different gems.)
When the divine reaction happens, each core is automatically filled with divine particles.
Formally, each divine particle can be viewed as a domino placed horizontally or vertically. A core is considered filled if every cell of the grid is covered by exactly one domino. If, between two filling plans, there exists at least one domino whose position or orientation is different, then the two plans are considered different.
For example, there are different ways to fill a grid, and different ways to fill a grid.

If the filling plans of the cores in an Arcane Gem are all different from each other, they will combine into a powerful spell. Black Robe wants to know how many different spells a gem can produce. (For two spell combinations, if for every core filling plan in the first combination, one can find some core filling plan in the second combination such that and are exactly the same, then these two spell combinations are considered the same.)
For a “2-type” Arcane Gem with width and reaction cores, let the number of different spells be . For a “3-type” Arcane Gem with width and reaction cores, let the number of different spells be . For example, , , and .
The Geocast Legion’s technology cannot precisely measure the core length , and can only determine an approximate range . You need to compute the average number of spells when the core length is within this interval, namely:
Let the final answer be in the form . Output , where is the multiplicative inverse of modulo .
Input Format
The first line contains two positive integers and , representing the number of test cases and the type of Arcane Gem (only or ).
The next lines each contain three positive integers , representing the range of core lengths and the number of cores.
Output Format
For each test case, if output ; if output .
5 2
2 4 2
1 10000 501
52501 233333333333 1
52501 233333333333 2
52501 233333333333 50
665496240
218802505
745517510
133015204
910014966
5 3
2 2 2
1 10000 501
52501 233333333333 1
52501 233333333333 2
52501 233333333333 50
3
900767573
52671648
600503426
678428567
Hint

For all testdata, it is guaranteed that is not a multiple of .
Because the computers in the contest were very slow, after the setter modified the testdata, all cases have .
Note: The Constraints of this problem are incorrect. The actual constraints satisfy .
Translated by ChatGPT 5
京公网安备 11011102002149号