luogu#P8057. D ToPTree

D ToPTree

Background

ToPTree stands for ToothPaste Tree. It works like squeezing toothpaste: it moves only when you squeeze it.

Problem Description

You have a sequence of nn numbers [a1an][a_1\sim a_n]. Initially, ai=0a_i=0.

There is an operation sequence AA consisting of qq operations. The ii-th operation is chosen uniformly at random from all nN(n+1)nN(n+1) possible operations:

  • With probability 12\dfrac{1}{2}, randomly choose one of the following two types of operations as this operation.
  • Randomly choose positive integers l,r,x (1lrn,1xN)l,r,x\ (1\le l\le r\le n,1\le x\le N), and add xx to every number in the interval [l,r][l,r].
  • Randomly choose positive integers l,r,x (1lrn,1xN)l,r,x\ (1\le l\le r\le n,1\le x\le N), and set every number in the interval [l,r][l,r] to xx.
  • It is easy to see that there are nN(n+1)nN(n+1) possible operations each time.

Since this tree is a ToothPaste Tree, it will execute only the operations related to your current query that have not been executed yet, and only when you query. Specifically, suppose you query the values of ap1pma_{p_1\sim p_m} in order, then:

  • When querying apia_{p_i}, execute all operations in AA that involve this number (i.e. whose interval [l,r][l,r] contains pip_i) in chronological order (i.e. in the order in AA), and delete them from AA. Then output the current value of apia_{p_i}.

For example, AA currently contains the following four operations in order, and all elements in aa are currently 00:

  1. Add 11 to interval [2,3][2,3].
  2. Add 11 to interval [3,4][3,4].
  3. Assign interval [2,4][2,4] to 11.
  4. Add 11 to interval [2,3][2,3].

Now query the value of a2a_2. Then operations 1,3,41,3,4 will be executed in order, causing a1a4a_1\sim a_4 to become [0,2,2,1][0,2,2,1]. Therefore ToPTree outputs 22. The operation sequence becomes:

  1. Add 11 to interval [3,4][3,4].

Query the value of a3a_3 next. Then operation 11 will be executed, making the operation sequence empty, and a1a4a_1\sim a_4 becomes [0,2,3,2][0,2,3,2], so the output is 33. Note that this result is not the same as the result obtained by executing all operations 141\sim 4 in order.

Given n,m,q,Nn,m,q,N and the sequence pp, ToPTree will output mm values in the query order. Find the expected value of each of these mm outputs, modulo 998244353998244353.

(Before the first query, no operations have been executed.)

Input Format

The first line contains four positive integers n,m,q,Nn,m,q,N.

The next line contains mm positive integers p1pmp_1\sim p_m.

Output Format

Output one line with mm non-negative integers separated by spaces, as the answers modulo 998244353998244353.

2 2 2 2
1 1
665496237 665496237

Hint

Constraints

This problem uses bundled testdata.

For all testdata: 1n,N,q9×1081\le n,N,q\le 9\times 10^8, 1m1061\le m\le 10^6, 1pin1\le p_i\le n. The detailed constraints are as follows:

  • Subtask #1 (4 pts): n,m,q,N3n,m,q,N\le 3.
  • Subtask #2 (10 pts): n,m,q,N5n,m,q,N\le 5.
  • Subtask #3 (3 pts): n=1n=1, m,q123456m,q\le 123456.
  • Subtask #4 (3 pts): q=1q=1, m123456m\le 123456.
  • Subtask #5 (12 pts): m=1m=1, q123456q\le 123456.
  • Subtask #6 (27 pts): n,m,q,N50n,m,q,N\le 50.
  • Subtask #7 (9 pts): m,q5000m,q\le 5000.
  • Subtask #8 (16 pts): m,q123456m,q\le 123456.
  • Subtask #9 (16 pts): No additional constraints.

Translated by ChatGPT 5