#P10764. [BalticOI 2024] Wall

    ID: 12232 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP线段树2024BalticOI(波罗的海)

[BalticOI 2024] Wall

Problem Description

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 NN segments arranged from one end to the other and numbered 11 to NN. 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 aa or bb. Thus, 2N2^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,34, 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,34, 4, 4, 8, 7, 7, 7, 3, 3, 3.

Follow Luogu UID 911054 (https://www.luogu.com.cn/user/911054) plz!!

Formally, for every i=1,2,...,Ni = 1, 2,..., N, the level of water at position ii is at least hh if and only if there exist integers ll and rr such that lil \le i and iri \le r and the segment heights at positions ll and rr are at least hh. In particular, the level of water at positions 11 and NN 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 ii 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,...,N1, 2,..., N.

Task

Your task is to compute the sum, over all 2N2^N possible walls, of the total amount of collected water. You should output the answer modulo 109+710^9 + 7.

Input Format

The first line of the input contains one integer number NN.

The second line of the input contains NN integers aia_i.

The third line of the input contains NN integers bib_i.

Output Format

Your program should output a single integer, the sum of the total amount of water collected over all 2N2^N possible walls modulo 109+710^9 + 7.

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

Sample 11 Explanation

There is a single possible wall where two units of water are collected:

  • 2 1 1 22\ 1\ 1\ 2

and four possible walls where one unit of water is collected:

  • 1 2 1 21\ 2\ 1\ 2,
  • 2 1 2 12\ 1\ 2\ 1,
  • 2 1 2 22\ 1\ 2\ 2,
  • 2 2 1 22\ 2\ 1\ 2.

Constraints

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

1ai,bi1091\le a_i,b_i\le 10^9 and aibia_i\neq b_i (for all 1iN1\le i\le N).

Subtasks

No. Points Additional constraints
1 8 N20N\le 20.
2 17 N100N\le 100 and for all segments, ai,bi1000a_i , b_i \le 1 000.
3 19 N10 000N\le 10\ 000 and for all segments, ai,bi1000a_i , b_i \le 1 000.
4 14 N10 000N\le 10\ 000.
5 12 For all segments, ai,bi2a_i , b_i \le 2.
6 30 No additional constraints.