#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 21;
const int zn = 351;
int n, q, ans, dp[zn][maxn];
deque<int> Q[zn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    dp[i / zn][a]++;
    Q[i / zn].push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t, l, r;
    cin >> t >> l >> r;
    l = (l + ans - 1) % n;
    r = (r + ans - 1) % n;
    if (l > r) swap(l, r);
    int Lzone = l / zn, Rzone = r / zn;
    int Lmod = l % zn, Rmod = r % zn;
    if (t == 2) {
      int k;
      cin >> k;
      k = (k + ans - 1) % n;
      ans = 0;
      if (Lzone == Rzone) {
        for (int j = Lmod; j <= Rmod; j++)
          if (Q[Lzone][j] == k) ans++;
        cout << ans << "\n";
        continue;
      }
      for (int j = Lmod; j < zn; j++)
        if (Q[Lzone][j] == k) ans++;
      Lzone++;
      while (Lzone != Rzone) {
        ans += dp[Lzone][k];
        Lzone++;
      }
      for (int j = 0; j <= Rmod; j++)
        if (Q[Rzone][j] == k) ans++;
      cout << ans << "\n";
    } else {
      int x = Q[Rzone][Rmod];
      Q[Rzone].erase(Q[Rzone].begin() + (Rmod));
      dp[Rzone][x]--;
      Q[Lzone].insert(Q[Lzone].begin() + (Lmod), x);
      dp[Lzone][x]++;
      while (Lzone != Rzone) {
        x = Q[Lzone].back();
        Q[Lzone].pop_back();
        dp[Lzone][x]--;
        Q[Lzone + 1].push_front(x);
        dp[Lzone + 1][x]++;
        Lzone++;
      }
    }
  }
}
