Pear Trees

Time limit: 0.3s Memory limit: 128MB Input: Output:

Alin wants to start a new life as a farmer in the countryside of Sălaj. He has planted a row of NN pear trees, numbered from 11 to NN in the order in which they were planted. The pear tree ii (1iN1 \leq i \leq N) has PiP_i flowers. Now he is thinking about pollinating the trees by releasing some bees.

Alin studies QQ scenarios in which pollination can take place. For each scenario jj (1jQ1 \leq j \leq Q):

  • he chooses an interval [LjL_j, RjR_j];
  • releases the bees at the LjL_j pear tree;
  • the bees will then visit all pear trees to the right, in order, up to the RjR_j pear tree, including RjR_j.

The Sălaj pear trees have a special way of interacting with bees:

  • if the number of flowers in the currently visited tree, call that number XX, has the same parity as the number of flowers in the previous tree, call that number YY, then the number of flowers in the current tree will increase to XYX \cdot Y;
  • otherwise, the current tree will bloom YY new flowers, increasing its total to X+YX+Y.

For each scenario jj (1jQ1 \leq j \leq Q), Alin wants to know the number of flowers in the RjR_j-th pear tree at the end of the pollination for this scenario. Since this number may be very large, output it modulo 998 244 353998 \ 244 \ 353.

Task

Given the number of flowers for each pear tree, compute the result for each scenario modulo 998 244 353998 \ 244 \ 353.

Input data

The first line of the input contains the integer NN - the number of pear trees.

The second line of the input contains NN integers separated by spaces - the number of flowers in each pear tree.

The third line of the input contains the integer QQ - the number of scenarios.

Each of the next QQ lines contain 22 space-separated integers - LjL_j and RjR_j - the left and right ends for the interval of trees that will be pollinated in the jj-th scenario.

Output data

The output will contain QQ lines. On line jj (1jQ1 \leq j \leq Q), the result for scenario jj will be printed.

Constraints and clarifications

  • 1N,Q2000001 \leq N, Q \leq 200 ' 000
  • 0Pi998 244 3520 \leq P_i \leq 998 \ 244 \ 352 for 1iN1 \leq i \leq N.
  • 1LjRjN1 \leq L_j \leq R_j \leq N for 1jQ1 \leq j \leq Q.
  • All scenarios are independent
# Score Constraints
0 0 Examples
1 13 $N, Q \leq 1 \ 000
2 14 Pi1P_i \leq 1
3 8 N1 000N \leq 1 \ 000
4 9 The number of flowers in each pear tree has the same parity.
5 56 No further restrictions.

Example 1

stdin

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

stdout

13
15
99

Explanation

For the first example, each scenario goes as follows:

4 2 5 7 84 8 5 7 84 8 13 7 8\underline{\textbf{4}}\ \textbf{2}\ \underline{5}\ 7\ 8 \rightarrow \underline{4}\ \textbf{8}\ \underline{\textbf{5}}\ 7\ 8 \rightarrow \underline{4}\ 8\ \underline{\textbf{13}}\ 7\ 8

4 2 5 7 84 2 5 7 154\ 2\ 5\ \underline{\textbf{7}}\ \underline{\textbf{8}} \rightarrow 4\ 2\ 5\ \underline{7}\ \underline{\textbf{15}}

4 2 5 7 84 8 5 7 84 8 13 7 84 8 13 91 84 8 13 91 99\underline{\textbf{4}}\ \textbf{2}\ 5\ 7\ \underline{8} \rightarrow \underline{4}\ \textbf{8}\ \textbf{5}\ 7\ \underline{8} \rightarrow \underline{4}\ 8\ \textbf{13}\ \textbf{7}\ \underline{8} \rightarrow \underline{4}\ 8\ 13\ \textbf{91}\ \underline{\textbf{8}} \rightarrow \underline{4}\ 8\ 13\ 91\ \underline{\textbf{99}}

Example 2

stdin

3
100000 100000 100000
1
1 3

stdout

733427426

Explanation

For the second example, the answer is 10000000000000001000000000000000, which modulo 998 244 353998 \ 244 \ 353 is equal to 733427426733427426.

Log in or sign up to be able to send submissions!