#P1795D. Triangle Coloring

Triangle Coloring

Description

You are given an undirected graph consisting of nn vertices and nn edges, where nn is divisible by 66. Each edge has a weight, which is a positive (greater than zero) integer.

The graph has the following structure: it is split into n3\frac{n}{3} triples of vertices, the first triple consisting of vertices 1,2,31, 2, 3, the second triple consisting of vertices 4,5,64, 5, 6, and so on. Every pair of vertices from the same triple is connected by an edge. There are no edges between vertices from different triples.

You have to paint the vertices of this graph into two colors, red and blue. Each vertex should have exactly one color, there should be exactly n2\frac{n}{2} red vertices and n2\frac{n}{2} blue vertices. The coloring is called valid if it meets these constraints.

The weight of the coloring is the sum of weights of edges connecting two vertices with different colors.

Let WW be the maximum possible weight of a valid coloring. Calculate the number of valid colorings with weight WW, and print it modulo 998244353998244353.

The first line contains one integer nn (6n31056 \le n \le 3 \cdot 10^5, nn is divisible by 66).

The second line contains nn integers w1,w2,,wnw_1, w_2, \dots, w_n (1wi10001 \le w_i \le 1000) — the weights of the edges. Edge 11 connects vertices 11 and 22, edge 22 connects vertices 11 and 33, edge 33 connects vertices 22 and 33, edge 44 connects vertices 44 and 55, edge 55 connects vertices 44 and 66, edge 66 connects vertices 55 and 66, and so on.

Print one integer — the number of valid colorings with maximum possible weight, taken modulo 998244353998244353.

Input

The first line contains one integer nn (6n31056 \le n \le 3 \cdot 10^5, nn is divisible by 66).

The second line contains nn integers w1,w2,,wnw_1, w_2, \dots, w_n (1wi10001 \le w_i \le 1000) — the weights of the edges. Edge 11 connects vertices 11 and 22, edge 22 connects vertices 11 and 33, edge 33 connects vertices 22 and 33, edge 44 connects vertices 44 and 55, edge 55 connects vertices 44 and 66, edge 66 connects vertices 55 and 66, and so on.

Output

Print one integer — the number of valid colorings with maximum possible weight, taken modulo 998244353998244353.

Sample Input 1

12
1 3 3 7 8 5 2 2 2 2 4 2

Sample Output 1

36

Sample Input 2

6
4 2 6 6 6 4

Sample Output 2

2

Note

The following picture describes the graph from the first example test.

The maximum possible weight of a valid coloring of this graph is 3131.