#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #include using mint = atcoder::modint998244353; namespace { int mex(int x, int y) { int i = 0; while (i == x || i == y) { ++i; } return i; } struct node { virtual ~node() = default; virtual mint count(int v) = 0; }; using pnode = unique_ptr; struct integer : node { integer(char c): c(c) { } mint count(int v) override { if (c == '?' || v == c - '0') { return 1; } return 0; } char c; }; struct function : node { function(char c, pnode x, pnode y): c(c), x(move(x)), y(move(y)), memo() { } mint count(int v) override { if (!memo[v]) { mint a = 0, e = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (max(i, j) == v) { a += x->count(i) * y->count(j); } if (mex(i, j) == v) { e += x->count(i) * y->count(j); } } } if (c == '?') { memo[v] = a + e; } if (c == 'a') { memo[v] = a; } if (c == 'e') { memo[v] = e; } } return *memo[v]; } char c; pnode x, y; optional memo[3]; }; tuple parse(string_view s) { if (isdigit(s[0]) || s[0] == '?') { return { make_unique(s[0]), 1 }; } char c = s[1]; auto [x, d1] = parse(s.substr(4)); auto [y, d2] = parse(s.substr(5 + d1)); return { make_unique( s[1], move(x), move(y) ), 6 + d1 + d2 }; } } int main() { string s; cin >> s; int k; cin >> k; auto [n, _] = parse(s); cout << n->count(k).val() << endl; }