#abc242e. E - (∀x∀)

E - (∀x∀)

Score : 500500 points

问题描述

对于 TT 个测试用例,解决以下问题。

给定一个整数 NN 和一个字符串 SS,求满足以下所有条件的字符串 XX 的个数,结果对 998244353998244353 取模。

  • XX 是一个长度为 NN 的由大写英文字母组成的字符串。
  • XX 是回文串。
  • 按字典序排列时,XSX \le S
    • X=SX=S 或者 XX 在字典序上小于 SS

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Solve the following problem for TT test cases.

Given an integer NN and a string SS, find the number of strings XX that satisfy all of the conditions below, modulo 998244353998244353.

  • XX is a string of length NN consisting of uppercase English letters.
  • XX is a palindrome.
  • XSX \le S in lexicographical order.
    • That is, X=SX=S or XX is lexicographically smaller than SS.

Constraints

  • 1T2500001 \le T \le 250000
  • NN is an integer between 11 and 10610^6 (inclusive).
  • In a single input, the sum of NN over the test cases is at most 10610^6.
  • SS is a string of length NN consisting of uppercase English letters.

Input

Input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

case2\mathrm{case}_2

\vdots

caseT\mathrm{case}_T

Here, casei\mathrm{case}_i represents the ii-th test case.

Each test case is in the following format:

NN

SS

Output

Print TT lines. The ii-th line should contain the answer for the ii-th test case as an integer.

Sample Input 1

5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW

Sample Output 1

24
29
212370247
36523399
231364016

This input contains five test cases.

Test case #1:
The 2424 strings satisfying the conditions are AAA,, ABA,, ACA,...,,..., AXA.

Test case #2:
SS may not be a palindrome.

Test case #3:
Be sure to find the count modulo 998244353998244353.

update @ 2024/3/10 10:25:36