#include <bits/stdc++.h>
using namespace std;
const int C = 350, MAXN = 1e5 + C - 1, MAXC = MAXN / C;
int cnt[MAXC][MAXN];
deque<int> deq[MAXC];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int c = i / C, a;
    scanf("%d", &a);
    deq[c].push_back(a);
    cnt[c][a]++;
  }
  int q;
  cin >> q;
  int lastans = 0;
  while (q--) {
    int t, l, r;
    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--;
    if (t == 1) {
      int add = deq[(r - 1) / C][r - (r - 1) / C * C - 1];
      for (int s = l / C; s * C < r; s++) {
        int sl = s * C, sr = sl + C;
        if (l <= sl && r >= sr) {
          cnt[s][add]++;
          deq[s].push_front(add);
          add = deq[s].back();
          cnt[s][add]--;
          deq[s].pop_back();
        } else {
          int L = max(sl, l), R = min(sr, r), tmp = deq[s][R - 1 - sl];
          cnt[s][add]++;
          cnt[s][tmp]--;
          for (int i = R - 1 - sl; i > L - sl; i--) deq[s][i] = deq[s][i - 1];
          deq[s][L - sl] = add;
          add = tmp;
        }
      }
    } else {
      int k;
      scanf("%d", &k);
      k = ((k + lastans - 1) % n) + 1;
      int ans = 0;
      for (int s = l / C; s * C < r; s++) {
        int sl = s * C, sr = sl + C;
        if (l <= sl && r >= sr)
          ans += cnt[s][k];
        else {
          int L = max(l, sl), R = min(r, sr);
          for (int i = L; i < R; i++)
            if (deq[s][i - sl] == k) ans++;
        }
      }
      printf("%d\n", ans);
      lastans = ans;
    }
  }
}
