#P15659. [ICPC 2025 Jakarta R] Mugen E

[ICPC 2025 Jakarta R] Mugen E

题目描述

You are given KK strings S1,S2,,SKS_1, S_2, \dots, S_K of lowercase Latin letters. You are also given a string TT.

Define a function f(n)f(n), where nn is an integer, as the following.

  • Initially, you have an empty string UU.
  • For nn times, choose a string uniformly at random among S1,S2,,SKS_1, S_2, \dots, S_K, and append that string to UU.
  • Let EnE_n be the expected value of the number of occurrences of TT in UU as a substring. Then, f(n)=En/nf(n) = E_n / n.

It can be proven that limnf(n)\lim\limits_{n \to \infty} f(n) exists and can be written as a rational number p/qp / q. Find pq1mod998  244  353pq^{-1} \bmod 998\;244\;353.

输入格式

The first line contains the string TT (1T50001 \leq |T| \leq 5000) consisting of lowercase Latin letters.

The second line contains an integer KK (1K5000)1 \leq K \leq 5000).

Each of the next KK lines contains SiS_i (1Si50001 \leq |S_i| \leq 5000) consisting of lowercase Latin letters.

The sum of Si|S_i| does not exceed 1  000  0001\;000\;000.

输出格式

Output the limit pq1mod998  244  353pq^{-1} \bmod 998\;244\;353.

ab
2
a
b
748683265
ab
4
aaa
abab
baba
bbb
1

提示

Explanation of Sample 1:\textit{Explanation of Sample 1:} It can be shown that limnf(n)=14\lim\limits_{n \to \infty} f(n) = \frac{1}{4}.

Explanation of Sample 2:\textit{Explanation of Sample 2:} It can be shown that limnf(n)=1\lim\limits_{n \to \infty} f(n) = 1.