#include using namespace std; #define REP(i,n) for(int i=0;i P; const ll MOD=998244353; ll c[100010]; string b[100010],a[100010]; int main(void){ ll i,j; cin.tie(0); ios_base::sync_with_stdio(false); ll N; string T; cin >> N >> T; ll n=T.size(); for(i=1;i<=N;i++){ string s; cin >> s; ll m=s.size(); if(s!="~"){ for(j=0;j<=m-n;j++) if(T==s.substr(j,n)){ c[i]++; c[i]%=MOD; } if(m>=n-1){ b[i]=s.substr(0,n-1); a[i]=s.substr(m-n+1,m); }else{ b[i]=s; a[i]=s; } }else{ ll J,K; cin >> J >> K; b[i]=b[J]; a[i]=a[K]; string x=a[J]; x+=b[K]; ll p=x.size(),q=0; if(p>=n){ for(j=0;j<=p-n;j++) if(T==x.substr(j,n)) q++; c[i]=c[J]+c[K]+q; c[i]%=MOD; }else{ c[i]=c[J]+c[K]; c[i]%=MOD; } } if(i==N) cout << c[N] << endl; } return 0; }