#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; } b[i]=s.substr(0LL,min(n-1,m)); a[i]=s.substr(max(0LL,m-n+1),min(n-1,m)); }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(); ll 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; }