#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / gcd(a, b)) * b;   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   inline long long getnum() {     char c = getchar();     long long num, sign = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (num = 0; c >=  0  && c <=  9 ;) {       c -=  0 ;       num = num * 10 + c;       c = getchar();     }     return num * sign;   }   inline long long power(long long a, long long b) {     long long multiply = 1;     for (int i = (0); i < (b); i++) {       multiply *= a;     }     return multiply;   }   int n, q, type, l, r, k, a[100002], blockSZ;   vector<int> v[318];   int sz, cnt[100002][318];   void Shift(int to, int from) {     int b1 = -1, b2 = -1;     int s = 0, idx1, idx2;     if (to == from) return;     for (int i = (0); i < (sz + 1); i++) {       if (b1 == -1 && s + (int)v[i].size() > from) {         b1 = from - s;         idx1 = i;       }       if (b2 == -1 && s + (int)v[i].size() > to) {         b2 = to - s;         idx2 = i;       }       s += v[i].size();     }     int val = v[idx1][b1];     v[idx1].erase(v[idx1].begin() + b1);     v[idx2].insert(v[idx2].begin() + b2, val);     cnt[val][idx1]--;     cnt[val][idx2]++;   }   int Query(int from, int to, int x) {     int s = 0, idx1 = -1, idx2 = -1, b1 = -1, b2 = -1;     for (int i = (0); i < (sz + 1); i++) {       if (idx1 == -1 && s + (int)v[i].size() > from) {         idx1 = i;         b1 = from - s;       }       if (idx2 == -1 && s + (int)v[i].size() > to) {         idx2 = i;         b2 = to - s;       }       s += v[i].size();     }     int ret = 0;     for (int i = (idx1 + 1); i < (idx2); i++) {       ret += cnt[x][i];     }     if (idx1 != idx2) {       for (int i = (b1); i < (v[idx1].size()); i++) {         if (v[idx1][i] == x) ret++;       }       for (int i = (0); i < (b2 + 1); i++) {         if (v[idx2][i] == x) ret++;       }     } else {       for (int i = (b1); i < (b2 + 1); i++) {         if (v[idx1][i] == x) ret++;       }     }     return ret;   }   void Rebuild() {     vector<int> temp;     for (int i = (0); i < (sz + 1); i++) {       for (int j = (0); j < (v[i].size()); j++) {         temp.push_back(v[i][j]);         cnt[v[i][j]][i]--;       }       v[i].clear();     }     sz = 0;     for (int i = (0); i < (n); i++) {       v[sz].push_back(temp[i]);       cnt[temp[i]][sz]++;       if (i + 1 < n && (i + 1) % blockSZ == 0) sz++;     }   }   void Print() {     cout <<  All numbers:  ;     for (int i = (0); i < (sz + 1); i++) {       for (int j = (0); j < (v[i].size()); j++) cout << v[i][j] <<    ;     }     cout <<   n ;     cout <<  All vectors:            <<   n ;     for (int i = (0); i < (sz + 1); i++) {       for (int J = (0); J < (v[i].size()); J++) cout << v[i][J] <<    ;       cout <<   n ;     }   }   int main() {     int test, cases = 1;     scanf( %d , &n);     blockSZ = 700;     for (int i = (0); i < (n); i++) {       scanf( %d , &a[i]);     }     for (int i = (0); i < (n); i++) {       v[sz].push_back(a[i]);       cnt[a[i]][sz]++;       if (i + 1 < n && (i + 1) % blockSZ == 0) sz++;     }     scanf( %d , &q);     int last = 0, shift = 0;     while (q--) {       scanf( %d , &type);       if (type == 1) {         scanf( %d%d , &l, &r);         l = ((l + last - 1) % n) + 1;         r = ((r + last - 1) % n) + 1;         l--, r--;         if (l > r) swap(l, r);         Shift(l, r);         shift++;       } else {         scanf( %d%d%d , &l, &r, &k);         l = ((l + last - 1) % n) + 1;         r = ((r + last - 1) % n) + 1;         k = ((k + last - 1) % n) + 1;         l--, r--;         if (l > r) swap(l, r);         last = Query(l, r, k);         printf( %d n , last);       }       if (shift == 1200) {         Rebuild();         shift = 0;       }     }     return 0;   }