#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, SQRT = 340;
int a[MAXN], cnt[SQRT][MAXN];
deque<int> q[MAXN];
void build(int n) {
  for (int i = 0; i < n; i++) {
    q[i / SQRT].push_back(a[i]);
    cnt[i / SQRT][a[i]]++;
  }
}
void calc(int l, int r) {
  int a2 = q[r / SQRT][r % SQRT];
  cnt[r / SQRT][a2]--;
  q[r / SQRT].erase(q[r / SQRT].begin() + (r % SQRT));
  cnt[l / SQRT][a2]++;
  q[l / SQRT].insert(q[l / SQRT].begin() + (l % SQRT), a2);
  for (int i = (l / SQRT); i < (r / SQRT); i++) {
    int d = q[i].back();
    q[i].pop_back();
    cnt[i][d]--;
    q[i + 1].push_front(d);
    cnt[i + 1][d]++;
  }
}
int get(int l, int r, int val) {
  int ans = 0;
  bool is1 = false, is2 = false;
  int x1 = l / SQRT, x2 = r / SQRT;
  while (l % SQRT != 0 and l <= r)
    ans += (q[x1][l % SQRT] == val), l++, is1 = true;
  while ((r + 1) % SQRT != 0 and l <= r)
    ans += (q[x2][r % SQRT] == val), r--, is2 = true;
  if (is1) x1++;
  if (is2) x2--;
  for (int i = x1; i <= x2; i++) ans += cnt[i][val];
  return ans;
}
int main() {
  int n, m;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  build(n);
  cin >> m;
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int l, r;
      cin >> l >> r;
      l = (l + ans - 1 + n) % n + 1;
      r = (r + ans - 1 + n) % n + 1;
      if (l > r) swap(l, r);
      calc(l - 1, r - 1);
    } else {
      int l, r, val;
      cin >> l >> r >> val;
      l = (l + ans - 1 + n) % n + 1;
      r = (r + ans - 1 + n) % n + 1;
      if (l > r) swap(l, r);
      val = (val + ans - 1 + n) % n + 1;
      cout << get(l - 1, r - 1, val) << endl;
      ans = get(l - 1, r - 1, val);
    }
  }
}
