#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
const int SQRT = 317;
int n, a[MAXN], cnt[SQRT][MAXN];
deque<int> deq[SQRT];
int decode(int x, int y) { return ((x + y - 1) % n) + 1; }
int ask(int l, int r, int k) {
  int ret = 0;
  while (l < r && l % SQRT) {
    if (deq[l / SQRT][l % SQRT] == k) ret++;
    l++;
  }
  while (l < r && r % SQRT) {
    r--;
    if (deq[r / SQRT][r % SQRT] == k) ret++;
  }
  for (; l < r; l += SQRT) ret += cnt[l / SQRT][k];
  return ret;
}
void shift(int l, int r) {
  r--;
  if (l / SQRT == r / SQRT) {
    int gro = l / SQRT;
    int x = deq[gro][r % SQRT];
    for (int i = r; i >= l + 1; --i)
      deq[gro][i % SQRT] = deq[gro][(i - 1) % SQRT];
    deq[gro][l % SQRT] = x;
  } else {
    int lg = l / SQRT, rg = r / SQRT;
    int x = deq[rg][r % SQRT];
    deq[rg].erase(deq[rg].begin() + r % SQRT);
    deq[lg].insert(deq[lg].begin() + l % SQRT, x);
    cnt[rg][x]--;
    cnt[lg][x]++;
    for (int i = lg + 1; i < rg + 1; ++i) {
      deq[i].push_front(deq[i - 1].back());
      cnt[i][deq[i - 1].back()]++;
      cnt[i - 1][deq[i - 1].back()]--;
      deq[i - 1].pop_back();
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    deq[i / SQRT].push_back(a[i]);
    cnt[i / SQRT][a[i]]++;
  }
  int q;
  cin >> q;
  int ans = 0;
  for (int p = 0; p < q; ++p) {
    int t;
    cin >> t;
    int l, r;
    cin >> l >> r;
    l = decode(l, ans);
    r = decode(r, ans);
    if (l > r) swap(l, r);
    l--;
    if (t == 2) {
      int k;
      cin >> k;
      k = decode(k, ans);
      ans = ask(l, r, k);
      cout << ans << "\n";
    } else
      shift(l, r);
  }
  return 0;
}
