MOD = 998244353 def solve(x, t, a, b, n) f = Array.new(n + 1) f[0] = 1 f[1] = x 2.upto(n) do |i| if t[i] == 1 f[i] = (f[a[i]] + f[b[i]]) % MOD elsif t[i] == 2 f[i] = a[i] * f[b[i]] % MOD else f[i] = (f[a[i]] * f[b[i]]) % MOD end end return f[n] end n = gets.to_i t = Array.new(n + 1) a = Array.new(n + 1) b = Array.new(n + 1) 2.upto(n) do |i| t[i], a[i], b[i] = gets.split.map(&:to_i) end q = gets.to_i puts gets.split.map{|x| solve(x.to_i, t, a, b, n)}