Problem I
Wall
Languages
en
is
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.
Since attacks from the middle of the lake aren’t as likely as attacks from the nearby shore, the wall does not need to form a closed loop. Instead, it will be in the shape of a straight line, and consist of $N$ segments arranged from one end to the other and numbered $1$ to $N$. What still remains is picking the height of each segment.
The chief architect has already picked two possible heights for each segment. He decided that the height of the $i$-th segment will be either $a_ i$ or $b_ i$. Thus, $2^ N$ possibilities remain.
Having the castle on a small island in a lake has its difficulties. During stormy weather, the castle can get flooded. In such cases, water collects above wall segments if there are higher segments to each side of them, preventing the water from draining.
For a particular choice of the segments’ heights, we are interested in the amount of water that will collect on the wall after a heavy storm. This is illustrated in the following figure, where the segments’ heights from left to right are $4, 2, 1, 8, 6, 2, 7, 1, 2, 3$ and the level of water at each position is $4, 4, 4, 8, 7, 7, 7, 3, 3, 3$.
![\includegraphics[width=0.5\textwidth ]{wall_illustration_v1.jpg}](/problems/wall/file/statement/en/img-0001.jpg)
Formally, for every $i = 1, 2, \dotsc , N$, the level of water at position $i$ is at least $h$ if and only if there exist integers $l$ and $r$ such that $l \leq i$ and $i \leq r$ and the segment heights at positions $l$ and $r$ are at least $h$. In particular, the level of water at positions $1$ and $N$ is always equal to the heights of the corresponding segments, and the level of water at any position is always at least as large as the height of the corresponding segment. The amount of water that collects at position $i$ is equal to the difference between the level of water and the height of the segment. The total amount of water collected is just the sum of collected water at positions $1, 2, \dotsc , N$.
Task
Your task is to compute the sum, over all $2^ N$ possible walls, of the total amount of collected water. You should output the answer modulo $10^9 + 7$.
Input
The first line of the input contains one integer number $N$.
The second line of the input contains $N$ integers $a_1, a_2, \dotsc , a_ N$.
The third line of the input contains $N$ integers $b_1, b_2, \dotsc , b_ N$.
Output
Your program should output a single integer, the sum of the total amount of water collected over all $2^ N$ possible walls modulo $10^9 + 7$.
Constraints
-
$1 \leq N \leq 5 \cdot 10^5$.
-
$1 \leq a_ i, b_ i \leq 10^9$ and $a_ i \neq b_ i$ (for all $1 \leq i \leq N$)
Subtasks
No. |
Points |
Additional constraints |
1 |
8 |
$N \leq 20$. |
2 |
17 |
$N \leq 100$ and for all segments, $a_ i, b_ i \leq 1\, 000$. |
3 |
19 |
$N \leq 10\, 000$ and for all segments, $a_ i, b_ i \leq 1\, 000$. |
4 |
14 |
$N \leq 10\, 000$. |
5 |
12 |
For all segments, $a_ i, b_ i \leq 2$. |
6 |
30 |
No additional constraints. |
Examples
There is a single possible wall where two units of water are collected:
-
$2 \, 1 \, 1 \, 2$
and four possible walls where one unit of water is collected:
-
$1 \, 2 \, 1 \, 2$,
-
$2 \, 1 \, 2 \, 1$,
-
$2 \, 1 \, 2 \, 2$,
-
$2 \, 2 \, 1 \, 2$.
Sample Input 1 | Sample Output 1 |
---|---|
4 1 1 1 1 2 2 2 2 |
6 |
Sample Input 2 | Sample Output 2 |
---|---|
10 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 |
21116 |