#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int num = 0;
  char c = getchar();
  if (c == '-') return -read();
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num;
}
inline long long READ() {
  long long num = 0;
  char c = getchar();
  if (c == '-') return -READ();
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num;
}
deque<int> dq[357];
int n, m, q, mode, L, R, l, r, ans = 0, bb, bs;
int hvn[357][100007];
void deal(int& a) { a = (a + ans - 1) % n + 1; }
int main() {
  scanf("%d", &n);
  bs = int(sqrt(n)) + 1;
  for (int i = 0; i < n; i++) {
    scanf("%d", &bb);
    dq[i / bs].push_back(bb);
    hvn[i / bs][bb]++;
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &mode);
    if (mode == 1) {
      scanf("%d%d", &L, &R);
      deal(L);
      deal(R);
      L--;
      R--;
      if (L > R) swap(L, R);
      l = L / bs;
      L %= bs;
      r = R / bs;
      R %= bs;
      if (l == r) {
        int x = dq[l][R];
        dq[l].erase(dq[l].begin() + R);
        dq[l].insert(dq[l].begin() + L, x);
      } else {
        int x = dq[r][R];
        dq[r].erase(dq[r].begin() + R);
        hvn[r][x]--;
        for (int i = l; i < r; i++) {
          int y = dq[i].back();
          dq[i].pop_back();
          hvn[i][y]--;
          hvn[i + 1][y]++;
          dq[i + 1].push_front(y);
        }
        dq[l].insert(dq[l].begin() + L, x);
        hvn[l][x]++;
      }
    } else {
      int x;
      scanf("%d%d%d", &L, &R, &x);
      deal(L);
      deal(R);
      deal(x);
      L--;
      R--;
      if (L > R) swap(L, R);
      l = L / bs;
      L %= bs;
      r = R / bs;
      R %= bs;
      ans = 0;
      if (l == r) {
        for (int i = L; i <= R; i++)
          if (dq[l][i] == x) ans++;
      } else {
        for (int i = l + 1; i < r; i++) ans += hvn[i][x];
        for (int i = L; i < bs; i++)
          if (dq[l][i] == x) ans++;
        for (int i = R; i >= 0; i--)
          if (dq[r][i] == x) ans++;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
