#P12538. [XJTUPC 2025] 泰拉构史

[XJTUPC 2025] 泰拉构史

Description

This is the first step to solve the mystery, a counting problem.

There is a sequence a1,a2,,ana_1, a_2, \dots, a_n of length nn, elements in the sequence are different, and an operation is defined as follows:

  • Select a subscript ii (1in11\le i\le n-1) that satisfies aiai+1=1|a_i-a_{i+1}|=1, and swap aia_i with ai+1a_{i+1} in the sequence.

You can perform any number of operations. How many different sequences can you get? Since the answer is large, please output the answer modulo 998244353998244353.

Two sequences a1,a2,,ana_1, a_2, \dots, a_n and b1,b2,bnb_1, b_2, \dots b_n are different if and only if there exists i[1,n]i\in [1,n] such that aibia_i\neq b_i.

Input Format

There are two lines of input.

The first line contains a positive integer nn (1n1061\le n\le 10^6), which indicates the length of the sequence.

The second line contains nn positive integers aia_i (1ai1091\le a_i\le 10^9), separated by a space, which indicates that the initial sequence is a1,a2,,ana_1, a_2, \dots, a_n. The data guarantees that aia_i in the sequence are different.

Output Format

Output a single positive integer, representing the number of possible sequences modulo 998244353998244353.

4
1 4 2 3
3
9
11 4 5 14 19 1 9 8 10
6
1
1
1
12
4 3 7 6 8 11 9 10 12 14 13 15
90

Hint

For the first sample, the initial sequence is 1,4,2,31,4,2,3. Note that the initial sequence is also a possible sequence and needs to be counted.

For the sequence 1,4,2,31,4,2,3, since a3a4=1|a_3 - a_4| = 1, a3,a4a_3, a_4 can be swapped, and the sequence after swapping is 1,4,3,21,4,3,2.

For the sequence 1,4,3,21,4,3,2, since a2a3=1|a_2 - a_3| = 1, a2,a3a_2, a_3 can be swapped, and the sequence after swapping is 1,3,4,21,3,4,2.

It can be proved that for this sequence, there are only the above 33 possibilities for different sequences after any number of operations.

Since the input and output data scale of this question is large, it is recommended to use a faster input and output method.