#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int SQRT = 400;
deque<int> d[SQRT];
int cnt[SQRT][MAX], tmp[SQRT + 100], t;
void read(int x) {
  t = 0;
  for (deque<int>::iterator it = d[x].begin(); it != d[x].end(); it++)
    tmp[t++] = *it;
}
void recalc(int x) {
  while (!d[x].empty()) {
    cnt[x][d[x].back()]--;
    d[x].pop_back();
  }
  for (int i = 0; i < t; i++)
    if (tmp[i] != -1) {
      d[x].push_back(tmp[i]);
      cnt[x][tmp[i]]++;
    }
}
void rot(int l, int r) {
  if (l / SQRT == r / SQRT) {
    read(l / SQRT);
    rotate(tmp + l % SQRT, tmp + r % SQRT, tmp + r % SQRT + 1);
    recalc(l / SQRT);
  } else {
    for (int i = l / SQRT; i < r / SQRT; i++) {
      int val = d[i].back();
      d[i + 1].push_front(val);
      cnt[i + 1][val]++;
      d[i].pop_back();
      cnt[i][val]--;
    }
    read(r / SQRT);
    int last = tmp[r % SQRT + 1];
    tmp[r % SQRT + 1] = -1;
    recalc(r / SQRT);
    read(l / SQRT);
    for (int i = t; i > l % SQRT; i--) tmp[i] = tmp[i - 1];
    tmp[l % SQRT] = last;
    t++;
    recalc(l / SQRT);
  }
}
int get(int l, int r, int k) {
  int ans = 0;
  if (l / SQRT == r / SQRT) {
    read(l / SQRT);
    for (int i = l % SQRT; i < r % SQRT + 1; i++) ans += (tmp[i] == k);
  } else {
    read(l / SQRT);
    for (int i = l % SQRT; i < t; i++) ans += (tmp[i] == k);
    read(r / SQRT);
    for (int i = 0; i < r % SQRT + 1; i++) ans += (tmp[i] == k);
    for (int i = l / SQRT + 1; i < r / SQRT; i++) ans += cnt[i][k];
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int val;
    cin >> val;
    d[i / SQRT].push_back(val);
    cnt[i / SQRT][val]++;
  }
  int q;
  cin >> q;
  int last_ans = 0;
  while (q--) {
    int t, l, r;
    cin >> t >> l >> r;
    l = (l + last_ans - 1) % n;
    r = (r + last_ans - 1) % n;
    if (l > r) swap(l, r);
    if (t == 1)
      rot(l, r);
    else {
      int k;
      cin >> k;
      k = (k + last_ans - 1) % n + 1;
      last_ans = get(l, r, k);
      cout << last_ans << endl;
    }
  }
  return 0;
}
