import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable long MOD = 998244353; enum Exp { MAX, MEX, UNKNOWN }; class Node { bool isLeaf; Exp exp; int num; long[3] cnt; Node left; Node right; } void main() { auto S = readln.chomp; auto K = readln.chomp.to!int; int idx = 0; Node parse() { Node node = new Node; if (S[idx] == ',') idx++; if (S[idx] == 'm') { node.isLeaf = false; if (S[idx..idx+3] == "max") { node.exp = Exp.MAX; } else if (S[idx..idx+3] == "mex") { node.exp = Exp.MEX; } else { node.exp = Exp.UNKNOWN; } idx += 4; node.left = parse(); idx += 1; node.right = parse(); idx += 1; } else { node.isLeaf = true; if (S[idx] != '?') { node.num = S[idx] - '0'; } else { node.num = -1; } } return node; } auto mex = new int[][](3, 3); int mex_(int i, int j) { foreach (k; 0..3) if (k != i && k != j) return k; assert(0); } foreach (i; 0..3) foreach (j; 0..3) mex[i][j] = mex_(i, j); void calc(Node node) { if (node.isLeaf) { if (node.num == -1) { node.cnt = [1, 1, 1]; } else { node.cnt[node.num] = 1; } return; } calc(node.left); calc(node.right); if (node.exp == Exp.MAX) { foreach (i; 0..3) foreach (j; 0..3) { (node.cnt[max(i, j)] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD; } } else if (node.exp == Exp.MEX) { foreach (i; 0..3) foreach (j; 0..3) { (node.cnt[mex[i][j]] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD; } } else { foreach (i; 0..3) foreach (j; 0..3) { (node.cnt[max(i, j)] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD; (node.cnt[mex[i][j]] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD; } } } auto root = parse(); calc(root); writeln(root.cnt[K]); }