#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int N = 1e5 + 10;
const int sz = 320;
int a[N], n, Q;
deque<int> q[320];
int cnt[320][N];
void update(int l, int r) {
  l--, r--;
  int lb = l / sz;
  int rb = r / sz;
  int v = q[rb][r % sz];
  q[rb].erase(q[rb].begin() + (r % sz));
  q[lb].insert(q[lb].begin() + (l % sz), v);
  cnt[rb][v]--;
  cnt[lb][v]++;
  while (lb < rb) {
    v = q[lb].back();
    q[lb].pop_back();
    cnt[lb][v]--;
    q[++lb].push_front(v);
    cnt[lb][v]++;
  }
}
int query(int l, int r, int k) {
  l--, r--;
  int lb = l / sz;
  int rb = r / sz;
  if (lb == rb) {
    int ret = 0;
    l %= sz;
    r %= sz;
    for (int i = l; i <= r; i++) ret += q[lb][i] == k;
    return ret;
  }
  int ret = 0;
  for (int i = l % sz; i < q[lb].size(); i++) ret += q[lb][i] == k;
  int lst = r % sz;
  for (int i = 0; i <= lst; i++) ret += q[rb][i] == k;
  for (int i = lb + 1; i < rb; i++) ret += cnt[i][k];
  return ret;
}
void print() {
  for (int i = 0;; i++) {
    if (q[i].size()) {
      for (int x : q[i]) cout << x << " ";
    } else
      break;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cnt[i / sz][a[i]]++;
    q[i / sz].push_back(a[i]);
  }
  scanf("%d", &Q);
  int last = 0;
  while (Q--) {
    int t, l, r, k;
    scanf("%d %d %d", &t, &l, &r);
    l = (l + last - 1) % n + 1;
    r = (r + last - 1) % n + 1;
    if (l > r) swap(l, r);
    if (t == 1) {
      update(l, r);
    } else {
      scanf("%d", &k);
      k = (k + last - 1) % n + 1;
      printf("%d\n", last = query(l, r, k));
    }
  }
}
