#P8895. 「DPOI-1」优美的序列

「DPOI-1」优美的序列

Background

Update on 2022.12.18: Added one set of hack testdata targeting
https://www.luogu.com.cn/user/421155

Update on 2022.12.18: Added one set of hack testdata targeting @大眼仔Happy, placed at #22, worth 55 points.

Update on 2025.7.7: Added two sets of hack testdata from ticket https://www.luogu.com.cn/ticket/XQCA458514, placed at #23 and #24. #21 to #24 are all changed to 00 points.


No, Commander-in-Chief.

Problem Description

The Commander-in-Chief gives you a sequence aa of length nn.

He thinks that this aa may not be beautiful enough, and he wants to reorder it into a sequence aa' to make it beautiful.

We call a sequence aa of length nn beautiful if and only if i[1,n]\exists i \in [1,n] such that:

  • j[1,i)\forall j \in [1, i), aj>aj+1a_j > a_{j + 1}.
  • j(i,n]\forall j \in (i, n], aj>aj1a_j > a_{j - 1}.

He orders you to find how many different aa' can be obtained by reordering aa. Since the result may be very large, you only need to output it modulo pp.

A fixed aa is too boring, so he gives you mm modifications. Each modification is of the form x k, meaning set axka_x \leftarrow k. You need to output the current answer after each modification.

Input Format

The first line contains three integers n,m,pn, m, p.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n.

The next mm lines each contain two integers x,kx, k, representing one modification operation.

Output Format

Output a total of m+1m + 1 lines. Each line contains one integer, representing the required value for the initial state and after each modification.

4 1 998244353
1 2 2 3
3 4
2
8
见下发文件 sequence2.in
见下发文件 sequence2.out

Hint

Explanation for Sample #1

For the initial state, the valid aa' are [2,1,2,3],[3,2,1,2][2, 1, 2, 3], [3, 2, 1, 2], for a total of 22.

For a=[1,2,4,3]a = [1, 2, 4, 3] after the first modification, the valid aa' are $[1, 2, 3, 4], [2, 1, 3, 4], [3, 1, 2, 4], [4, 1, 2, 3], [3, 2, 1, 4], [4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 2, 1]$, for a total of 88.

Explanation for Sample #2

This sample satisfies the constraints of test points 152015 \sim 20.

Constraints

Test Point ID nn \leq mm \leq Special Condition
121 \sim 2 1010 None
343 \sim 4 100100
565 \sim 6 10310^3
7107 \sim 10 10510^5
111211 \sim 12 5×1055 \times 10^5 00 aa is a permutation
131413 \sim 14 None
152015 \sim 20 5×1055\times 10^5

For 100%100\% of the data, 1n5×1051 \leq n \leq 5 \times 10^5, 0m5×1050 \leq m \leq 5 \times 10^5, 2p1092 \leq p \leq 10^9, and 1ai,k,xn1 \leq a_i, k, x \leq n.

Translated by ChatGPT 5