#include #include #include #include #include using namespace std; vector ZAlgorithm(const string& S) { int n = S.size(); vector res(n, 0); res[0] = n; int pbegin = 1, pend = 1; for (int i = 1; i < n; i++) { if (i < pend) { res[i] = res[i - pbegin]; if (pend > i + res[i]) continue; res[i] = pend - i; pbegin = i; } else { pbegin = pend = i; } while (true) { if (pend == n) break; if (S[pend] != S[res[i]]) break; res[i]++; pend++; } } return res; } int countT(const string& s, const string& t){ auto z = ZAlgorithm(t + s); int ans = 0; for(size_t i=t.size(); i= (int)t.size()) ans++; return ans; } const int MOD = 998244353; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; string T; cin >> T; vector dp(N); vector pre(N); vector suf(N); vector tooshort(N, false); for(int i=0; i> key; if(key == "~"){ int j, k; cin >> j >> k; j--; k--; if(tooshort[j]){ if(tooshort[k]){ string q = pre[j] + pre[k]; if(q.size() < T.size()){ pre[i] = q; tooshort[i] = true; } else{ pre[i] = q.substr(0, T.size()-1); suf[i] = q.substr(q.size() - T.size() + 1); dp[i] = countT(q, T); } } else{ string q = pre[j] + pre[k]; pre[i] = q.substr(0, T.size()-1); suf[i] = suf[k]; dp[i] = (countT(q, T) + dp[k]) % MOD; } } else{ if(tooshort[k]){ string q = suf[j] + pre[k]; pre[i] = pre[j]; suf[i] = q.substr(q.size() - T.size() + 1); dp[i] = (countT(q, T) + dp[j]) % MOD; } else{ pre[i] = pre[j]; suf[i] = suf[k]; dp[i] = (dp[j] + dp[k] + countT(suf[j] + pre[k], T)) % MOD; } } } else{ if(key.size() < T.size()){ pre[i] = key; tooshort[i] = true; } else{ pre[i] = key.substr(0, T.size() - 1); suf[i] = key.substr(key.size() - T.size() + 1); dp[i] = countT(key, T); tooshort[i] = false; } } } cout << dp[N-1] << '\n'; return 0; }