#P16117. [USTCPC 2026] Evil Counting Problem
[USTCPC 2026] Evil Counting Problem
Background
“Waa—! How can there be such a weird array!”
Kruskal-chan stared at the blackboard full of and , and felt completely overwhelmed.
“Senior, senior!” the junior tugged at her sleeve. “If I give you an interval, and the numbers inside can be rearranged freely, then how many rearrangements make the sum of products of all contiguous subsegments equal to exactly?”
Facing those sparkling eyes, Kruskal-chan could only bite the bullet and accept the challenge.
Sigh, today’s club activity seems not so peaceful again……
Problem Description
You are given an array of length , where each element is either .
You are also given a constant and queries. Each query specifies . You need to compute: assuming you may arbitrarily permute the elements whose indices are in , how many permutations make the sum of products of all non-empty subsegments of the new array (still of length ), i.e. , equal to . Output the result modulo .
Note: even if two different permutations produce exactly the same resulting array, they are still considered different permutations.
Input Format
This problem contains multiple test cases.
The first line contains an integer (), the number of test cases.
For each test case, the first line contains three integers: the array length (), the constant (), and the number of queries ().
The next line contains integers. The -th integer is , satisfying .
Then follow lines. Each line contains two integers. The two integers on the -th line are for the -th query ().
It is guaranteed that .
Output Format
Output lines, each containing one integer, the answer to the corresponding query.
2
5 -3 3
1 -1 -1 1 -1
1 4
2 5
3 3
3 6 1
1 -1 -1
1 3
8
12
0
0
Hint
For the first query of the first sample, the first four elements must be rearranged into or . There are permutations in total that satisfy the requirement.
Translated by ChatGPT 5