455_D. Serega and Fun_18698
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.

You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:

  1. Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
  2. Count how many numbers equal to k are on the segment from l to r (both borders inclusive). 



Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).

The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.

As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n.

To decode the queries from the data given in input, you need to perform the following transformations:

li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.

Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

Output

For each query of the 2-nd type print the answer on a single line.

Examples

Input

7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3


Output

2
1
0
0


Input

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


Output

2
0

import java.io.*;
import java.util.*;
import java.math.BigInteger.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import java.util.logging.Level;
import java.util.logging.Logger;

// https://netbeans.org/kb/73/java/editor-codereference_ru.html#display
//<editor-fold defaultstate="collapsed" desc="Main">
public class Main {

    private void run() {
        Locale.setDefault(Locale.US);
        boolean oj = true;
        try {
            oj = System.getProperty("MYLOCAL") == null;
        } catch (Exception e) {
        }

        if (oj) {
            sc = new FastScanner(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        } else {
            try {
                sc = new FastScanner(new FileReader("input.txt"));
                out = new PrintWriter(new FileWriter("output.txt"));
            } catch (IOException e) {
                MLE();
            }
        }
        Solver s = new Solver();
        s.sc = sc;
        s.out = out;
        s.solve();
        if (!oj) {
            err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
            err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
        }
        out.flush();
    }

    private void show(int[] arr) {
        for (int v : arr) {
            err.print(" " + v);
        }
        err.println();
    }

    public static void MLE() {
        int[][] arr = new int[1024 * 1024][];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[1024 * 1024];
        }
    }

    public static void main(String[] args) {
        new Main().run();
    }

    long timeBegin = System.currentTimeMillis();
    FastScanner sc;
    PrintWriter out;
    PrintStream err = System.err;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="FastScanner">
class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStreamReader reader) {
        br = new BufferedReader(reader);
        st = new StringTokenizer("");
    }

    String next() {
        while (!st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException ex) {
                Main.MLE();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }
}
//</editor-fold>

class Solver {

    FastScanner sc;
    PrintWriter out;
    PrintStream err = System.err;

    int n, sqrt;
    ArrayList<Integer>[] arr;
    int[][] cnt;
    //ArrayList<Integer> bfArr = new ArrayList<>();
    
    void initArr(){
        arr = new ArrayList[sqrt];
        for (int i = 0; i < sqrt; i++) {
            arr[i] = new ArrayList<>(2*sqrt+10);
            for (int j = 0; j < sqrt; j++) {
                if( i*sqrt + j < n )
                    arr[i].add(-1);
            }
        }
    }
    
    void solve(){
        n = sc.nextInt();
        
        sqrt = 1;
        while( sqrt*sqrt < n ) ++sqrt;
        
        initArr();
        cnt = new int[sqrt][n+1];
        
        for( int i = 0; i < n; ++i ){
            int val = sc.nextInt();
//            bfArr.add(val);
            int b = i/sqrt;
            arr[b].set(i%sqrt, val);
            cnt[b][val]++;
        }
        
        
        int Q = sc.nextInt();
        int lastans = 0;
        for (int iter = 0; iter < Q; iter++) {
            //if( iter % 1000 == 0 ) err.println( iter );
            int op = sc.nextInt();
            int ql = sc.nextInt();
            int qr = sc.nextInt();
            ql = (ql + lastans - 1) % n;
            qr = (qr + lastans - 1) % n;
            if( ql > qr ){ int t = ql; ql = qr; qr = t; }
            
            if     ( op == 1 ){
                if( iter % sqrt == 0 ){
                    int sz = 0;
                    int[] val = new int[n];
                    for ( int b = 0; b < arr.length; ++b){
                        ArrayList<Integer> arrayList = arr[b];
                        for (Integer v : arrayList){
                            if( sz < n ){
                                val[sz++] = v;
                                cnt[b][v]--;
                            }
                        }
                    }
                    
                    initArr();
                    

                    for (int i = 0; i < n; ++i) {
                        int v = val[i];
                        int b = i / sqrt;
                        arr[b].set(i % sqrt, v);
                        cnt[b][v]++;
                    }
                }
                int valR = remove(qr);
                add( valR, ql );
                //
//                Collections.rotate(bfArr.subList(ql, qr+1), 1);
                //
                
//                err.println(Arrays.deepToString(arr));
//                err.println(bfArr);
//                err.println();
            }
            else if( op == 2 ){
                int k = sc.nextInt();
                k = (k + lastans - 1) % n + 1;
                lastans = 0;
                for (int b = 0, sz = 0; b < sqrt; sz += arr[b].size(), b++) {
                    int l = sz;
                    int r = l + arr[b].size() - 1;
                    if( max(l,ql) <= min(r,qr) ){
                        if( ql <= l && r <= qr ){
                            lastans += cnt[b][k];
                        }
                        else{
                            for( int i = max(l,ql); i <= min(r,qr); ++i )
                                if( arr[b].get(i-sz) == k )
                                    ++lastans;
                        }
                    }
                }
                
//                int bfAns = 0; for( int i = ql; i <= qr; ++i )
//                    if( bfArr.get(i) == k ) ++bfAns;
//                err.println("bfAns: " + bfAns);
//                err.println( "ql, qr: " + ql +" "+ qr );
//                err.println();
                
                out.println(lastans);
            }
            else Main.MLE();
        }
    }

    int remove(int pos) {
        for (int b = 0, sz = 0; b < sqrt; sz += arr[b].size(), b++) {
            int l = sz;
            int r = l + arr[b].size() - 1;
            if ( l <= pos && pos <= r ) {
                int val = arr[b].remove( pos - sz );
                --cnt[b][val];
                return val;
            }
        }
        throw new RuntimeException();
    }

    void add(int val, int pos) {
        for (int b = 0, sz = 0; b < sqrt; sz += arr[b].size(), b++) {
            int l = sz;
            int r = l + arr[b].size() - 1;
            if ( l <= pos && pos <= r ) {
                cnt[b][val]++;
                arr[b].add( pos - sz, val );
                return;
            }
        }
    }

}
