Editorial
Write combinations as , and define the value as when is outside . All combination values are computed modulo .
Case
Diameter at most means that every selected pair must communicate directly.
Fix a selected position that is the leftmost among the minimum-height selected positions. Every other selected position must satisfy the following conditions.
- If , then and communicate directly.
- If , then and communicate directly.
An equal-height position on the left is forbidden because is the leftmost minimum. One equal-height position on the right may still be selected.
Any subset of these candidate positions forms a clique together with . For example, if and communicates directly with , then is larger than every height strictly between and . Therefore and also communicate directly. The other relative orders are proved similarly.
The candidates to the right form a chain. First move to the nearest position on the right whose height is at least . Afterwards, repeatedly move to the nearest strictly greater position on the right. The candidates to the left are obtained by repeatedly moving to the previous strictly greater position.
Nearest greater-or-equal and nearest strictly greater positions are found with monotone stacks. The chain lengths are then computed by simple recurrences.
If candidate positions are available for a fixed , they contribute
Every valid installation is counted exactly once by its leftmost minimum-height relay.
Case
Let be the maximum selected height, and let the selected positions of height be
List all positions whose height is at least in increasing order. The positions must be consecutive in this list. Otherwise, an unselected mountain of height at least lies between two selected sides and blocks every possible edge across it, so the graph is disconnected.
Let be the previous position before in this list, and let be the next position after . Use and when the corresponding position does not exist. Define
All lower selected relays must lie between and . The values , , and count candidates in the left outer region, the internal regions between maximum relays, and the right outer region.
The maximum relays form the path . A relay in an internal gap is adjacent to the neighboring maximum relays. A relay in the left outer region can attach only to , and a relay in the right outer region can attach only to .
Let and indicate whether at least one relay is selected from the left and right outer regions. Since , distances of at most inside one region require no special handling. The relevant longest path has length
which must not exceed .
Therefore, after fixing the maximum relays, the number of ways to choose the remaining relays is as follows.
- If , both outer regions may be used:
- If , the two outer regions may not both be nonempty:
- If , neither outer region may be used:
- If , the choice is impossible.
It remains to enumerate every possible block of maximum relays. Process mountains in decreasing order of height and maintain an ordered set containing positions whose height is at least the current height. Insert all positions of one equal-height group simultaneously. In every consecutive run of the current height in the ordered set, enumerate all contiguous subarrays of length at most .
Each position is the left endpoint of at most enumerated blocks. The time complexity per test case is
and the memory complexity is . The case takes time.
Solution written by GPT5.6