Universal Cup Judging System

Universal Cup

Limite de temps : 2.0 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓
Statistiques

有一個 $n$ 列 $m$ 行的網格。每個格子中都有一個整數,其中 $a_{i,j}$ 表示位於第 $i$ 列、第 $j$ 行格子中的整數。

令 $(i, j)$ 為位於第 $i$ 列、第 $j$ 行的格子。你現在從 $(1, 1)$ 出發,需要到達 $(n, m)$。當你位於格子 $(i, j)$ 時,若 $j < m$,你可以移動到右側的格子 $(i, j + 1)$;若 $i < n$,你可以移動到下方的格子 $(i + 1, j)$。

令 $\mathbb{S}$ 為路徑上所有格子中整數所組成的集合,包含 $a_{1,1}$ 和 $a_{n,m}$。路徑的值定義為 $\mathbb{S}$ 中元素的個數(回想集合不包含重複元素)。請計算所有可能路徑的值的總和。

輸入格式

輸入包含多組測試資料。第一行包含一個整數 $T$ ($1 \le T \le 10^3$),表示測試資料的組數。對於每組測試資料:

第一行包含兩個整數 $n$ 和 $m$ ($1 \le n, m \le 10^5$, $1 \le n \times m \le 10^5$),表示網格的列數與行數。

接下來 $n$ 行,第 $i$ 行包含 $m$ 個整數 $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($1 \le a_{i,j} \le n \times m$),其中 $a_{i,j}$ 表示格子 $(i, j)$ 中的整數。

保證所有測試資料的 $n \times m$ 總和不超過 $10^5$。

輸出格式

對於每組測試資料,輸出一行包含一個整數,表示所有可能路徑的值的總和。由於答案可能很大,請輸出對 $998\,244\,353$ 取模後的結果。

範例

範例輸入 1

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

範例輸出 1

7
1
3

說明

對於第一個範例測試資料,共有 3 條可能的路徑。

  • 第一條路徑為 $(1, 1) \to (1, 2) \to (1, 3) \to (2, 3)$。$\mathbb{S} = \{1, 2, 5\}$。
  • 第二條路徑為 $(1, 1) \to (1, 2) \to (2, 2) \to (2, 3)$。$\mathbb{S} = \{2, 5\}$。
  • 第三條路徑為 $(1, 1) \to (2, 1) \to (2, 2) \to (2, 3)$。$\mathbb{S} = \{1, 5\}$。

因此答案為 $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.