#include using namespace std; static constexpr long long MOD = 998244353; struct SegmentTree { int n; vector mul, add; explicit SegmentTree(int n) : n(n), mul(4 * n, 1), add(4 * n, 0) {} // Apply (x -> a*x + b) after the transform already stored here. void apply(int node, long long a, long long b) { mul[node] = a * mul[node] % MOD; add[node] = (a * add[node] + b) % MOD; } void push(int node) { if (mul[node] == 1 && add[node] == 0) return; apply(node * 2, mul[node], add[node]); apply(node * 2 + 1, mul[node], add[node]); mul[node] = 1; add[node] = 0; } void update(int node, int lo, int hi, int ql, int qr, long long a, long long b) { if (ql <= lo && hi <= qr) { apply(node, a, b); return; } push(node); int mid = (lo + hi) / 2; if (ql <= mid) update(node * 2, lo, mid, ql, qr, a, b); if (qr > mid) update(node * 2 + 1, mid + 1, hi, ql, qr, a, b); } void update(int l, int r, long long a, long long b) { if (l <= r) update(1, 0, n - 1, l, r, a, b); } long long query(int node, int lo, int hi, int p) { if (lo == hi) return (mul[node] + add[node]) % MOD; push(node); int mid = (lo + hi) / 2; if (p <= mid) return query(node * 2, lo, mid, p); return query(node * 2 + 1, mid + 1, hi, p); } long long query(int p) { return query(1, 0, n - 1, p); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector parent(n, -1); vector> children(n), adj(n); for (int v = 1; v < n; ++v) { cin >> parent[v]; children[parent[v]].push_back(v); adj[parent[v]].push_back(v); adj[v].push_back(parent[v]); } vector score(n); for (auto& x : score) cin >> x; // Rooted traversal order and depths. vector depth(n, 0), order; order.reserve(n); order.push_back(0); for (int i = 0; i < (int)order.size(); ++i) { int v = order[i]; for (int c : children[v]) { depth[c] = depth[v] + 1; order.push_back(c); } } // Subtree sizes and heavy child. vector sub(n, 1), heavy(n, -1); for (int i = n - 1; i >= 0; --i) { int v = order[i]; int bestSize = 0; for (int c : children[v]) { sub[v] += sub[c]; if (sub[c] > bestSize) { bestSize = sub[c]; heavy[v] = c; } } } // Heavy-light positions. Heavy-first ordering also keeps subtrees contiguous. vector head(n), pos(n); int timer = 0; vector> chains = {{0, 0}}; while (!chains.empty()) { auto [start, chainHead] = chains.back(); chains.pop_back(); for (int v = start; v != -1; v = heavy[v]) { head[v] = chainHead; pos[v] = timer++; for (int c : children[v]) { if (c != heavy[v]) chains.push_back({c, c}); } } } SegmentTree seg(n); auto applyRange = [&](int l, int r, long long b, long long a) { seg.update(l, r, a, b); }; auto applyPath = [&](int u, int v, long long b, long long a) { while (head[u] != head[v]) { if (depth[head[u]] < depth[head[v]]) swap(u, v); applyRange(pos[head[u]], pos[u], b, a); u = parent[head[u]]; } if (depth[u] > depth[v]) swap(u, v); applyRange(pos[u], pos[v], b, a); }; while (q--) { string op; cin >> op; if (op == "LOOK") { int v; cin >> v; cout << seg.query(pos[v]) << '\n'; } else if (op == "LINEAGE") { int v; long long b, a; cin >> v >> b >> a; applyRange(pos[v], pos[v] + sub[v] - 1, b, a); } else if (op == "BRIDGE") { int u, v; long long b, a; cin >> u >> v >> b >> a; applyPath(u, v, b, a); } else if (op == "RIPPLE") { int v, r; long long b, a; cin >> v >> r >> b >> a; queue> todo; todo.push({v, -1, 0}); while (!todo.empty()) { auto [x, prev, dist] = todo.front(); todo.pop(); seg.update(pos[x], pos[x], a, b); if (dist == r) continue; for (int y : adj[x]) { if (y != prev) todo.push({y, x, dist + 1}); } } } } return 0; }