Under construction

Still being built. Things may change

Enigmatic Device

Time limit3sMemory limit256 MB

Summary
Given an array, support range squaring modulo 2010 and range sum queries, using the fact that values cycle quickly under repeated squaring.
Level

Medium6 of 10

Topics
Segment tree, Math, Implementation
Solved
No attempts yet

Problem

A curious device stores an integer sequence a1,a2,…,ana_1, a_2, \ldots, a_n and repeatedly transforms it. It supports two kinds of operations:

  1. Square — given an interval [l,r][l, r], replace ai←ai2 mod 2010a_i \leftarrow a_i^2 \bmod 2010 for every index ii with l≤i≤rl \le i \le r.
  2. Sum — given an interval [l,r][l, r], output ∑i=lrai\sum_{i=l}^{r} a_i. This running sum is not reduced modulo 2010.

Simulate the device: apply each squaring operation and answer each sum query.

Input

The first line contains the sequence length nn (1≤n≤50 0001 \le n \le 50\,000).

The second line contains nn integers a1,…,ana_1, \ldots, a_n, the initial sequence (0≤ai≤20090 \le a_i \le 2009).

The third line contains the number of operations mm (1≤m≤50 0001 \le m \le 50\,000).

Each of the next mm lines describes one operation with three integers kk, ll, rr: k=1k = 1 squares the interval, k=2k = 2 queries its sum, and 1≤l≤r≤n1 \le l \le r \le n.

Output

For each operation of the second kind, print its result on its own line, in the order the queries appear in the input.

Examples6

  1. Example 1

    Input
    3
    17 239 999
    4
    2 1 3
    1 2 3
    2 2 3
    2 1 2
    
    Expected output
    1255
    1882
    858
    
  2. Example 2

    Input
    1
    3
    8
    2 1 1
    1 1 1
    2 1 1
    1 1 1
    2 1 1
    1 1 1
    2 1 1
    1 1 1
    
    Expected output
    3
    9
    81
    531
    
  3. Example 3

    Input
    4
    0 0 0 0
    3
    1 1 4
    2 1 4
    1 2 3
    
    Expected output
    0
    
  4. Example 4

    Input
    5
    1 1 1 1 1
    4
    2 1 5
    1 1 5
    2 1 5
    2 2 4
    
    Expected output
    5
    5
    3
    
  5. Example 5

    Input
    3
    2009 2008 2007
    5
    2 1 3
    1 1 3
    2 1 3
    1 1 3
    2 1 3
    
    Expected output
    6024
    14
    98
    
  6. Example 6

    Input
    4
    29 30 37 38
    5
    2 1 4
    1 1 4
    2 1 4
    1 1 4
    2 1 4
    
    Expected output
    134
    4554
    5358