#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 17, chk = 320;
deque<int> blocks[chk];
int ctr[chk][N];
int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q, c, l, r, k, prev, lidx, ridx, elem, st, en, num;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    blocks[i / chk].push_back(num);
    ctr[i / chk][num]++;
  }
  cin >> q;
  prev = 0;
  while (q--) {
    cin >> c >> l >> r;
    l = (l + prev - 1) % n;
    r = (r + prev - 1) % n;
    if (l > r) swap(l, r);
    lidx = l % chk;
    ridx = r % chk;
    st = l / chk;
    en = r / chk;
    elem = blocks[en][ridx];
    if (c == 1) {
      if (st == en) {
        blocks[en].erase(blocks[en].begin() + ridx);
        blocks[en].insert(blocks[en].begin() + lidx, elem);
      } else {
        blocks[en].erase(blocks[en].begin() + ridx);
        ctr[en][elem]--;
        for (int j = en; j > st; j--) {
          int x = blocks[j - 1].back();
          blocks[j - 1].pop_back();
          ctr[j - 1][x]--;
          blocks[j].push_front(x);
          ctr[j][x]++;
        }
        blocks[st].insert(blocks[st].begin() + lidx, elem);
        ctr[st][elem]++;
      }
    } else {
      cin >> k;
      k = (k + prev - 1) % n + 1;
      prev = 0;
      if (st == en) {
        for (int j = lidx; j <= ridx; j++)
          if (blocks[st][j] == k) prev++;
      } else {
        for (int j = lidx; j < chk; j++)
          if (blocks[st][j] == k) prev++;
        for (int j = 0; j <= ridx; j++)
          if (blocks[en][j] == k) prev++;
        for (int j = st + 1; j < en; j++) prev += ctr[j][k];
      }
      cout << prev << "\n";
    }
  }
  return 0;
}
