#P9554. 「CROI · R1」浣熊的溪石

「CROI · R1」浣熊的溪石

Background

Looking back to the past, the sky was clear, the sun was warm, and the breeze was gentle.
Creek stones, with different depths and heights, were scattered in the crystal-clear Mengfeng Creek like jade.
The endless fun memories of raccoons doing parkour on the water also began here...

Problem Description

As the sun rises and the moon sets, spring goes and autumn comes, the heights of the Mengfeng creek stones change in countless ways.
The thoughtful little raccoon CleverRaccoon wants to find out how many different height sequences these creek stones can form.

Now there are some non-negative integer sequences aa of length nn.

When n>1n>1, a1,ana_1,a_n take values in 0m0\sim m, and the other elements aia_i take values in 0m10\sim m-1, where 2in12\leq i \leq n-1.

When n=1n=1, a1a_1 takes values in 0m+10\sim m+1.

Definition: Sequences a,ba,b are considered the same if and only if either i{1,2,,n},ai=bi\forall i\in\{1,2,\dots ,n\},a_i=b_i or i{1,2,,n},ai=bni+1\forall i\in\{1,2,\dots ,n\},a_i=b_{n-i+1}.

Given n,mn,m, find the maximum number of different sequences. Output the answer modulo 998244353998244353.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

The next TT lines each contain two positive integers n,mn,m separated by spaces, with meanings as described above.

Output Format

Output TT lines. For each test case, output one positive integer, the answer modulo 998244353998244353.

3
1 3
2 2
6 3
5
6
666

Hint

Explanation #1

When n=1,m=3n=1,m=3, ai{0,1,2,3,4}a_i\in\{0,1,2,3,4\}, so there are 55 different height sequences in total.

When n=2,m=2n=2,m=2, there are 66 different height sequences in total, listed as follows:

Index a1=a_1= a2=a_2=
11 00 00
22 11
33 22
44 11 11
55 22
66 22

Constraints

This problem uses bundled Subtasks.

Subtask nn\leq mm\leq TT\leq Special Property Score
00 1010 None 1010
11 10310^3 11 10310^3 m=1m=1 55
22 33 1010 m=3m=3 1010
33 10310^3 11 None 1515
44 10610^6 100100 1010
55 10610^6 10610^6 2020
66 10910^9 10910^9 nn is odd 1010
77 nn is even
88 101810^{18} None

For 100%100\% of the testdata, it is guaranteed that 1n1018,1m109,1T1061\leq n\leq10^{18},1\leq m\leq10^9,1\leq T\leq10^6.

Translated by ChatGPT 5