#include <bits/stdc++.h>
using namespace std;
const int NMAX = 335;
vector<vector<int> > B(NMAX);
int lastans, cnt[NMAX][100001];
int main() {
  int N;
  cin >> N;
  int len = (int)ceil(sqrt(N + .0));
  int num = 0, up = len;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (i == up) {
      up += len;
      num++;
    }
    B[num].push_back(a);
    cnt[num][a]++;
  }
  int M;
  cin >> M;
  for (int t = 0; t < M; t++) {
    int type;
    scanf("%d", &type);
    if (type == 1) {
      int l, r;
      scanf("%d%d", &l, &r);
      l = (l + lastans - 1) % N, r = (r + lastans - 1) % N;
      if (l > r) swap(l, r);
      int sum = 0, cur = 0;
      for (int i = 0; i < NMAX; i++) {
        if (r >= sum && r < sum + (int)B[i].size()) {
          cur = B[i][r - sum];
          B[i].erase(B[i].begin() + (r - sum));
          cnt[i][cur]--;
          break;
        }
        sum += (int)B[i].size();
      }
      sum = 0;
      for (int i = 0; i < NMAX; i++) {
        if (l >= sum && l < sum + (int)B[i].size()) {
          B[i].insert(B[i].begin() + (l - sum), cur);
          cnt[i][cur]++;
          break;
        }
        sum += (int)B[i].size();
      }
    } else {
      int l, r, k;
      scanf("%d%d%d", &l, &r, &k);
      l = (l + lastans - 1) % N, r = (r + lastans - 1) % N,
      k = (k + lastans - 1) % N + 1;
      if (l > r) swap(l, r);
      int sum = 0, ans = 0;
      for (int i = 0; i < NMAX; i++) {
        if (sum >= l && sum + (int)B[i].size() - 1 <= r)
          ans += cnt[i][k];
        else if ((l >= sum && l < sum + (int)B[i].size()) ||
                 (r >= sum && r < sum + (int)B[i].size())) {
          for (int j = 0; j < (int)B[i].size(); j++) {
            if (sum + j >= l && sum + j <= r && B[i][j] == k) ans++;
          }
        }
        sum += (int)B[i].size();
      }
      lastans = ans;
      printf("%d\n", ans);
    }
    if (t % len == 0) {
      vector<int> list;
      for (int i = 0; i < NMAX; i++) {
        for (int j = 0; j < (int)B[i].size(); j++) {
          list.push_back(B[i][j]);
          cnt[i][B[i][j]]--;
        }
        B[i].clear();
      }
      num = 0, up = len;
      for (int i = 0; i < (int)list.size(); i++) {
        if (i == up) {
          num++;
          up += len;
        }
        B[num].push_back(list[i]);
        cnt[num][list[i]]++;
      }
    }
  }
  return 0;
}
