atcoder#ARC138F. [ARC138F] KD Tree

[ARC138F] KD Tree

Score : 10001000 points

Problem Statement

There are NN points in a plane, the ii-th (1iN1 \leq i \leq N) of which is (i,Pi)(i,P_i). Here, (P1,P2,,PN)(P_1,P_2,\cdots,P_N) is a permutation of (1,2,,N)(1,2,\cdots,N).

You can arrange a non-empty set ss of points, which is a recursive operation defined as follows.

  • If ss contains exactly one point, arranging it creates a sequence composed of just that point.
  • If ss contains two or more points, arranging it creates a sequence composed of those points by doing one of the following two operations.- Choose any integer xx and divide ss into two: the set aa of points whose XX-coordinates are less than xx and the set bb of the other points. Here, neither aa nor bb should be empty. Create a sequence by concatenating the sequence obtained by arranging aa and the sequence obtained by arranging bb in this order.
    • Choose any integer yy and divide ss into two: the set aa of points whose YY-coordinates are less than yy and the set bb of the other points. Here, neither aa nor bb should be empty. Create a sequence by concatenating the sequence obtained by arranging aa and the sequence obtained by arranging bb in this order.
  • Choose any integer xx and divide ss into two: the set aa of points whose XX-coordinates are less than xx and the set bb of the other points. Here, neither aa nor bb should be empty. Create a sequence by concatenating the sequence obtained by arranging aa and the sequence obtained by arranging bb in this order.
  • Choose any integer yy and divide ss into two: the set aa of points whose YY-coordinates are less than yy and the set bb of the other points. Here, neither aa nor bb should be empty. Create a sequence by concatenating the sequence obtained by arranging aa and the sequence obtained by arranging bb in this order.

Find the number, modulo (109+7)(10^9+7), of sequences that can be obtained by arranging the set of points {(1,P1),(2,P2),,(N,PN)}\{(1,P_1),(2,P_2),\cdots,(N,P_N)\}.

Constraints

  • 1N301 \leq N \leq 30
  • (P1,P2,,PN)(P_1,P_2,\cdots,P_N) is a permutation of (1,2,,N)(1,2,\cdots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \cdots PNP_N

Output

Print the answer.

3
3 1 2
3

The following three sequences can be obtained.

  • ((1,3),(2,1),(3,2))((1,3),(2,1),(3,2))
  • ((2,1),(3,2),(1,3))((2,1),(3,2),(1,3))
  • ((2,1),(1,3),(3,2))((2,1),(1,3),(3,2))

For example, the sequence ((2,1),(1,3),(3,2))((2,1),(1,3),(3,2)) can be obtained as follows.

  • Arrange the set {(1,3),(2,1),(3,2)}\{(1,3),(2,1),(3,2)\} by dividing it to the set of points whose YY-coordinates are less than 22 (={(2,1)}=\{(2,1)\}) and the set of the other points (={(1,3),(3,2)}=\{(1,3),(3,2)\}).- Arrange the set {(2,1)}\{(2,1)\} to obtain the sequence ((2,1))((2,1)).
    • Arrange the set {(1,3),(3,2)}\{(1,3),(3,2)\} by dividing it to the set of points whose XX-coordinates are less than 33 and the set of the other points.- Arrange the set {(1,3)}\{(1,3)\} to obtain the sequence ((1,3))((1,3)).
    • Arrange the set {(3,2)}\{(3,2)\} to obtain the sequence ((3,2))((3,2)).
    • Concatenate the resulting two sequences to obtain the sequence ((1,3),(3,2))((1,3),(3,2)).
    • Concatenate the resulting two sequences to obtain the sequence ((2,1),(1,3),(3,2))((2,1),(1,3),(3,2)).
  • Arrange the set {(2,1)}\{(2,1)\} to obtain the sequence ((2,1))((2,1)).
  • Arrange the set {(1,3),(3,2)}\{(1,3),(3,2)\} by dividing it to the set of points whose XX-coordinates are less than 33 and the set of the other points.
  • Arrange the set {(1,3)}\{(1,3)\} to obtain the sequence ((1,3))((1,3)).
  • Arrange the set {(3,2)}\{(3,2)\} to obtain the sequence ((3,2))((3,2)).
  • Concatenate the resulting two sequences to obtain the sequence ((1,3),(3,2))((1,3),(3,2)).
  • Concatenate the resulting two sequences to obtain the sequence ((2,1),(1,3),(3,2))((2,1),(1,3),(3,2)).
5
1 2 3 4 5
1
10
3 6 4 8 7 2 10 5 9 1
1332
30
7 11 8 26 4 13 28 5 14 1 16 27 10 2 23 25 17 6 3 18 24 15 9 22 21 29 12 20 19 30
641915679