#include <bits/stdc++.h>
using namespace std;
const int N = 100005, sz = 320;
deque<int> block[sz];
int cnt[sz][N];
int main() {
  int n, num, t, l, k, r, lastans = 0, elem, st, en, lidx, ridx;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &num), block[i / sz].push_back(num), cnt[i / sz][num]++;
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d%d", &t, &l, &r);
    l = ((l + lastans - 1) % n) + 1;
    r = ((r + lastans - 1) % n) + 1;
    if (l > r) swap(l, r);
    l--;
    r--;
    lidx = l % sz;
    ridx = r % sz;
    st = l / sz;
    en = r / sz;
    elem = block[en][ridx];
    if (t == 1) {
      if (st == en) {
        block[en].erase(block[en].begin() + ridx);
        block[en].insert(block[en].begin() + lidx, elem);
      } else {
        block[en].erase(block[en].begin() + ridx);
        cnt[en][elem]--;
        for (int j = st; j < en; j++) {
          int sv = block[j].back();
          block[j].pop_back();
          cnt[j][sv]--;
          cnt[j + 1][sv]++;
          block[j + 1].push_front(sv);
        }
        block[st].insert(block[st].begin() + lidx, elem);
        cnt[st][elem]++;
      }
    } else {
      scanf("%d", &k);
      k = (k + lastans - 1) % n + 1;
      int res = 0;
      if (st == en) {
        for (int j = lidx; j <= ridx; j++)
          if (block[st][j] == k) res++;
      } else {
        for (int j = lidx; j < sz; j++)
          if (block[st][j] == k) res++;
        for (int j = 0; j <= ridx; j++)
          if (block[en][j] == k) res++;
        for (int j = st + 1; j < en; j++) res += cnt[j][k];
      }
      cout << res << "\n";
      lastans = res;
    }
  }
  return 0;
}
