#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
const int SQ = 376;
int fen[SQ][maxN], a[maxN];
inline void add(int ps, int n, int k) { fen[ps][n] += k; }
inline int get(int ps, int n) { return fen[ps][n]; }
deque<int> d[SQ];
void shift(int l, int r) {
  int stl = l / SQ;
  int str = r / SQ;
  if (stl == str) {
    int ck = d[stl][r % SQ];
    for (int i = (r % SQ) - 1; i >= l % SQ; i--) d[stl][i + 1] = d[stl][i];
    d[stl][l % SQ] = ck;
    return;
  }
  vector<int> fake;
  int tmp = r % SQ;
  tmp++;
  while (tmp) fake.push_back(d[str].front()), d[str].pop_front(), tmp--;
  int ck = fake.back();
  fake.pop_back();
  add(str, ck, -1);
  while (fake.size()) d[str].push_front(fake.back()), fake.pop_back();
  while (d[stl].size() > l % SQ)
    fake.push_back(d[stl].back()), d[stl].pop_back();
  d[stl].push_back(ck);
  add(stl, ck, 1);
  while (fake.size() > 1) d[stl].push_back(fake.back()), fake.pop_back();
  ck = fake.back();
  add(stl, ck, -1);
  for (int i = stl + 1; i < str; i++) {
    add(i, ck, 1);
    d[i].push_front(ck);
    ck = d[i].back();
    d[i].pop_back();
    add(i, ck, -1);
  }
  add(str, ck, 1);
  d[str].push_front(ck);
}
int gett(int l, int r, int k) {
  int stl = l / SQ;
  int str = r / SQ;
  int cnt = 0;
  for (int i = stl + 1; i < str; i++) cnt += get(i, k);
  for (int i = l % SQ; i < d[stl].size(); i++) {
    if (d[stl][i] == k) cnt++;
    if (stl == str && r % SQ == i) break;
  }
  if (stl < str) {
    for (int i = 0; i < (r % SQ) + 1; i++)
      if (d[str][i] == k) cnt++;
  }
  return cnt;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    int ps = i / SQ;
    d[ps].push_back(a[i]);
    add(ps, a[i], 1);
  }
  int ans = 0, q;
  cin >> q;
  while (q--) {
    int t, lp, rp;
    cin >> t >> lp >> rp;
    int l = (lp + ans - 1) % n, r = (rp + ans - 1) % n;
    if (l > r) swap(l, r);
    if (t == 1) {
      shift(l, r);
    } else {
      int kp;
      cin >> kp;
      int k = (kp + ans - 1) % n + 1;
      ans = gett(l, r, k);
      cout << ans << endl;
    }
  }
  return 0;
}
