Sprinklers

Given a permutation of N sprinklers, count axis-aligned integer rectangles whose every point lies both northeast of some sprinkler and southwest of another, modulo 1e9+7.

Hard8CombinatoricsDivide and conquerSegment treeMathNo attempts yetTime limit2sMemory limit512 MB

Problem

Farmer John's field is a (N1)×(N1)(N-1) \times (N-1) square. Its southwest corner is at (0,0)(0,0) and its northeast corner is at (N1,N1)(N-1,N-1). John wants to plant sweet corn in part of it.

At some integer coordinates there are double-headed sprinklers, and each one sprays both water and fertilizer. A sprinkler at (i,j)(i,j) waters the part of the field north and east of itself, and fertilizes the part south and west of itself. Formally, it waters every real coordinate (x,y)(x,y) with xix \geq i and yjy \geq j, and it fertilizes every real coordinate (x,y)(x,y) with xix \leq i and yjy \leq j.

John picks an axis-aligned rectangle inside the field whose four corners all have integer coordinates, and plants sweet corn in it. For the corn to grow, every point of the rectangle must be both watered and fertilized. The rectangle must also have area greater than 00, or there is no room for corn.

Help John count the rectangles in which he could grow sweet corn. This number can be large, so report it modulo 109+710^9 + 7.

Input

The first line contains one integer NN, the size of the field (1N1051 \leq N \leq 10^5).

Each of the next NN lines contains two space-separated integers ii and jj (0i,jN10 \leq i, j \leq N-1), meaning that a sprinkler sits at (i,j)(i,j).

Every column holds exactly one sprinkler and every row holds exactly one sprinkler. That is, no two sprinklers share an xx-coordinate, and no two sprinklers share a yy-coordinate.

Output

Print one line with the number of rectangles of area greater than 00 that are fully watered and fully fertilized, modulo 109+710^9 + 7.