#include <bits/stdc++.h>
using namespace std;
int ans = 0;
deque<int> dq[400];
int buc[350][100005];
int main() {
  int n;
  scanf("%d", &n);
  int div = sqrt(n);
  memset(buc, 0, sizeof(buc));
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    dq[i / div].push_back(x);
    buc[i / div][x]++;
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int typ, l, r;
    scanf("%d%d%d", &typ, &l, &r);
    l = (l + ans - 1) % n + 1;
    r = (r + ans - 1) % n + 1;
    if (l > r) swap(l, r);
    l--;
    int l1 = l / div, r1 = r / div, l2 = l % div, r2 = r % div;
    if (typ == 1) {
      if (l1 == r1) {
        r2--;
        int x = dq[r1][r2];
        dq[r1].erase(dq[r1].begin() + r2);
        dq[l1].insert(dq[l1].begin() + l2, x);
      } else {
        for (int i = l1; i < r1; i++) {
          int x = dq[i].back();
          dq[i].pop_back();
          buc[i][x]--;
          dq[i + 1].push_front(x);
          buc[i + 1][x]++;
        }
        int x = dq[r1][r2];
        dq[r1].erase(dq[r1].begin() + r2);
        buc[r1][x]--;
        dq[l1].insert(dq[l1].begin() + l2, x);
        buc[l1][x]++;
      }
    } else if (typ == 2) {
      int k;
      scanf("%d", &k);
      k = (k + ans - 1) % n + 1;
      ans = 0;
      if (l1 == r1)
        for (int i = l2; i < r2; i++) ans += (dq[l1][i] == k);
      else {
        for (int i = l1 + 1; i < r1; i++) ans += buc[i][k];
        for (int i = l2; i < dq[l1].size(); i++) ans += (dq[l1][i] == k);
        for (int i = 0; i < r2; i++) ans += (dq[r1][i] == k);
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
