atcoder#ABC268G. [ABC268G] Random Student ID

[ABC268G] Random Student ID

Score : 600600 points

Problem Statement

Takahashi Elementary School has NN new students. For i=1,2,,Ni = 1, 2, \ldots, N, the name of the ii-th new student is SiS_i (which is a string consisting of lowercase English letters). The names of the NN new students are distinct.

The NN students will be assigned a student ID 1,2,3,,N1, 2, 3, \ldots, N in ascending lexicographical order of their names. However, instead of the ordinary order of lowercase English letters where a is the minimum and z is the maximum, we use the following order:

  • First, Principal Takahashi chooses a string PP from the 26!26! permutations of the string abcdefghijklmnopqrstuvwxyz of length 2626, uniformly at random.
  • The lowercase English characters that occur earlier in PP are considered smaller.

For each of the NN students, find the expected value, modulo 998244353998244353, of the student ID assigned (see Notes).

What is the lexicographical order?

A string S=S1S2SSS = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T=T1T2TTT = T_1T_2\ldots T_{|T|} if one of the following 1. and 2. holds. Here, S|S| and T|T| denote the lengths of SS and TT, respectively.

  1. S<T|S| \lt |T| and S1S2SS=T1T2TSS_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
  2. There exists an integer 1imin{S,T}1 \leq i \leq \min\lbrace |S|, |T| \rbrace satisfying the following two conditions:
    • S1S2Si1=T1T2Ti1S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
    • SiS_i is a smaller character than TiT_i.

Notes

We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the value is represented as PQ\frac{P}{Q} by two coprime integers PP and QQ, we can prove that there is a unique integer RR such that R×QP(mod998244353)R \times Q \equiv P\pmod{998244353} and 0R<9982443530 \leq R \lt 998244353. Find such RR.

Constraints

  • 2N2 \leq N
  • NN is an integer.
  • SiS_i is a string of length at least 11 consisting of lowercase English letters.
  • The sum of lengths of the given strings is at most 5×1055 \times 10^5.
  • ijSiSji \neq j \Rightarrow S_i \neq S_j

Input

Input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print NN lines. For each i=1,2,,Ni = 1, 2, \ldots, N, the ii-th line should contain the expected value, modulo 998244353998244353, of the student ID assigned to Student ii.

3
a
aa
ab
1
499122179
499122179

The expected value of the student ID assigned to Student 11 is 11; the expected values of the student ID assigned to Student 22 and 33 are 52\frac{5}{2}.

Note that the answer should be printed modulo 998244353998244353. For example, the sought expected value for Student 22 and 33 is 52\frac{5}{2}, and we have 2×4991221795(mod998244353)2 \times 499122179 \equiv 5\pmod{998244353}, so 499122179499122179 should be printed.

3
a
aa
aaa
1
2
3