The first line contains N,K,Q.
The next N lines each contain N bits (each 0 or 1). The j-th entry of the i-th line is 1 if there exists a door from room i to room j, and 0 if no such door exists.
This is followed by Q lines, each containing four integers bs, s, bt, t, denoting the starting button, starting room, final button, and final room respectively.
The number of sequences for each of the Q queries modulo 109+7 on separate lines.
6 3 8 010000 001000 000100 000010 000000 000001 1 1 1 1 3 3 1 1 1 1 3 3 1 1 1 5 2 1 1 5 1 1 2 5 3 1 3 5 2 6 2 6
The doors connect rooms 1→2, 2→3, 3→4, 4→5, and 6→6.
For the first query, Bessie must stop immediately after pressing the first button.
For the second query, the answer is clearly zero because there is no way to get to room 1 from room 3.
For the third query, Bessie's only option is to move from room 1 to room 2 to room 3 while pressing buttons 1, 2, and 3.
For the fourth query, Bessie's pattern of movement is fixed, and she has three possible sequences of button presses:
(1,2,3,2,1)
(1,2,1,3,1)
(1,3,1,2,1)
For the last query, Bessie has five possible sequences of button presses:
(2)
(2,3,2)
(2,3,1,2)
(2,1,3,2)
(2,1,3,1,2)
In test cases 4-7, K≤5 and (bs,s) is the same for all queries.
In test cases 8-11, bs=K−1 and bt=K for each query.
In test cases 12-15, N,K,Q≤20.
In test cases 16-23, there are no additional constraints.
Problem credits: Benjamin Qi