#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int B = 512;
int n, m, a[N], bl[N], bll[N], blr[N], cnt[N / B + 5][N];
list<int> lst[N / B + 5];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1, l = 1, r; l <= n; l = r + 1, ++i) {
    r = min(n, l + B - 1);
    bll[i] = l;
    blr[i] = r;
    for (int j = l; j <= r; ++j) {
      bl[j] = i;
      ++cnt[i][a[j]];
      lst[i].push_back(a[j]);
    }
  }
  scanf("%d", &m);
  int ans = 0;
  while (m--) {
    int type, l, r;
    scanf("%d%d%d", &type, &l, &r);
    l = (l + ans - 1) % n + 1;
    r = (r + ans - 1) % n + 1;
    if (l > r) swap(l, r);
    if (type == 1) {
      auto it = lst[bl[r]].begin();
      for (int i = bll[bl[r]]; i < r; ++i) ++it;
      int tmp = *it;
      --cnt[bl[r]][tmp];
      ++cnt[bl[l]][tmp];
      lst[bl[r]].erase(it);
      it = lst[bl[l]].begin();
      for (int i = bll[bl[l]]; i < l; ++i) ++it;
      lst[bl[l]].insert(it, tmp);
      for (int i = bl[l]; i < bl[r]; ++i) {
        int t = lst[i].back();
        lst[i].pop_back();
        lst[i + 1].push_front(t);
        --cnt[i][t];
        ++cnt[i + 1][t];
      }
    } else {
      int k;
      scanf("%d", &k);
      k = (k + ans - 1) % n + 1;
      ans = 0;
      if (bl[l] == bl[r]) {
        auto it = lst[bl[l]].begin();
        for (int i = bll[bl[l]]; i <= r; ++i) {
          if (i >= l) ans += *it == k;
          ++it;
        }
      } else {
        auto it = lst[bl[l]].begin();
        for (int i = bll[bl[l]]; i <= blr[bl[l]]; ++i) {
          if (i >= l) ans += *it == k;
          ++it;
        }
        it = lst[bl[r]].begin();
        for (int i = bll[bl[r]]; i <= r; ++i) {
          ans += *it == k;
          ++it;
        }
        for (int i = bl[l] + 1; i < bl[r]; ++i) ans += cnt[i][k];
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
