#P8852. Sequence Transformation and Sum Queries

    ID: 22016 Type: Default 1000ms 256MiB

Sequence Transformation and Sum Queries

Sequence Transformation and Sum Queries

You are given two sequences \(a\) and \(b\), each of length \(n\), where \(a_i, b_i \in [1, n]\) for every \(i \in [1, n]\).

Define an operation as updating every element of \(b\) by the rule: \[ b_i \gets a_{b_i} \quad \text{for all } i \in [1, n]. \]

You need to perform \(n\) operations sequentially. After each operation, output the answer for the sum \[ S = \sum_{i=1}^n b_i \quad (\textrm{mod } 998244353), \] where the sum is computed modulo \(998244353\).

inputFormat

The input consists of three lines:

  1. The first line contains an integer \(n\) -- the length of the arrays.
  2. The second line contains \(n\) space-separated integers representing the sequence \(a\) (i.e. \(a_1, a_2, \dots, a_n\)).
  3. The third line contains \(n\) space-separated integers representing the sequence \(b\) (i.e. \(b_1, b_2, \dots, b_n\)).

outputFormat

Output exactly \(n\) lines. The \(i\)-th line should contain the value of \(S\) (i.e. the sum of the sequence \(b\) modulo \(998244353\)) after performing the \(i\)-th operation.

sample

3
1 2 3
1 2 3
6

6 6

</p>