N = $stdin.gets.chomp.to_i P = [[0,1,1],[0,1,1]] + (2..N).map{ $stdin.gets.chomp.split(" ").map{ |a| a.to_i } } _ = $stdin.gets.chomp.to_i Q = $stdin.gets.chomp.split(" ").map{ |a| a.to_i } M = 998244353 $funcs = [ lambda { |x,mem| 1 }, lambda { |x,mem| x }, ] def ff(i,x,mem) if mem[i] return mem[i] end r = $funcs[i].call(x,mem) mem[i] = r r end (2..N).each { |i| t,a,b = *P[i] $funcs << case t when 1 lambda{ |x,mem| (ff(a,x,mem) + ff(b,x,mem)) % M } when 2 lambda{ |x,mem| (a * ff(b,x,mem)) % M } when 3 lambda{ |x,mem| (ff(a,x,mem) * ff(b,x,mem)) % M } end } Q.each { |x| puts $funcs[-1].call(x,{}) }