Tick, Tock
Contest: Codeforces Round 836 (Div. 2)
Difficulty rating: 2500
Tags: combinatorics, dfs and similar, dsu, graphs

Tannhaus, the clockmaker in the town of Winden, makes mysterious clocks that measure time in $$$h$$$ hours numbered from $$$0$$$ to $$$h-1$$$. One day, he decided to make a puzzle with these clocks.

The puzzle consists of an $$$n \times m$$$ grid of clocks, and each clock always displays some hour exactly (that is, it doesn't lie between two hours). In one move, he can choose any row or column and shift all clocks in that row or column one hour forward$$$^\dagger$$$.

The grid of clocks is called solvable if it is possible to make all the clocks display the same time.

While building his puzzle, Tannhaus suddenly got worried that it might not be possible to make the grid solvable. Some cells of the grid have clocks already displaying a certain initial time, while the rest of the cells are empty.

Given the partially completed grid of clocks, find the number of ways$$$^\ddagger$$$ to assign clocks in the empty cells so that the grid is solvable. The answer can be enormous, so compute it modulo $$$10^9 + 7$$$.

$$$^\dagger$$$ If a clock currently displays hour $$$t$$$ and is shifted one hour forward, then the clock will instead display hour $$$(t+1) \bmod h$$$.

$$$^\ddagger$$$ Two assignments are different if there exists some cell with a clock that displays a different time in both arrangements.

Input format:
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases.

The first line of each test case consists of 3 integers $$$n$$$, $$$m$$$, and $$$h$$$ ($$$1 \leq n, m \leq 2 \cdot 10^5$$$; $$$1 \leq h \leq 10^9$$$) — the number of rows in the grid, the number of columns in the grid, and the number of the hours in the day respectively.

The next $$$n$$$ lines each contain $$$m$$$ integers, describing the clock grid. The integer $$$x$$$ ($$$-1 \leq x < h$$$) in the $$$i$$$-th row and the $$$j$$$-th column represents the initial hour of the corresponding clock, or if $$$x = -1$$$, an empty cell.

It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output the number of ways to assign clocks in the empty cells so that the grid is solvable. The answer can be huge, so output it modulo $$$10^9 + 7$$$.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
For the first sample, this is a possible configuration for the clocks: 103032

This is solvable since we can:

1. Move the middle column forward one hour.
2. Move the third column forward one hour.
3. Move the third column forward one hour.
4. Move the second row forward one hour.

For the second test case, it can be shown that there are no possible solvable clock configurations.

Example input:
5
2 3 4
1 0 -1
-1 -1 2
2 2 10
1 2
3 5
4 5 1024
1 -1 -1 -1 -1
-1 -1 -1 1000 -1
-1 -1 -1 -1 69
420 -1 -1 999 -1
3 3 3
-1 -1 1
2 -1 1
2 -1 2
3 3 3
1 -1 2
-1 0 -1
-1 1 0

Example output:
4
0
73741817
0
1

Editorial:
Notice that a relationship between two clocks with assigned values on the grid on different rows but the same column, that is, $$$g_{x, z}$$$ and $$$g_{y, z}$$$, can be represented as $$$g_{y, z} \equiv g_{x, z} + d \pmod{h}$$$, where $$$0 \le d < h$$$. Now, for every $$$1 \le i \le m$$$, $$$g_{y, i} \equiv g_{x, i} + d \pmod{h}$$$. Using these relationships, we can create a weighted directed graph using our rows as nodes.
Obviously, no solutions exist if there are discrepancies in the graph $$$\pmod{h}$$$, no solution exists. Now, for each connected component, if there is an assigned value in one of the rows it contains, we can determine all of the other values for that column in the connected component. We can merge different connected components $$$i, j$$$ by choosing a common difference $$$d_{i, j}$$$ for these components. This needs to be done (connected components - 1) times, and there are $$$h$$$ different ways to choose a common difference when combining different components, resulting in $$$h^{\text{connected components} - 1}$$$ different ways to combine all components into one connected component.
This leaves us with columns that are fully empty, i.e., they consist $$$\textit{only}$$$ of unassigned clocks. As all rows are in one connected component at this point, assigning a clock in one empty column results in all other clocks in that column becoming assigned values too. There are $$$h^{\text{empty columns}}$$$ different ways to assign clocks to these empty columns.
Thus, overall, our solution is $$$h^{\text{connected components} + \text{empty columns} - 1}$$$.