Ladybug
Time limit2sMemory limit512 MB
Count the distinguishable escape routes from cell (I, 1) to the exit in each row, where two routes match if they visit the same cells and have the same first-visit rows per column.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Matrix, Bit manipulation
- Solved
- No attempts yet
Problem
Albert studies how ladybugs find their way. He builds a grid-shaped maze and records which cells the ladybug passes through on its way out. The maze has rows and columns, for a total of cells. of these cells contain obstacles, which the ladybug cannot visit or pass through. For convenience, the -th obstacle is in row and column . Also, denotes the cell in row and column .
Albert releases the ladybug in the cell at row , column 1. The ladybug moves according to these rules.
- From a cell with , the ladybug can move up to . If , the upward move is blocked, so it cannot move up.
- From a cell with , the ladybug can move down to . If , the downward move is blocked, so it cannot move down.
- From a cell , the ladybug can move right to . If (the move from column to column ), the ladybug has escaped the maze and the experiment ends.
- In short, apart from escaping from the last column, the ladybug can move freely up, down, and right, but it cannot move left.
The figure below shows the case , , , , , and . The two cells marked "x" contain obstacles, and the ladybug starts at . When the ladybug escapes by moving right from column , it reaches one of the cells marked "#" in the figure. For convenience, let be the row number of the first cell the ladybug visits in column . It always holds that .

The figure below shows three escape routes that reach cell . For convenience, the four cells the ladybug passes through other than the endpoints are labeled A, B, C, and D.
- Route 1: A -> B -> D -> C -> escape. Here .
- Route 2: A -> B -> A -> B -> D -> C -> D -> C -> escape. Here .
- Route 3: A -> B -> A -> B -> A -> C -> D -> C -> escape. Here .

Albert's apparatus can record which cells the ladybug visited and the first cell it visited in each column. It cannot record how many times a cell was visited or the order of the visits. So Routes 1 and 2 cannot be told apart, since both visit cells A, B, C, and D and have the same . Route 3 can be told apart from them, because it has the same visited cells but a different .
In the maze above, the ladybug can escape in 9 ways that the apparatus can tell apart, shown in the figure below. In the figure, "x" marks an obstacle and "*" marks the first cell visited in that column. For each escape, let be the set of visited cells.

- Top row, left to right:
- , . For example, (1, 1) -> (1, 2) -> (1, 3).
- , . For example, (1, 1) -> (1, 2) -> (2, 2) -> (1, 2) -> (1, 3).
- , . For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (1, 3).
- , . For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (2, 2) -> (1, 2) -> (1, 3).
- , . For example, (1, 1) -> (2, 1) -> (2, 2) -> (1, 2) -> (1, 3).
- Bottom row, left to right:
- , . For example, (1, 1) -> (1, 2) -> (2, 2) -> (2, 3).
- , . For example, (1, 1) -> (2, 1) -> (2, 2) -> (2, 3).
- , . For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (2, 2) -> (2, 3).
- , . For example, (1, 1) -> (2, 1) -> (2, 2) -> (1, 2) -> (2, 2) -> (2, 3).
These 9 are the only distinguishable ways for the ladybug to escape this maze.
Two escape routes are the same method if they visit the same set of cells and have the same first-visit values . Albert wants the number of distinguishable escapes that reach cell , denoted , because the exit cell matters. For the maze above, .
Given , , , , and the arrays and , compute .
Input
The first line contains the number of test cases .
The first line of each test case contains , , , and , separated by spaces. Each of the next lines contains two integers separated by a space. The two integers on the -th line are the position of the -th obstacle.
Output
For each test case, print the answers on one line, separated by spaces. The values can be very large, so print each one modulo .
Constraints
- For every with :
- (no obstacle is placed on the starting cell)
- is unique