Under construction

Still being built. Things may change

Race of robots

Time limit1sMemory limit512 MB

Summary
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 n×mn \times m divided into nmnm unit cells. Rows are numbered from 11 to nn, and columns are numbered from 11 to mm. We denote by (i,j)(i, j) the cell in row ii and column jj. There can be a barrier between each pair of adjacent cells.

There are nn robots. Before the race, they are located in the cells of the first column: (1,1),(2,1),…,(n,1)(1, 1), (2, 1), \ldots, (n, 1). The finish is the cell (n,m)(n, m). When the race begins, each robot moves toward the finish. From cell (i,j)(i, j), a robot can move either to (i+1,j)(i + 1, j) or to (i,j+1)(i, j + 1). 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 nn and mm, the size of the field (1≤n≤51 \le n \le 5, 1≤m≤151 \le m \le 15).

The second line contains a single integer kk, the number of pairs of cells for which Ivan knows the barrier status (0≤k≤2nm−n−m0 \le k \le 2nm - n - m).

Each of the next kk lines contains five integers r1r_1, c1c_1, r2r_2, c2c_2 and ww (1≤r1≤r2≤n1 \le r_1 \le r_2 \le n, 1≤c1≤c2≤m1 \le c_1 \le c_2 \le m, 0≤w≤10 \le w \le 1). If w=0w = 0, there is no barrier between cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2). If w=1w = 1, 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.

Examples1

  1. Example 1

    Input
    2 2
    1
    1 1 1 2 0
    
    Expected output
    5