wall — BOI contest
Task type: batch | Time limit: 5 s | Memory limit: 256 MB | Difficulty: 23
Algorithms: implementation, data structures, segment tree, coordinate compression

![BOI 2024 Logo](_page_0_Picture_0.jpeg)

# Flooding Wall

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.

![Illustration of water collecting on the wall](_page_0_Figure_10.jpeg)

Formally, for every $i = 1, 2, ..., 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 \le i$ and $i \le 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, ..., 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, ..., a_N$.

The third line of the input contains $N$ integers $b_1, b_2, ..., 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$.

## Examples

| Input          | Output | Explanation                                                                                                                                                                                                                   |
|----------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 4              | 6      | There is a single possible wall where two units of water are collected:                                                                                                                                                      |
| 1 1 1 1        |        | $\cdot$ 2 1 1 2                                                                                                                                                                                                            |
| 2 2 2 2        |        | and four possible walls where one unit of water is collected:                                                                                                                                                               |
|                |        | $\cdot$ 1 2 1 2,                                                                                                                                                                                                           |
|                |        | $\cdot$ 2 1 2 1,                                                                                                                                                                                                           |
|                |        | $\cdot$ 2 1 2 2,                                                                                                                                                                                                           |
|                |        | $\cdot$ 2 2 1 2.                                                                                                                                                                                                           |
| 10             | 21116  |                                                                                                                                                                                                                               |
| 1 2 3 4 5 6 7 8 9 10 |        |                                                                                                                                                                                                                               |
| 10 9 8 7 6 5 4 3 2 1 |        |                                                                                                                                                                                                                               |

## Constraints

$1 \le N \le 5 \cdot 10^5$.

$1 \le a_i, b_i \le 10^9$ and $a_i \ne b_i$ (for all $1 \le i \le N$).

## Subtasks

| No. | Points | Additional constraints                        |
|-----|--------|---------------------------------------------|
| 1   | 8      | $N \le 20$.                                 |
| 2   | 17     | $N \le 100$ and for all segments, $a_i, b_i \le 1000$. |
| 3   | 19     | $N \le 10\ 000$ and for all segments, $a_i, b_i \le 1000$.|
| 4   | 14     | $N \le 10\ 000$.                            |
| 5   | 12     | For all segments, $a_i, b_i \le 2$.         |
| 6   | 30     | No additional constraints.                  |

Starter code: