#include <bits/stdc++.h>
using namespace std;
const int magic = 350;
const int maxn = 101000;
int a[maxn], cnt[magic][maxn];
deque<int> b[magic];
void modify(int l, int r) {
  --l;
  --r;
  int lg = l / magic, rg = r / magic;
  int ln = l - lg * magic, rn = r - rg * magic;
  int num = b[rg][rn];
  --cnt[rg][num];
  ++cnt[lg][num];
  b[rg].erase(b[rg].begin() + rn);
  b[lg].insert(b[lg].begin() + ln, num);
  for (int i = lg; i < rg; ++i) {
    num = b[i].back();
    b[i].pop_back();
    b[i + 1].push_front(num);
    --cnt[i][num];
    ++cnt[i + 1][num];
  }
}
int solve(int l, int r, int k) {
  --l;
  --r;
  int ret = 0;
  int lg = l / magic, rg = r / magic;
  for (int i = l - lg * magic, j = 0; i < magic && j < (r - l + 1); ++i, ++j) {
    if (b[lg][i] == k) ++ret;
  }
  if (lg == rg) return ret;
  for (int i = lg + 1; i < rg; ++i) ret += cnt[i][k];
  for (int i = 0; i <= r - rg * magic; ++i)
    if (b[rg][i] == k) ++ret;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    b[i / magic].push_back(a[i]);
    cnt[i / magic][a[i]]++;
  }
  int q, lastans = 0;
  cin >> q;
  while (q--) {
    int d, l, r;
    cin >> d >> l >> r;
    l = ((l + lastans - 1) % n) + 1;
    r = ((r + lastans - 1) % n) + 1;
    if (l > r) swap(l, r);
    if (d == 1) {
      modify(l, r);
    } else {
      int k;
      cin >> k;
      k = ((k + lastans - 1) % n) + 1;
      lastans = solve(l, r, k);
      cout << lastans << endl;
    }
  }
  return 0;
}
