Universal Cup Judging System

Universal Cup

時間限制: 2.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓
统计

There is a grid with $n$ rows and $m$ columns. Each cell of the grid has an integer in it, where $a_{i,j}$ indicates the integer in the cell located at the $i$-th row and the $j$-th column.

Let $(i, j)$ be the cell located at the $i$-th row and the $j$-th column. You now start from $(1, 1)$ and need to reach $(n, m)$. When you are in cell $(i, j)$, you can either move to its right cell $(i, j + 1)$ if $j < m$ or move to its bottom cell $(i + 1, j)$ if $i < n$.

Let $\mathbb{S}$ be the set consisting of integers in each cell on your path, including $a_{1,1}$ and $a_{n,m}$. The value of a path is defined as the number of elements in $\mathbb{S}$ (recall that sets do not contain duplicated elements). For all possible paths, calculate the sum of their values.

Input

There are multiple test cases. The first line of the input contains an integer $T$ ($1 \le T \le 10^3$) indicating the number of test cases. For each test case:

The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$, $1 \le n \times m \le 10^5$) indicating the number of rows and columns of the grid.

For the following $n$ lines, the $i$-th line contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($1 \le a_{i,j} \le n \times m$) where $a_{i,j}$ indicates the integer in cell $(i, j)$.

It’s guaranteed that the sum of $n \times m$ of all test cases will not exceed $10^5$.

Output

For each test case, output one line containing one integer indicating the sum of values of all possible paths. As the answer might be large, output it modulo $998\,244\,353$.

Examples

Input 1

3
2 3
5 2 1
1 5 5
1 1
1
2 3
3 3 3
3 3 3

Output 1

7
1
3

Note

For the first sample test case, there are 3 possible paths.

  • The first path is $(1, 1) \to (1, 2) \to (1, 3) \to (2, 3)$. $\mathbb{S} = \{1, 2, 5\}$.
  • The second path is $(1, 1) \to (1, 2) \to (2, 2) \to (2, 3)$. $\mathbb{S} = \{2, 5\}$.
  • The third path is $(1, 1) \to (2, 1) \to (2, 2) \to (2, 3)$. $\mathbb{S} = \{1, 5\}$.

So the answer is $3 + 2 + 2 = 7$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.