#P10764. [BalticOI 2024] Wall
[BalticOI 2024] Wall
Background
Translated from BalticOI 2024 Day2 T3.
Problem Description
You want to build a wall consisting of segments. Each segment can have height or . For every possible wall height sequence , you want to compute the total amount of trapped water, and then sum this value over all possible sequences.
For example, the figure below shows an example with and wall heights . Its actual water levels are .

For a position , the water level after rain must satisfy that there exist two indices such that and , and is as large as possible. Then the trapped water amount at position is .
Output the sum of trapped water over all possible cases, modulo .
Input Format
The first line contains an integer .
The second line contains integers .
The third line contains integers .
Output Format
Output the sum of trapped water over all possible wall configurations, modulo .
4
1 1 1 1
2 2 2 2
6
10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
21116
Hint
For sample 1, the case has trapped water amount . The cases , , , and each have trapped water amount .
| Subtask ID | Special Property | Points |
|---|---|---|
| and | ||
| and | ||
| No special property |
Constraints for all testdata: , , and for each , .
Translated by ChatGPT 5