注释写得很清楚了

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//如果在不支持 avx2 的平台上将 avx2 换成 avx 或 SSE 之一
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
vector<int> vx;
inline void divide() {sort(vx.begin(),vx.end());vx.erase(unique(vx.begin(),vx.end()),vx.end());}
//inline int mp(int x) {return upper_bound(vx.begin(),vx.end(),x)-vx.begin();}
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
inline ll Lsqrt(ll x) { ll L = 1,R = 2e9;while(L + 1 < R){ll M = (L+R)/2;if(M*M <= x) L = M;else R = M;}return L;}
const int p = 998244353;
void solve()
{
    int n;
    cin>>n;
    vector<pair<string,pair<int,int>>> s(n);
    for(int i = 0; i < n; ++i) cin>>s[i].x, s[i].y.x = i;
    sort(s.begin(),s.end(),[&](auto A,auto B){return A.x.size() < B.x.size();});
    auto KMP = [&](string s, string t) -> int 
    {
        int n = s.size(), m = t.size();
        int cnt = 0;
        vector<int> nxt(m + 1);
        s = '-' + s;
        t = '-' + t;
        for (int i = 2, j = 0; i <= m; i++) 
        {
            while (j && t[i] != t[j + 1]) j = nxt[j];
            if (t[i] == t[j + 1]) j++;
            nxt[i] = j;
        }
        for (int i = 1, j = 0; i <= n; i++) 
        {
            while (j && s[i] != t[j + 1]) j = nxt[j];
            if (s[i] == t[j + 1]) j++;
            if (j == m) 
            {
                //cout << i - m + 1 << "\n"; // t 在 s 中出现的位置
                cnt ++;
                j = nxt[j];
            }
        }
        return cnt;
        //for(int i = 1; i <= m; ++i) cout<<nxt[i]<<' ';
    };
    int Minsz = s[0].x.size();
    //对于长度大于Minsz的ans一定为0
    //对于长度等于Minsz的字符串集合,若不是全相等,则ans = 0
    //若对于长度等于Minsz的字符串集合全相等,只需跑一次KMP
    int flag = n;
    bool ok = true;
    for(int i = 0; i < n; ++i)
    {
        if(s[i].x.size() != Minsz) {flag = i;break;}
        if(s[i].x != s[0].x) ok = false;
    }
    if(!ok) for(int i=0;i<n;++i) cout<<"0\n";
    else
    {
        ll ans = 1;
        for(int i = flag; i < n; ++i)
        {
            ans = ans * KMP(s[i].x, s[0].x) % p;
            if(!ans) break;
        }
        for(int i = 0; i < flag; ++i) s[i].y.y = ans;
        for(int i = flag; i < n; ++i) s[i].y.y = 0;
        sort(s.begin(),s.end(),[&](auto A,auto B){return A.y.x < B.y.x;});
        for(int i = 0; i < n; ++i) cout<<s[i].y.y<<'\n';
    }
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T = 1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
}