CF BUDDY
← Problems·

1402A · Fancy Fence

1800 · *special, data structures, dsu

Problem: Everybody knows that Balázs has the fanciest fence in the whole town. It's built up from NN fancy sections. The sections are rectangles standing closely next to each other on the ground. The iith section has integer height hih_i and integer width wiw_i. We are looking for fancy rectangles on this fancy fence. A rectangle is fancy if:

  • its sides are either horizontal or vertical and have integer lengths
  • the distance between the rectangle and the ground is integer
  • the distance between the rectangle and the left side of the first section is integer
  • it's lying completely on sections

Input Format: The first line contains NN (1N1051\leq N \leq 10^{5}) – the number of sections. The second line contains NN space-separated integers, the iith number is hih_i (1hi1091 \leq h_i \leq 10^{9}). The third line contains NN space-separated integers, the iith number is wiw_i (1wi1091 \leq w_i \leq 10^{9}).

Output Format: You should print a single integer, the number of fancy rectangles modulo 109+710^9+7. So the output range is 0,1,2,,109+60,1,2,\ldots, 10^9+6.

Note: Scoring: SubtaskPointsConstraints10sample212N50andhi50andwi=1for alli313hi=1orhi=2for alli415allhiare equal515hihi+1for alliN1618N1000727no additional constraints\begin{array}{|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & 0 & \text{sample}\\ \hline 2 & 12 & N \leq 50 \: \text{and} \: h_i \leq 50 \: \text{and} \: w_i = 1 \: \text{for all} \: i \\ \hline 3 & 13 & h_i = 1 \: \text{or} \: h_i = 2 \: \text{for all} \: i \\ \hline 4 & 15 & \text{all} \: h_i \: \text{are equal} \\ \hline 5 & 15 & h_i \leq h_{i+1} \: \text{for all} \: i \leq N-1 \\ \hline 6 & 18 & N \leq 1000\\ \hline 7 & 27 & \text{no additional constraints}\\ \hline \end{array}

The fence looks like this:

There are 5 fancy rectangles of shape:

There are 3 fancy rectangles of shape:

There is 1 fancy rectangle of shape:

There are 2 fancy rectangles of shape:

There is 1 fancy rectangle of shape:

Sample Cases

Case 1

Input

2
1 2
1 2

Output

12

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback