#include <bits/stdc++.h>
using namespace std;
deque<int> box[351];
int ile[351][100005];
int ans = 0, n, q, blocks;
inline void deal(int &x) { x = (x + ans - 1) % n + 1; }
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  blocks = sqrt(n);
  for (int i = 0; i < n; ++i) {
    int temp;
    cin >> temp;
    int block = i / blocks;
    box[block].push_back(temp);
    ile[block][temp]++;
  }
  cin >> q;
  while (q--) {
    int type, ll, rr, k;
    cin >> type >> ll >> rr;
    deal(ll);
    deal(rr);
    ll--;
    rr--;
    if (ll > rr) swap(ll, rr);
    if (type == 1) {
      int left = ll / blocks;
      int right = rr / blocks;
      if (left == right) {
        ll -= blocks * left;
        rr -= blocks * left;
        int value = box[left][rr];
        box[left].erase(box[left].begin() + rr);
        box[left].insert(box[left].begin() + ll, value);
      } else {
        ll -= blocks * left;
        rr -= blocks * right;
        int value = box[right][rr];
        box[left].insert(box[left].begin() + ll, value);
        ile[left][value]++;
        box[right].erase(box[right].begin() + rr);
        ile[right][value]--;
        for (int i = left; i < right; ++i) {
          int value = box[i].back();
          box[i].pop_back();
          ile[i][value]--;
          box[i + 1].push_front(value);
          ile[i + 1][value]++;
        }
      }
    } else {
      cin >> k;
      deal(k);
      ans = 0;
      int left = ll / blocks;
      int right = rr / blocks;
      ll -= blocks * left;
      rr -= blocks * right;
      if (left == right) {
        for (int i = ll; i <= rr; ++i) {
          if (box[left][i] == k) ans++;
        }
        cout << ans << endl;
      } else {
        for (int i = ll; i < box[left].size(); ++i) {
          if (box[left][i] == k) ans++;
        }
        for (int i = left + 1; i < right; ++i) {
          ans += ile[i][k];
        }
        for (int i = 0; i <= rr; ++i) {
          if (box[right][i] == k) ans++;
        }
        cout << ans << endl;
      }
    }
  }
}
