luogu#P4828. Nagisa loves Tomoya

Nagisa loves Tomoya

Problem Description

Cute Furukawa Nagisa gets a sequence. She defines one operation as changing all aia_i to ai+a(i mod n+1)a_i + a_{(i\ mod\ n + 1)}. Then she will ask you QQ queries. Each query gives x,yx, y, meaning: after the xx-th operation, what is the value of the element at position yy.

This problem is too easy, and our Nagisa solved it effortlessly. Can you solve it too?

Input Format

An integer nn, representing the length of the sequence.

The next line contains nn integers, representing this sequence.

The next line contains an integer QQ, representing the number of queries.

The next QQ lines each contain two integers x,yx, y, with the meaning as described in the statement.

Output Format

Output QQ lines. Each line contains one integer, the answer to the query. Please output the result modulo 998244353998244353.

5
1 2 3 4 5
2
1 2
2 2
5
12

Hint

For 30%30\% of the testdata, it holds that:

1n1001 \leq n \leq 100

1x1001 \leq x \leq 100

1Q1001 \leq Q \leq 100

For 50%50\% of the testdata:

1n1000001 \leq n \leq 100000

1x5001 \leq x \leq 500

1Q1001 \leq Q \leq 100

For 100%100\% of the testdata, it holds that:

1n1061 \leq n \leq 10^6

1ai1091 \leq a_i \leq 10^9

1x20001 \leq x \leq 2000

1Q1041 \leq Q \leq 10^4

Translated by ChatGPT 5