Race of robots
Time limit1sMemory limit512 MB
Count the n by m grids that match Ivan's known barrier positions and in which every robot needs the same time to reach the finish, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
The annual race of robots takes place today in Berland. Ivan is one of its participants. Before the race starts, he wants to find out what the racing field looks like.
The racing field is a rectangle of size divided into unit cells. Rows are numbered from to , and columns are numbered from to . We denote by the cell in row and column . There can be a barrier between each pair of adjacent cells.
There are robots. Before the race, they are located in the cells of the first column: . The finish is the cell . When the race begins, each robot moves toward the finish. From cell , a robot can move either to or to . If there is no barrier between the two cells, the robot moves between them immediately. Otherwise, the move takes one second.
Each robot knows the locations of all barriers in advance and takes a path that needs the minimum time to reach the finish. Robots are very small, so any number of them may stay in the same cell. The field is called fair if all robots need the same time to reach the finish.
Ivan has seen the field and remembers some information about it. For some pairs of adjacent cells, he knows for certain whether there is a barrier between them. He is also sure that the field is fair. Ivan wants to know how many fields satisfy all of these conditions. Since the answer may be very large, output it modulo 998244353.
Input
The first line contains two integers and , the size of the field (, ).
The second line contains a single integer , the number of pairs of cells for which Ivan knows the barrier status ().
Each of the next lines contains five integers , , , and (, , ). If , there is no barrier between cells and . If , there is a barrier between them. The two cells are always adjacent, and no pair of cells is listed more than once.
Output
Output one integer, the number of fair fields that satisfy all of Ivan's conditions, modulo 998244353.
Hint
Illustration for the sample test. Solid lines denote a barrier, dashed lines denote its absence.
