#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const int N = 700000;
const int s = 500;
int n, m, a[N], b[460][120000], res;
deque<int> q[N], q1;
void change(int* val) { *val = (*val + res - 1) % n; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n - 1; i++) b[i / s][a[i]]++;
  for (int i = 0; i <= n - 1; i++) q[i / s].push_back(a[i]);
  scanf("%d", &m);
  res = 0;
  while (m--) {
    int type, l, r;
    scanf("%d", &type);
    if (type == 1) {
      scanf("%d%d", &l, &r);
      change(&l);
      change(&r);
      if (l > r) swap(l, r);
      if (l == r) continue;
      int i1 = l / s, i2 = r / s;
      for (int i = i1; i <= i2 - 1; i++) {
        b[i + 1][q[i].back()]++;
        b[i][q[i].back()]--;
        q[i + 1].push_front(q[i].back());
        q[i].pop_back();
      }
      int f = 0;
      if (i1 != i2) f = 1;
      int val;
      int kol = 0;
      for (deque<int>::iterator it = q[i2].begin(); it != q[i2].end(); it++) {
        if (kol == r % s + f) {
          val = (*it);
          break;
        }
        kol++;
      }
      q[i2].erase(q[i2].begin() + r % s + f);
      q[i1].insert(q[i1].begin() + l % s, val);
      b[i2][val]--;
      b[i1][val]++;
    } else {
      int k;
      scanf("%d%d%d", &l, &r, &k);
      change(&l);
      change(&r);
      change(&k);
      k++;
      if (l > r) swap(l, r);
      int i1 = l / s, i2 = r / s;
      res = 0;
      for (int i = i1 + 1; i <= i2 - 1; i++) res += b[i][k];
      int kol = 0;
      for (deque<int>::iterator it = q[i1].begin(); it != q[i1].end(); it++) {
        if (i1 != i2) {
          if (kol >= l % s && (*it) == k) res++;
        } else {
          if (kol >= l % s && kol <= r % s && (*it) == k) res++;
        }
        kol++;
      }
      if (i1 != i2) {
        kol = 0;
        for (deque<int>::iterator it = q[i2].begin(); it != q[i2].end(); it++) {
          if (kol <= r % s && (*it) == k) res++;
          kol++;
        }
      }
      printf("%d\n", res);
    }
  }
  return 0;
}
