Under construction

Still being built. Things may change

Robot

Time limit1sMemory limit512 MB

Summary
Count the height assignments within each pillar's range so that, for every start pillar, the two robots' pass counts differ by at most 2, modulo 1e9+7.
Level

Medium7 of 10

Topics
Dynamic programming, Divide and conquer, Combinatorics
Solved
No attempts yet

Problem

Two robots, PP and QQ, take part in a series of experiments. There are nn pillars in a row, numbered 1 to nn, and the height of pillar ii is a positive integer hih_i. Each robot moves only to a neighboring pillar. A robot on pillar ii can only try to move to pillar i−1i-1 or i+1i+1. In each experiment, you choose a starting pillar ss and place both robots on it. Then the robots move by the rules below. Robot PP always moves left, but it cannot move onto a pillar higher than pillar ss. Precisely, PP stops at pillar ll (l≤sl \le s) if and only if l=1l = 1 or hl−1>hsh_{l-1} > h_s, and hj≤hsh_j \le h_s for all l≤j≤sl \le j \le s. Robot QQ always moves right, but it can only move onto pillars shorter than pillar ss. Precisely, QQ stops at pillar rr (r≥sr \ge s) if and only if r=nr = n or hr+1≥hsh_{r+1} \ge h_s, and hj<hsh_j < h_s for all s<j≤rs < j \le r. The height of each pillar ii can be any integer from AiA_i to BiB_i, inclusive. For every choice of starting pillar ss, the absolute difference between the number of pillars that PP and QQ pass through must be at most 2. Count the height assignments that satisfy this for every ss. Two assignments differ if some pillar kk has a different height in them. Output the count modulo 109+710^9+7.

Input

The first line contains nn. Each of the next nn lines contains two integers AiA_i and BiB_i.

Output

Print one integer on one line, the answer modulo 109+710^9+7.

Constraints

1≤n≤3001 \le n \le 300 and 1≤Ai≤Bi≤1091 \le A_i \le B_i \le 10^9.

Examples1

  1. Example 1

    Input
    5
    3 3
    2 2
    3 4
    2 2
    3 3
    
    Expected output
    1