#include <bits/stdc++.h>
using namespace std;
const int block = 320;
int cnt[block][100005];
deque<int> baris[block];
int main() {
  int n, q, a, l, r, block1, block2, urutan1, urutan2, ans, stat, tampung, k;
  ans = 0;
  scanf("%d", &n);
  baris[0].push_back(0);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    cnt[i / block][a]++;
    baris[i / block].push_back(a);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &stat, &l, &r);
    l = (l + ans - 1) % n + 1;
    r = (r + ans - 1) % n + 1;
    if (l > r) {
      int tmp = l;
      l = r;
      r = tmp;
    }
    block1 = l / block;
    urutan1 = l % block;
    block2 = r / block;
    urutan2 = r % block;
    if (stat == 1) {
      tampung = baris[block2][urutan2];
      baris[block2].erase(baris[block2].begin() + urutan2);
      baris[block1].insert(baris[block1].begin() + urutan1, tampung);
      if (block1 == block2) {
      } else {
        cnt[block2][tampung]--;
        cnt[block1][tampung]++;
        for (int j = block1; j < block2; j++) {
          tampung = baris[j].back();
          cnt[j][tampung]--;
          cnt[j + 1][tampung]++;
          baris[j].pop_back();
          baris[j + 1].push_front(tampung);
        }
      }
    } else {
      scanf("%d", &k);
      k = (k + ans - 1) % n + 1;
      ans = 0;
      if (block1 == block2) {
        for (int j = urutan1; j <= urutan2; j++) {
          if (baris[block1][j] == k) {
            ans++;
          }
        }
      } else {
        for (int j = urutan1; j < block; j++) {
          if (baris[block1][j] == k) ans++;
        }
        for (int j = 0; j <= urutan2; j++) {
          if (baris[block2][j] == k) ans++;
        }
        for (int j = block1 + 1; j < block2; j++) {
          ans += cnt[j][k];
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
