Codeforces Round 329 (Div. 2) |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

dp

matrices

*2400

No tag edit access

The problem statement has recently been changed. View the changes.

×
E. Strange Calculation and Cats

time limit per test

4 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputGosha's universe is a table consisting of *n* rows and *m* columns. Both the rows and columns are numbered with consecutive integers starting with 1. We will use (*r*, *c*) to denote a cell located in the row *r* and column *c*.

Gosha is often invited somewhere. Every time he gets an invitation, he first calculates the number of ways to get to this place, and only then he goes. Gosha's house is located in the cell (1, 1).

At any moment of time, Gosha moves from the cell he is currently located in to a cell adjacent to it (two cells are adjacent if they share a common side). Of course, the movement is possible only if such a cell exists, i.e. Gosha will not go beyond the boundaries of the table. Thus, from the cell (*r*, *c*) he is able to make a move to one of the cells (*r* - 1, *c*), (*r*, *c* - 1), (*r* + 1, *c*), (*r*, *c* + 1). Also, Ghosha can skip a move and stay in the current cell (*r*, *c*).

Besides the love of strange calculations, Gosha is allergic to cats, so he never goes to the cell that has a cat in it. Gosha knows exactly where and when he will be invited and the schedule of cats travelling along the table. Formally, he has *q* records, the *i*-th of them has one of the following forms:

- 1,
*x*_{i},*y*_{i},*t*_{i}— Gosha is invited to come to cell (*x*_{i},*y*_{i}) at the moment of time*t*_{i}. It is guaranteed that there is no cat inside cell (*x*_{i},*y*_{i}) at this moment of time. - 2,
*x*_{i},*y*_{i},*t*_{i}— at the moment*t*_{i}a cat appears in cell (*x*_{i},*y*_{i}). It is guaranteed that no other cat is located in this cell (*x*_{i},*y*_{i}) at that moment of time. - 3,
*x*_{i},*y*_{i},*t*_{i}— at the moment*t*_{i}a cat leaves cell (*x*_{i},*y*_{i}). It is guaranteed that there is cat located in the cell (*x*_{i},*y*_{i}).

Gosha plans to accept only one invitation, but he has not yet decided, which particular one. In order to make this decision, he asks you to calculate for each of the invitations *i* the number of ways to get to the cell (*x*_{i}, *y*_{i}) at the moment *t*_{i}. For every invitation, assume that Gosha he starts moving from cell (1, 1) at the moment 1.

Moving between two neighboring cells takes Gosha exactly one unit of tim. In particular, this means that Gosha can come into the cell only if a cat sitting in it leaves the moment when Gosha begins his movement from the neighboring cell, and if none of the cats comes to the cell at the time when Gosha is in it.

Two ways to go from cell (1, 1) to cell (*x*, *y*) at time *t* are considered distinct if for at least one moment of time from 1 to *t* Gosha's positions are distinct for the two ways at this moment. Note, that during this travel Gosha is allowed to visit both (1, 1) and (*x*, *y*) multiple times. Since the number of ways can be quite large, print it modulo 10^{9} + 7.

Input

The first line of the input contains three positive integers *n*, *m* and *q* (1 ≤ *n*·*m* ≤ 20, 1 ≤ *q* ≤ 10 000) — the number of rows and columns in the table and the number of events respectively.

Next *q* lines describe the events, each description contains four integers *tp*_{i}, *x*_{i}, *y*_{i} and *t*_{i} (1 ≤ *tp* ≤ 3, 1 ≤ *x* ≤ *n*, 1 ≤ *y* ≤ *m*, 2 ≤ *t* ≤ 10^{9}) — the type of the event (1 if Gosha gets an invitation, 2 if a cat comes to the cell and 3 if a cat leaves the cell), the coordinates of the cell where the action takes place and the moment of time at which the action takes place respectively.

It is guaranteed that the queries are given in the chronological order, i.e. *t*_{i} < *t*_{i + 1}.

Output

For each invitation *i* (that is, *tp*_{i} = 1) calculate the number of ways to get to cell (*x*_{i}, *y*_{i}) at the moment of time *t*_{i}. Respond to the invitations chronologically, that is, in the order they appear in the input.

Examples

Input

1 3 3

2 1 2 3

3 1 2 5

1 1 1 7

Output

5

Input

3 3 3

2 2 2 2

1 3 3 5

1 3 3 7

Output

2

42

Input

4 5 5

2 2 5 3

2 2 4 6

3 2 4 9

1 4 4 13

1 4 4 15

Output

490902

10598759

Note

Explanation of the first sample. Each picture specifies the number of ways to arrive at the cell at the appropriate time. (X stands for a cell blocked at this particular moment of time)

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/01/2023 18:26:46 (j1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|