Permutation Intersections

0

0 votes
Dynamic Programming, Inclusion Exclusion, Inclusion exclusion principle, Math, Medium, approved
Problem

You are given two horizontal rows of points. Each row has N points. You are also given a permutation P of .

The point in the top row has label i and has coordinates .
The point in the bottom row has label and has coordinates .

You will now draw N paths connecting the point in the top row to the point in the bottom row where . Denote this path as . The paths do not necessarily have to be straight lines.
The paths need to satisfy the following conditions:

  • Each pair of paths intersects at most once
  • Each path stays within the bounding box of all the points.
  • No three paths intersect at a single point.

The Intersection Sequence of is the ordered sequence such that

  • and intersects
  • Intersection point of and occurs before intersection point of and while moving from the top point to the bottom point of

Following figure shows the intersection sequences for
way 1 way 2

After drawing all the paths, you record the intersection sequence for each individual path. You are now wondering, what is the number of lists of intersection sequences you can write down. Two lists are considered different if there exists a path which has different intersection sequences in the two lists. As this number can get large, output it modulo .

Input Format:

The first line will contain the number of test casesT.
Each test case can be described with two lines.
The first line will contain a single integer N.
The second line will contain N space separated integers .

Output Format:

Output T numbers, the answers to each problem.

Constraints

For all subtasks:


will be a permutation of .

File 1 (50 pts)

File 3 (50 pts)

Sample Input
5
3
1 2 3
3
3 2 1
2
1 2
1
1
5
5 3 2 1 4
Sample Output
1
2
1
1
8
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

The second sample is illustrated in the statement.

Editor Image

?