#P9838. 挑战 NPC IV
挑战 NPC IV
Background
If only everything were as easy as an NPC problem.
Problem Description
Little A wants to write a love poem for Little B. He has come up with sentences, and the elegance of each sentence is , respectively. Little A needs to combine them in some order to form a complete poem. In other words, Little A needs to reorder these sentences to form a permutation of ; the elegance of the -th line is the elegance of the original -th sentence, that is, .
However, since Little A is an OIer, his writing quality is not very stable. In fact, he will greatly overestimate how elegant his sentences are. If a sentence has elegance in Little A's eyes, then Little B thinks its elegance is the position of the lowest bit in the binary representation of . Here, Little B considers the lowest bit position to be , the next lowest to be , and so on. That is, the elegance in Little B's eyes is .
Little A knows that after Little B gets the poem, she will only choose a segment of it to read, and the elegance she feels is the sum of the lines she reads. Specifically, if the poem has lines, then Little B will pick an interval among all intervals in with length , and feel an elegance of . To measure a poem's elegance, Little A defines the poem's total elegance as the sum of the elegance Little B feels in all cases.
In principle, the arrangement with the maximum total elegance must be the best. Unfortunately, after Little A's precise calculations, he found that only when he chooses the love poem whose total elegance is exactly the -th smallest will he be most likely to end up with Little B. So Little A wants to know: among the essentially different poems, what is the possible total elegance of the -th smallest one. Two poems are essentially the same if and only if the original permutation is the same.
Little A discovered that this is an NPC problem, so he had to ask you for help. Since the total elegance is too large, you only need to output the answer modulo .
In particular, Little A wrote groups of sentences, so you need to answer his queries separately.
Input Format
Read from standard input.
The first line contains a positive integer , representing the number of groups of sentences.
For each group of testdata, there is only one line containing two positive integers describing Little A's query.
Output Format
Write to standard output.
For each group of sentences, output one integer per line, representing the total elegance of the -th smallest poem modulo .
2
3 2
3 6
13
14
5
4 1
4 10
4 16
4 20
4 24
32
34
36
36
38
10
1000000000000000000 1000000000000000000
1145141919810 19260817998244353
15 131413141314
36 93930322810121243
172 354354645654567654
666 233
1048576 2147483648
1000000007 1000000009
99824 44353
10 1
36226088
846277092
1096
12356
1239174
70731494
274614617
511280969
625722816
330
Hint
Sample 1 Explanation
For example, when , the elegance of each line in Little B's eyes is . Then:
- When , , the sum is .
- When , , the sum is .
- When , , the sum is .
- When , , the sum is .
- When , , the sum is .
- When , , the sum is .
So the total elegance of is .
For all permutations , sorting their total elegance from small to large gives . Therefore, when and , the answers are and , respectively.
Sample 2
See the attached and .
Sample 3
See the attached and .
Constraints
The time limit is different for different test points. Specifically, the time limit for each point is .
| Test Point ID | |||
|---|---|---|---|
For of the testdata, it is guaranteed that , , and .
Translated by ChatGPT 5