atcoder#AGC053E. [AGC053E] More Peaks More Fun

[AGC053E] More Peaks More Fun

Score : 14001400 points

Problem Statement

We have 2N2N cards and NN boxes. The cards are numbered 11 through 2N2N, and the boxes are numbered 11 through NN. Each box contains two cards; Box ii contains Card AiA_i and Card BiB_i.

Find, modulo (109+7)(10^9+7), the number of ways to arrange the NN boxes in a row that satisfies the following condition.

  • Consider getting a sequence of 2N2N cards as follows: for each box, from left to right, open it and append the two cards in it to the end of the sequence in an order we like. In this way, it is possible to get a sequence P1,P2,,P2NP_1,P_2,\ldots, P_{2N} with N1N-1 peaks.

Here, a peak in a sequence P1,P2,,P2NP_1,P_2,\ldots, P_{2N} is an integer jj satisfying 2j<2N2 \leq j < 2N such that Pj1<PjP_{j-1} < P_j and Pj>Pj+1P_j > P_{j+1}.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1Ai,Bi2N1 \leq A_i, B_i \leq 2N
  • A1,,AN,B1,,BNA_1,\ldots,A_N,B_1,\ldots,B_N are pairwise distinct.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

::

ANA_N BNB_N

Output

Print the answer.

3
1 3
2 4
5 6
4

For example, if we arrange Boxes 1,2,31, 2, 3 in this order, we can arrange the cards as follows to get a sequence PP with 22 peaks:

  • first, arrange the cards in Box 11 in the order 1,31, 3;
  • next, append the cards in Box 22 at the end in the order 2,42, 4;
  • lastly, append the cards in Box 33 at the end in the order 6,56, 5.

Here, we have P=(1,3,2,4,6,5)P=(1,3,2,4,6,5) with peaks j=2,5j=2,5.

6
5 8
7 2
1 3
11 6
4 12
9 10
492
10
20 15
8 5
6 7
4 9
13 1
11 14
10 17
19 12
3 16
2 18
1411200