Depot

Time limit1sMemory limit128 MB

Summary
Given the final row placement produced by the depot insertion rule, count how many arrival orders of the containers could have produced it.
Level

Hard8 of 10

Topics
Dynamic programming, Combinatorics, Implementation, Brute force
Solved
No attempts yet

Problem

A Finnish high-technology company has a big rectangular depot. The depot has a worker and a manager. The sides of the depot, in order around it, are called left, top, right, and bottom. The depot floor is divided into equal-sized square cells by splitting it into rows and columns. The rows are numbered from the top with integers 1, 2, ..., and the columns are numbered from the left with integers 1, 2, ...

The depot holds containers that store invaluable technological devices. Each container has a distinct identification number and occupies one cell. The depot is so big that the total number of containers that will ever arrive is smaller than the number of rows and smaller than the number of columns. Containers are never removed from the depot, but from time to time a new container arrives. The entrance to the depot is at the top-left corner.

The worker arranges the containers near the top-left corner so that he can find them by their identification numbers, using the following method.

Let kk be the identification number of the next container to be inserted (container kk, for short). The worker scans the first row from the left and looks for the first container whose identification number is larger than kk. If no such container is found, container kk is placed immediately after the rightmost container already in that row. If such a container ll is found, container ll is replaced by container kk, and the displaced container ll is inserted into the next row using the same method. If the worker reaches a row that has no containers, the container is placed in the leftmost cell of that row.

Suppose containers 3, 4, 9, 2, 5, 1 arrived at the depot in this order. Then the placement of the containers is as follows.

1 4 5
2 9
3

The manager comes to the worker and they have the following dialogue.

Manager: Did container 5 arrive before container 4?

Worker: No, that is impossible.

Manager: Oh, so you can tell the arrival order of the containers from their placement.

Worker: Generally not. For instance, the containers now in the depot could have arrived in the order 3, 2, 1, 4, 9, 5, or in the order 3, 2, 1, 9, 4, 5, or in one of 14 other orders.

The manager, not wanting the worker to seem much smarter than he is, walks away. You are to help the manager: write a program that, given a container placement, computes how many arrival orders could have produced it.

Input

The first line contains one integer RR, the number of rows that contain containers. The following RR lines describe rows 1,...,R1, ..., R from the top. Each such line begins with an integer MM, the number of containers in that row, followed by MM integers: the identification numbers of the containers in that row from the left. Every container identification number II satisfies 1≤I≤501 \le I \le 50. If NN is the total number of containers in the depot, then 1≤N≤131 \le N \le 13.

Output

Print a single integer: the number of distinct arrival orders that could have produced the given placement.

Examples2

  1. Example 1

    Input
    3
    3 1 4 5
    2 2 9
    1 3
    
    Expected output
    16
    
  2. Example 2

    Input
    2
    2 1 2
    1 3
    
    Expected output
    2