import java.io.*; import java.util.*; public class Main { static int idx = 0; static final int MOD = 998244353; static int[][] def = new int[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 1}}; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int[] dp = search(sc.next().toCharArray()); System.out.println(dp[sc.nextInt()]); } static int[] search(char[] arr) { if (arr[idx] == 'm') { if (arr[idx + 1] == '?') { idx += 4; return mxx(arr); } else if (arr[idx + 1] == 'a') { idx += 4; return max(arr); } else { idx += 4; return mex(arr); } } else if (arr[idx] == '?') { idx++; return def[3]; } else { idx++; return def[arr[idx - 1] - '0']; } } static int[] max(char[] arr) { int[] dp1 = search(arr); idx++; int[] dp2 = search(arr); idx++; return max(dp1, dp2); } static int[] max(int[] dp1, int[] dp2) { int[] ans = new int[3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ans[Math.max(i, j)] += calc(dp1[i], dp2[j]); ans[Math.max(i, j)] %= MOD; } } return ans; } static int[] mex(char[] arr) { int[] dp1 = search(arr); idx++; int[] dp2 = search(arr); idx++; return mex(dp1, dp2); } static int[] mex(int[] dp1, int[] dp2) { int[] ans = new int[3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ans[mex(i, j)] += calc(dp1[i], dp2[j]); ans[mex(i, j)] %= MOD; } } return ans; } static int[] mxx(char[] arr) { int[] dp1 = search(arr); idx++; int[] dp2 = search(arr); idx++; return mxx(dp1, dp2); } static int[] mxx(int[] dp1, int[] dp2) { int[] ans = new int[3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ans[Math.max(i, j)] += calc(dp1[i], dp2[j]); ans[Math.max(i, j)] %= MOD; ans[mex(i, j)] += calc(dp1[i], dp2[j]); ans[mex(i, j)] %= MOD; } } return ans; } static int calc(int x, int y) { return (int)((long)x * y % MOD); } static int mex (int x, int y) { if (x == 0) { if (y == 1) { return 2; } else { return 1; } } else if (y == 0) { if (x == 1) { return 2; } else { return 1; } } else { return 0; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }