#include <bits/stdc++.h>
using namespace std;
const long long S = 320;
deque<long long> v[S];
long long b[S][100100], ls;
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v[i / S].push_back(x);
    b[i / S][x]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long tp, l, r;
    cin >> tp >> l >> r;
    l = (l + ls - 1) % n;
    r = (r + ls - 1) % n;
    if (l > r) swap(l, r);
    if (tp - 1) {
      long long k, ans = 0;
      cin >> k;
      k = ((k + ls - 1) % n) + 1;
      if (l / S == r / S) {
        for (long long i = l % S; i <= r % S; i++)
          if (v[l / S][i] == k) ans++;
      } else {
        for (long long i = l % S; i < v[l / S].size(); i++)
          if (v[l / S][i] == k) ans++;
        for (long long i = l / S + 1; i < r / S; i++) ans += b[i][k];
        for (long long i = 0; i <= r % S; i++)
          if (v[r / S][i] == k) ans++;
      }
      cout << (ls = ans) << endl;
    } else {
      b[l / S][v[r / S][r % S]]++;
      v[l / S].push_front(v[r / S][r % S]);
      for (long long i = 0; i < l % S; i++) swap(v[l / S][i], v[l / S][i + 1]);
      for (long long i = l / S; i < r / S; i++)
        b[i + 1][v[i].back()]++, v[i + 1].push_front(v[i].back()),
            b[i][v[i].back()]--, v[i].pop_back();
      for (long long i = r % S + 1; i < v[r / S].size() - 1; i++)
        swap(v[r / S][i], v[r / S][i + 1]);
      b[r / S][v[r / S].back()]--;
      v[r / S].pop_back();
    }
  }
  return 0;
}
