Balanced Domino Placements
Contest: Codeforces Global Round 5
Difficulty rating: 2600
Tags: combinatorics, dp

Consider a square grid with $$$h$$$ rows and $$$w$$$ columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino.

Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells covered by two different dominoes. In other words, every row and column may contain no covered cells, one covered cell, or two covered cells that belong to the same domino.

You are given a perfectly balanced placement of dominoes on a grid. Find the number of ways to place zero or more extra dominoes on this grid to keep the placement perfectly balanced. Output this number modulo $$$998\,244\,353$$$.

Input format:
The first line contains three integers $$$h$$$, $$$w$$$, and $$$n$$$ ($$$1 \le h, w \le 3600$$$; $$$0 \le n \le 2400$$$), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from $$$1$$$ to $$$h$$$, and the columns are numbered from $$$1$$$ to $$$w$$$.

Each of the next $$$n$$$ lines contains four integers $$$r_{i, 1}, c_{i, 1}, r_{i, 2}, c_{i, 2}$$$ ($$$1 \le r_{i, 1} \le r_{i, 2} \le h$$$; $$$1 \le c_{i, 1} \le c_{i, 2} \le w$$$), denoting the row id and the column id of the cells covered by the $$$i$$$-th domino. Cells $$$(r_{i, 1}, c_{i, 1})$$$ and $$$(r_{i, 2}, c_{i, 2})$$$ are distinct and share a common side.

The given domino placement is perfectly balanced.

Output format:
Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first example, the initial grid looks like this:

Here are $$$8$$$ ways to place zero or more extra dominoes to keep the placement perfectly balanced:

In the second example, the initial grid looks like this:

No extra dominoes can be placed here.

Example input:
5 7 2
3 1 3 2
4 4 4 5

Example output:
8

Example input:
5 4 2
1 2 2 2
4 3 4 4

Example output:
1

Editorial:
Suppose we're going to place $$$d_h$$$ extra horizontal dominoes and $$$d_v$$$ extra vertical ones. Consider all rows of the grid, and mark them with $$$0$$$ if it's empty and with $$$1$$$ if it already has a covered cell. Do the same for columns.
Now, let's find the number of ways $$$R$$$ to pick $$$d_h$$$ rows marked with $$$0$$$, and also $$$d_v$$$ pairs of neighboring rows marked with $$$0$$$, so that these sets of rows don't intersect. Similarly, let's find the number of ways $$$C$$$ to pick $$$d_h$$$ pairs of columns marked with $$$0$$$, and also $$$d_v$$$ columns marked with $$$0$$$. Then, the number of ways to place exactly $$$d_h$$$ horizontal dominoes and $$$d_v$$$ vertical ones is $$$R \cdot C \cdot d_h! \cdot d_v!$$$.
To find $$$R$$$, let's use DP: let $$$f(i, j)$$$ be the number of ways to pick $$$j$$$ pairs of neighboring rows out of the first $$$i$$$ rows. Then, $$$f(i, j) = f(i - 1, j)$$$ if any of rows $$$i$$$ or $$$i-1$$$ is marked with $$$1$$$, and $$$f(i, j) = f(i - 1, j) + f(i - 2, j - 1)$$$ if both are marked with $$$0$$$. It follows that $$$R = f(h, d_v) * \binom{h - 2d_v}{d_h}$$$. $$$C$$$ can be found similarly.
The complexity of this solution is $$$O((R+C)^2)$$$.