Under construction

Still being built. Things may change

Ladybug

Time limit2sMemory limit512 MB

Summary
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 RR rows and C+1C+1 columns, for a total of R×(C+1)R \times (C+1) cells. NN of these cells contain obstacles, which the ladybug cannot visit or pass through. For convenience, the ii-th obstacle is in row xix_i and column yiy_i. Also, (r,c)(r, c) denotes the cell in row rr and column cc.

Albert releases the ladybug in the cell at row II, column 1. The ladybug moves according to these rules.

  • From a cell (r,c)(r, c) with r>1r > 1, the ladybug can move up to (r−1,c)(r-1, c). If r=1r = 1, the upward move is blocked, so it cannot move up.
  • From a cell (r,c)(r, c) with r<Rr < R, the ladybug can move down to (r+1,c)(r+1, c). If r=Rr = R, the downward move is blocked, so it cannot move down.
  • From a cell (r,c)(r, c), the ladybug can move right to (r,c+1)(r, c+1). If c=Cc = C (the move from column CC to column C+1C+1), 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 R=3R = 3, C=2C = 2, N=2N = 2, x=[3,3]x = [3, 3], y=[1,2]y = [1, 2], and I=1I = 1. The two cells marked "x" contain obstacles, and the ladybug starts at (I,1)=(1,1)(I, 1) = (1, 1). When the ladybug escapes by moving right from column CC, it reaches one of the cells marked "#" in the figure. For convenience, let FcF_c be the row number of the first cell the ladybug visits in column cc. It always holds that F1=IF_1 = I.

The figure below shows three escape routes that reach cell (1,3)(1, 3). 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 F=[1,2,1]F = [1, 2, 1].
  • Route 2: A -> B -> A -> B -> D -> C -> D -> C -> escape. Here F=[1,2,1]F = [1, 2, 1].
  • Route 3: A -> B -> A -> B -> A -> C -> D -> C -> escape. Here F=[1,1,1]F = [1, 1, 1].

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 FF. Route 3 can be told apart from them, because it has the same visited cells but a different F2F_2.

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 SS be the set of visited cells.

  • Top row, left to right:
    • S={(1,1),(1,2),(1,3)}S = \{ (1, 1), (1, 2), (1, 3) \}, F=[1,1,1]F = [1, 1, 1]. For example, (1, 1) -> (1, 2) -> (1, 3).
    • S={(1,1),(1,2),(2,2),(1,3)}S = \{ (1, 1), (1, 2), (2, 2), (1, 3) \}, F=[1,1,1]F = [1, 1, 1]. For example, (1, 1) -> (1, 2) -> (2, 2) -> (1, 2) -> (1, 3).
    • S={(1,1),(2,1),(1,2),(1,3)}S = \{ (1, 1), (2, 1), (1, 2), (1, 3) \}, F=[1,1,1]F = [1, 1, 1]. For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (1, 3).
    • S={(1,1),(2,1),(1,2),(2,2),(1,3)}S = \{ (1, 1), (2, 1), (1, 2), (2, 2), (1, 3) \}, F=[1,1,1]F = [1, 1, 1]. For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (2, 2) -> (1, 2) -> (1, 3).
    • S={(1,1),(2,1),(1,2),(2,2),(1,3)}S = \{ (1, 1), (2, 1), (1, 2), (2, 2), (1, 3) \}, F=[1,2,1]F = [1, 2, 1]. For example, (1, 1) -> (2, 1) -> (2, 2) -> (1, 2) -> (1, 3).
  • Bottom row, left to right:
    • S={(1,1),(1,2),(2,2),(2,3)}S = \{ (1, 1), (1, 2), (2, 2), (2, 3) \}, F=[1,1,2]F = [1, 1, 2]. For example, (1, 1) -> (1, 2) -> (2, 2) -> (2, 3).
    • S={(1,1),(2,1),(2,2),(2,3)}S = \{ (1, 1), (2, 1), (2, 2), (2, 3) \}, F=[1,2,2]F = [1, 2, 2]. For example, (1, 1) -> (2, 1) -> (2, 2) -> (2, 3).
    • S={(1,1),(2,1),(1,2),(2,2),(2,3)}S = \{ (1, 1), (2, 1), (1, 2), (2, 2), (2, 3) \}, F=[1,1,2]F = [1, 1, 2]. For example, (1, 1) -> (2, 1) -> (1, 1) -> (1, 2) -> (2, 2) -> (2, 3).
    • S={(1,1),(2,1),(1,2),(2,2),(2,3)}S = \{ (1, 1), (2, 1), (1, 2), (2, 2), (2, 3) \}, F=[1,2,2]F = [1, 2, 2]. 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 SS and have the same first-visit values FF. Albert wants the number of distinguishable escapes that reach cell (r,C+1)(r, C+1), denoted ArA_r, because the exit cell matters. For the maze above, A=[5,4,0]A = [5, 4, 0].

Given RR, CC, NN, II, and the arrays xx and yy, compute A1,A2,…,ARA_1, A_2, \dots, A_R.

Input

The first line contains the number of test cases TT.

The first line of each test case contains RR, CC, NN, and II, separated by spaces. Each of the next NN lines contains two integers separated by a space. The two integers on the ii-th line are the position xi,yix_i, y_i of the ii-th obstacle.

Output

For each test case, print the answers A1,A2,…,ARA_1, A_2, \dots, A_R on one line, separated by spaces. The values can be very large, so print each one modulo 109+710^9 + 7.

Constraints

  • 1≤T≤301 \le T \le 30
  • 1≤R≤201 \le R \le 20
  • 1≤C≤1061 \le C \le 10^6
  • 1≤N≤2501 \le N \le 250
  • 1≤I≤R1 \le I \le R
  • For every ii with 1≤i≤N1 \le i \le N:
    • 1≤xi≤R1 \le x_i \le R
    • 1≤yi≤C1 \le y_i \le C
    • (xi,yi)≠(I,1)(x_i, y_i) \neq (I, 1) (no obstacle is placed on the starting cell)
    • (xi,yi)(x_i, y_i) is unique

Examples1

  1. Example 1

    Input
    7
    3 3 2 2
    2 2
    3 3
    10 1 2 5
    3 1
    8 1
    10 2 2 5
    3 1
    8 1
    10 3 2 5
    3 1
    8 1
    5 5 3 3
    2 2
    3 3
    4 4
    3 2 2 1
    3 1
    3 2
    3 2 0 1
    
    Expected output
    4 2 0
    0 0 0 3 6 4 2 0 0 0
    85 170 255 340 392 378 320 240 160 80
    13442 25949 36586 44418 48510 48290 43912 36014 25476 13178
    360 576 648 648 648
    5 4 0
    14 16 10