#P11959. 「ZHQOI R1」诗歌

「ZHQOI R1」诗歌

Description

Given a positive integer k k , the character set size for this problem is k k . We use positive integers i i to denote the i i -th character in the set.

Given a positive integer m m , a string S \mathcal{S} is defined as "harmonious" if and only if either:

  • The length of S \mathcal{S} is less than m+2 m + 2 ,or
  • After deleting any m m characters from S \mathcal{S} , there exists no contiguous palindromic substring of length greater than 11 in the resulting string.

You need to process q q queries. For each query, given a "harmonious" string T T of length m+2 m + 2 and a non-repeating character set U U , determine the number of strings of length n n that satisfy the following conditions:

  1. T T is a prefix of the string.
  2. The string is "harmonious".
  3. The last character of the string belongs to U U .

Output the answer modulo 998244353 998244353 . For all queries in a test case, k k and m m remain the same.

Note: It is guaranteed that km3k-m \ge 3 .

Input Format

The first line contains an integer c c representing the test case ID. In sample inputs, c=0 c = 0 .

The second line contains four integers q,k,m q, k, m , representing the number of queries, the character set size, and the constant used to define "harmonious" strings.

Each of the next q q lines describes a query, containing:

  • An integer n n ,
  • A sequence T T of m+2 m + 2 positive integers (the "harmonious" prefix),
  • An integer U |U| (the size of set U U ), followed by U |U| distinct integers denoting the elements of U U .

Output Format

For each query, output one integer per line representing the answer modulo 998244353 998244353 .

0
1 5 1
5 1 2 3 1 1
2
0
1 5 1
7 1 2 3 2 1 2
6
0
1 40 4
50 2 3 5 7 11 31 2 5 10

732767443
0
1 12 1
12 3 5 7 1 7
32390928

Hint

Sample 1 Explanation

The prefix is abc (encoded as 1 2 3), U={a} U = \{a\} , and the character set is {a,b,c,d,e}\{a,b,c,d,e\}. The valid strings of length 5 are:

abcda  
abcea  

Hence the answer is 2.

Sample 2 Explanation

The prefix is abc, U={a,b} U = \{a,b\} . Among valid strings of length 7, exactly 6 meet the conditions.

abceadb
abcdeab
abcedba
abcdeba
abcedab
abcdaeb

Hence the answer is 6.

Constraints

For all test cases: 1n1071 \leq n \leq 10^71q,m2×1031 \leq q,m \leq 2 \times 10^3m+3k109m + 3 \leq k\le 10^9U[1,k]U \subseteq [1,k]Ti[1,k]T_i \in [1,k]

Subtask q= q = n n \leq m= m = U= \sum \vert U \vert = Additional Constraints
1 700700 100100 700700 km=3 k-m=3
2 None
3 103 10^3 5×105 5 \times 10^5 2020 4×103 4 \times 10^3
4 2×103 2 \times 10^3 100100
5 106 10^6
6 2.5×105 2.5 \times 10^5 103 10^3 4×103 4 \times 10^3
7 5×105 5 \times 10^5 2×103 2 \times 10^3
8 2.5×105 2.5 \times 10^5 103 10^3 106 10^6
9 5×105 5 \times 10^5 2×103 2 \times 10^3
10 10710^7 2×106 2 \times 10^6