luogu#P16364. [OOI 2026] Permutations and Queries

[OOI 2026] Permutations and Queries

Problem Description

You are given a permutation pp of length nn. A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. We define the cost\textit{cost} of the permutation as the sum over all ii from 11 to nn of the quantity (pi)i\left(p_i\right)^i (the ii-th element of the permutation raised to the power of ii). Thus, the cost\textit{cost} of the permutation pp is given by

i=1n(pi)i\sum\limits_{i=1}^n \left(p_i\right)^i

There are qq queries of three types:

  • Reverse. After this, your permutation pp is replaced by the permutation qq, such that qi=pni+1q_i = p_{n - i + 1} for all ii from 11 to nn.
  • Invert. After this, your permutation pp is replaced by the permutation qq, such that qi=npi+1q_i = n - p_i + 1 for all ii from 11 to nn.
  • Take the inverse. After this, your permutation pp is replaced by the permutation qq, such that qpi=iq_{p_i} = i for all ii from 11 to nn.

Note that after each operation, pp remains a permutation.

After each query, you need to output the cost\textit{cost} of the permutation.

Input Format

The first line contains two integers nn and qq (1n,q1000001 \leq n, q \leq 100\,000) --- the length of the permutation and the number of queries.

The second line contains nn positive integers p1p_1, p2p_2, \ldots, pnp_n (1pin1 \leq p_i \leq n) --- the elements of the permutation. It is guaranteed that all pip_i are distinct.

The third line contains qq positive integers b1b_1, b2b_2, \ldots, bqb_q (1bi31 \leq b_i \leq 3) --- the description of the queries. The number bib_i means that the ii-th modification query to be applied to the permutation is of type bib_i.

Output Format

Output qq numbers, the ii-th of which is the remainder of the cost\textit{cost} of the permutation modulo 998244353998\,244\,353, after applying the first ii queries.

5 5
1 2 3 4 5
1 2 3 1 2
65 3413 3413 65 3413
5 6
5 3 1 4 2
3 3 1 2 3 1
293 303 3225 215 317 3209

Hint

Note

Let's analyze the second example.

Initially, p=[5,3,1,4,2]p = [5, 3, 1, 4, 2].

The first query is of type 33, meaning take the inverse. The permutation after this query becomes [3,5,2,4,1][3, 5, 2, 4, 1]. The \textit{cost} of this permutation is $3^1 + 5^2 + 2^3 + 4^4 + 1^5 = 3 + 25 + 8 + 256 + 1 = 293$.

The second query is of type 33, meaning take the inverse. The permutation after this query becomes [5,3,1,4,2][5, 3, 1, 4, 2]. The cost\textit{cost} of this permutation is $5^1 + 3^2 + 1^3 + 4^4 + 2^5 = 5 + 9 + 1 + 256 + 32 = 303$.

The third query is of type 11, meaning reverse. The permutation after this query becomes [2,4,1,3,5][2, 4, 1, 3, 5]. The cost\textit{cost} of this permutation is 21+42+13+34+55=32252^1 + 4^2 + 1^3 + 3^4 + 5^5 = 3225.

The fourth query is of type 22, meaning invert. The permutation after this query becomes [4,2,5,3,1][4, 2, 5, 3, 1]. The cost\textit{cost} of this permutation is 41+22+53+34+15=2154^1 + 2^2 + 5^3 + 3^4 + 1^5 = 215.

The fifth query is of type 33, meaning take the inverse. The permutation after this query becomes [5,2,4,1,3][5, 2, 4, 1, 3]. The cost\textit{cost} of this permutation is 51+22+43+14+35=3175^1 + 2^2 + 4^3 + 1^4 + 3^5 = 317.

The last query is of type 11, meaning reverse. The permutation after this query becomes [3,1,4,2,5][3, 1, 4, 2, 5]. The cost\textit{cost} of this permutation is 31+12+43+24+55=32093^1 + 1^2+ 4^3 + 2^4 + 5^5 = 3209.

Scoring

The tests for this problem consist of five groups. Points for each group are awarded only if all tests in the group and all tests in some of the previous groups are passed. Note that passing the tests from the statement is not required for some groups. The final score for each group is the maximum score obtained for that group of tests across all submitted solutions.

Group Points Additional constraints < Required groups Comment
nn qq
0 -- Tests from the statement.
1 15 n1000n \leq 1000 q1000q \leq 1000 0
2 22 -- -- bi=bjb_i = b_j for all 1i,jq1 \leq i, j \leq q
3 26 bi2b_i \leq 2 for all 1iq1 \leq i \leq q
4 16 pi=ip_i = i for all 1in1 \leq i \leq n
5 21 0--4