import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 def z_algorithm(target): len_t = len(target) lcp = [-1]*len_t top = 1 right = 0 lcp[0] = 0 while top < len_t: while top+right < len_t and target[right] == target[top+right]: right += 1 lcp[top] = right left = 1 if right == 0: top += 1 continue while left+lcp[left] < right and left < right: lcp[top+left] = lcp[left] left += 1 top += left right -= left return lcp n = II() t = list(map(ord, SI())) m = len(t)-1 def merge(j, k): v = dp[j]+dp[k] if m: if len(ll[j]) == m: ll.append(ll[j]) else: l = ll[j]+ll[k] ll.append(l[:m]) if len(rr[k]) == m: rr.append(rr[k]) else: r = rr[j]+rr[k] rr.append(r[-m:]) lcp = z_algorithm(t+[-1]+rr[j]+ll[k]) v += lcp.count(m+1) dp.append(v%md) ll = [] rr = [] dp = [] for i in range(n): s = SI() if s[0] == "~": _, j, k = s.split() j, k = int1(j), int1(k) merge(j, k) else: s = list(map(ord, s)) if m: ll.append(s[:m]) rr.append(s[-m:]) lcp = z_algorithm(t+[-1]+s) dp.append(lcp.count(m+1)) print(dp[-1])