PivotOJ

Flooding Wall

시간 제한: 5000ms메모리 제한: 1024MB출처: BOI 2024BOJ 31973

문제

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 ii-th segment will be either aia_i or bib_i. 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 44, 22, 11, 88, 66, 22, 77, 11, 22, 33 and the level of water at each position is 44, 44, 44, 88, 77, 77, 77, 33, 33, 33.

Formally, for every i=1,2,,Ni = 1, 2, \dots , N, the level of water at position ii is at least hh if and only if there exist integers ll and rr such that l ≤ i and i ≤ 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, \dots , N.

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.

입력

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

The second line of the input contains NN integers a1,a2,,aNa_1 , a_2 , \dots , a_N.

The third line of the input contains NN integers b1,b2,,bNb_1 , b_2 , \dots , b_N.

출력

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.

예제

예제 1

입력
4
1 1 1 1
2 2 2 2
출력
6

예제 2

입력
10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
출력
21116
코드를 제출하려면 로그인하세요.