Flooding Wall
Time limit5sMemory limit1024 MB
Each of N wall segments has one of two candidate heights, so sum the water trapped over all 2^N walls, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Sorting, Prefix sum, Math
- Solved
- No attempts yet
Problem
It's the 14th century and construction of the Trakai Island Castle is to begin soon. The first task on the chief architect's list is to plan the construction of the main castle wall.
Building a wall that can protect the castle from any possible attack is quite tricky. To ensure the safety of the castle garrison, the chief architect has already narrowed the design space somewhat.
Attacks from the middle of the lake are less likely than attacks from the nearby shore, so the wall does not need to form a closed loop. Instead, it is a straight line of segments arranged from one end to the other and numbered to . What remains is to pick the height of each segment.
The chief architect has already picked two possible heights for each segment. The height of the -th segment is either or . That leaves possibilities.
A castle on a small island in a lake has its difficulties. During stormy weather, the castle can get flooded. Water collects above a wall segment when there are higher segments on both sides of it, and it cannot drain.
Input
The first line contains one integer .
The second line contains integers .
The third line contains integers .
Output
Print a single integer: the sum of the total water collected over all possible walls, modulo .
For a fixed choice of heights, the water level at position is at least if and only if there exist integers such that the heights at positions and are both at least . The water level at positions and always equals their segment heights, and the water level at any position is at least the height of its segment. The water collected at position is the water level minus the segment height, and the total is the sum over positions .
Constraints
.
and for all .