#P10236. [yLCPC2024] D. 排卡

[yLCPC2024] D. 排卡

Background

After going through countless hardships, Fusu finally arrived at the arcade. But she soon found that there was a huge line (not the NOi Qualifier’s Team B).

When there are many people, maimai players often decide who plays next by “queueing cards”. Because there are simply too many people, while queueing the cards, they noticed the ID numbers on the cards.

They asked Fusu a question. Can you solve it?

Problem Description

Fusu has a deque aa. This deque is similar to the queue concept in computer science, but the difference is that it can read and pop elements from the front, and also read and pop elements from the back, so it is called a “double-ended queue (deque)”.

There are nn numbers in this deque. Fusu will construct a sequence bb of length nn through nn operations. In the ii-th operation (1in1 \leq i \leq n), she can perform one of the following two processes:

  1. Let bib_i be the front element of aa, and pop one element from the front of aa.
  2. Let bib_i be the back element of aa, and pop one element from the back of aa.

We define the score of a pair (i,j)(i, j) as:

score(i,j)=ijmod998244353\mathrm{score}(i,j) = i^j \bmod 998244353

That is, the result of ii to the power of jj modulo 998244353998244353. In particular, in this problem we define 00=00^0 = 0.

Now, Fusu wants to construct the sequence bb using the best strategy, to maximize the value of the following expression:

$$\sum_{i = 1}^{n - 1} \mathrm{score}(b_i, b_{i + 1})$$

That is, the sum of the scores of all adjacent pairs in bb, computed in the original order.

Note that we only take modulo 998,244,353998,244,353 when computing the score of a pair. When computing the sum, we do not take the sum modulo anything.

Input Format

This problem contains multiple test cases in a single test file. The first line is a positive integer TT, representing the number of test cases. For each test case:

The first line is an integer nn (2n1032 \leq n \leq 10^3), representing the length of the sequence.
The second line contains nn integers a1,a2,ana_1, a_2, \dots a_n (0ai<998,244,3530 \leq a_i < 998,244,353), representing the values in deque aa from front to back.

It is guaranteed that the sum of nn over all test cases in a single test file does not exceed 10310^3.

Output Format

For each test case, output one line with one integer representing the answer.

2
5
5 3 1 4 2
6
6 5 1 4 2 3
1168
15655

Hint

Translated by ChatGPT 5