luogu#P12538. [XJTUPC 2025] 泰拉构史

[XJTUPC 2025] 泰拉构史

Background

''Doctor, the connection between us will transcend even time and space. I am sure of it.''

''Even if the ocean completely boils away and the atmosphere vanishes, even if all the moons in the sky are pulled into the vortex of our planet's gravity, and even if the sun continues to mercilessly expand, eventually swallowing up all its children until none can be heard... I am sure we will meet again.''

''At the far end of our civilization, once adorned by darkness and the brilliance of the stars up above, we will meet once again.''

''I will wait until that day comes. I promise you I will. Wait for me. You must wait for me, too.''

''It's so quiet here... Too quiet. Don't leave me behind.''

You are the Doctor of Rhodes Island, the Babel's Omen. But above all, you are the Prophet of Priestess. Civilization teeters on the brink --- yet, against all odds, you remembered her before it was too late. Now go, pull her back from the darkness. Do not betray the one who waited through time itself.

Problem 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.