using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var t = ReadLine(); var list = new Pair[n]; var mod = 998_244_353; for (var i = 0; i < n; ++i) { var s = ReadLine(); if (s[0] == '~') { var c = s.Split(); var a = int.Parse(c[1]) - 1; var b = int.Parse(c[2]) - 1; if (list[a].IsShort && list[b].IsShort) { var u = list[a].Left.Concat(list[b].Left).ToArray(); if (u.Length < t.Length) { list[i] = new Pair(u.ToArray(), u.ToArray(), true, 0); } else { list[i] = new Pair(u.Take(t.Length - 1).ToArray(), u.Skip(u.Length - t.Length + 1).ToArray(), false, Count(t, u)); } } else if (list[a].IsShort) { var u = list[a].Left.Concat(list[b].Left).ToArray(); list[i] = new Pair(u.Take(t.Length - 1).ToArray(), list[b].Right, false, (list[b].Count + Count(t, u)) % mod); } else if (list[b].IsShort) { var u = list[a].Right.Concat(list[b].Left).ToArray(); list[i] = new Pair(list[a].Left, u.Skip(u.Length - t.Length + 1).ToArray(), false, (list[a].Count + Count(t, u)) % mod); } else { var u = list[a].Right.Concat(list[b].Left).ToArray(); list[i] = new Pair(list[a].Left, list[b].Right, false, (list[a].Count + list[b].Count + Count(t, u)) % mod); } } else { if (s.Length < t.Length) { list[i] = new Pair(s.ToCharArray(), s.ToCharArray(), true, 0); } else //if (t.Length > 1) { list[i] = new Pair(s.Take(t.Length - 1).ToArray(), s.Skip(s.Length - t.Length + 1).ToArray(), false, Count(t, s.ToCharArray())); } // else // { // list[i] = new Pair(Array.Empty(), Array.Empty(), false, Count(t, s.ToArray())); // } } } WriteLine(list[n - 1].Count); } static int Count(string t, char[] u) { var ans = 0; for (var i = 0; i + t.Length <= u.Length; ++i) { var flg = true; for (var j = 0; j < t.Length; ++j) { if (t[j] != u[i + j]) { flg = false; break; } } if (flg) ++ans; } return ans; } static int mod = 998_244_353; class Pair { public char[] Left; public char[] Right; public bool IsShort; public long Count; public Pair(char[] l, char[] r, bool i, long c) { Left = l; Right = r; IsShort = i; Count = c; } } }