Minimize Product

Time limit: 1sMemory limit: 256MBInput: Output:

You are given a 0-indexed array of NN integers. We define flip(l,r)\textit{flip}(l, r) as the operation that takes all elements in the range [l,r][l, r] and flips their sign, i.e. positive becomes negative, negative becomes positive.

You are given QQ queries of the form l r kl\ r\ k. For each query, determine the number of ways to achieve the minimum possible product in the subarray [l,r][l, r] using at most kk flip\textit{flip} operations in total, each applied to an arbitrary subarray of [l,r][l, r]. Since the number of ways can be quite large, print it modulo 998 244 353998\ 244\ 353.

A way is a sequence of at most kk flip\textit{flip} operations. Two ways are considered different if they contain a different number of operations or if their operations differ at any position in the sequence. Thus, the order of operations matters, the same subarray may be flipped multiple times, and the empty sequence (performing no operations) is also a valid way.

Input data

The first line of standard input contains two space-separated integers NN and QQ, representing the size of the array and the number of queries.

The second line of standard input contains NN space-separated integers ViV_i, representing the elements of the array VV.

The next QQ lines of standard input each contain three space-separated integers lil_i, rir_i and kik_i, representing the ii-th query.

Output data

Each line ii of the next QQ lines of standard output will contain the answer to the ii-th query, modulo 998 244 353998\ 244\ 353.

Constraints and clarifications

  • 1≤N≤500 0001 \le N \le 500\ 000.
  • 1≤Q≤500 0001 \le Q \le 500\ 000.
  • 0≤li≤ri<N0 \le l_i \le r_i < N, for all i∈{0,1,…,Q−1}i \in \{0, 1, \dots, Q-1\}.
  • 0≤ki≤1 000 000 0000 \le k_i \le 1\ 000\ 000\ 000, for all i∈{0,1,…,Q−1}i \in \{0, 1, \dots, Q-1\}.
  • −1 000 000 000≤Vi≤1 000 000 000-1\ 000\ 000\ 000 \le V_i \le 1\ 000\ 000\ 000, for all i∈{0,1,…,N−1}i \in \{0, 1, \dots, N-1\}.
# Score Constraints
0 0 Examples.
1 10 N≤7N \le 7, Q=1Q = 1, ki≤5k_i \le 5, $
2 10 N≤1 000N \le 1\ 000, Q≤1 000Q \le 1\ 000, ki≤10k_i \le 10.
3 15 N≤1 000N \le 1\ 000, Q≤1 000Q \le 1\ 000, ki≤1 000k_i \le 1\ 000.
4 15 N≤100 000N \le 100\ 000, Q≤100 000Q \le 100\ 000, ki≤1 000 000k_i \le 1\ 000\ 000, all queried subarrays contain no zeroes.
5 15 N≤100 000N \le 100\ 000, Q≤100 000Q \le 100\ 000, ki≤1 000 000k_i \le 1\ 000\ 000, all queried subarrays contain at least one zero.
6 15 N≤100 000N \le 100\ 000, Q≤100 000Q \le 100\ 000, ki≤1 000 000k_i \le 1\ 000\ 000.
7 20 No additional restrictions.

Example

stdin

7 7
1 6 -1 4 -5 0 7
1 4 4
2 3 1
2 3 2
0 4 3
2 2 1
1 3 0
5 6 2

stdout

5550
2
7
1818
1
1
13

Explanation

In the second query, the minimum possible product is −4-4, and it can be achieved by the following sequences of operations:

  • ∅\emptyset;
  • flip(2,3)\textit{flip}(2, 3).

In the third query, the minimum possible product is −4-4, and it can be achieved by the following sequences of operations:

  • ∅\emptyset;
  • flip(2,3)\textit{flip}(2, 3);
  • flip(2,3)\textit{flip}(2, 3), flip(2,3)\textit{flip}(2, 3);
  • flip(2,2)\textit{flip}(2, 2), flip(3,3)\textit{flip}(3, 3);
  • flip(3,3)\textit{flip}(3, 3), flip(2,2)\textit{flip}(2, 2);
  • flip(2,2)\textit{flip}(2, 2), flip(2,2)\textit{flip}(2, 2);
  • flip(3,3)\textit{flip}(3, 3), flip(3,3)\textit{flip}(3, 3).

In the fifth query, the minimum possible product is −1-1, and it can be achieved by the following sequences of operations:

  • ∅\emptyset.

In the sixth query, the minimum possible product is −24-24, and it can be achieved by the following sequences of operations:

  • ∅\emptyset.

In the seventh query, the minimum possible product is 00, and it can be achieved by the following sequences of operations:

  • ∅\emptyset;
  • flip(5,5)\textit{flip}(5, 5);
  • flip(5,6)\textit{flip}(5, 6);
  • flip(6,6)\textit{flip}(6, 6);
  • flip(5,5)\textit{flip}(5, 5), flip(5,5)\textit{flip}(5, 5);
  • flip(5,6)\textit{flip}(5, 6), flip(5,5)\textit{flip}(5, 5);
  • flip(6,6)\textit{flip}(6, 6), flip(5,5)\textit{flip}(5, 5);
  • flip(5,5)\textit{flip}(5, 5), flip(5,6)\textit{flip}(5, 6);
  • flip(5,6)\textit{flip}(5, 6), flip(5,6)\textit{flip}(5, 6);
  • flip(6,6)\textit{flip}(6, 6), flip(5,6)\textit{flip}(5, 6);
  • flip(5,5)\textit{flip}(5, 5), flip(6,6)\textit{flip}(6, 6);
  • flip(5,6)\textit{flip}(5, 6), flip(6,6)\textit{flip}(6, 6);
  • flip(6,6)\textit{flip}(6, 6), flip(6,6)\textit{flip}(6, 6).

Example

stdin

3 2
3 -2 -5
0 2 0
1 2 1

stdout

1
2

Explanation

In the first query of the second example, no operations are allowed, so the product stays 3⋅(−2)⋅(−5)=303 \cdot (-2) \cdot (-5) = 30. This is the minimum possible product, and it is achieved by exactly one sequence of operations, the empty one, so the answer is 11:

  • ∅\emptyset.

In the second query of the second example, the minimum possible product is −10-10, and it can be achieved by the following sequences of operations:

  • flip(1,1)\textit{flip}(1, 1);
  • flip(2,2)\textit{flip}(2, 2).

Log In or Sign Up to be able to send submissions!