#include <bits/stdc++.h>
const int MAXN = 1e5 + 19, MAXB = 400;
int n, b, q, belong[MAXN];
int lastans;
std::deque<int> a[MAXB];
int cnt[MAXB][MAXN];
inline void move(int node) {
  ++cnt[node + 1][a[node].back()];
  --cnt[node][a[node].back()];
  a[node + 1].push_front(a[node].back());
  a[node].pop_back();
}
void modify(int l, int r) {
  int vr = a[belong[r]][r - (belong[r] - 1) * b - 1];
  if (belong[l] == belong[r]) {
    for (int i = r - (belong[l] - 1) * b - 1; i >= l - (belong[l] - 1) * b; --i)
      a[belong[l]][i] = a[belong[l]][i - 1];
    a[belong[l]][l - (belong[l] - 1) * b - 1] = vr;
    return;
  }
  for (int i = belong[l]; i < belong[r]; ++i) move(i);
  ++cnt[belong[l]][vr];
  --cnt[belong[r]][vr];
  std::vector<int> tmp;
  int pos = l - (belong[l] - 1) * b - 1;
  for (int i = 0; i < a[belong[l]].size(); ++i) {
    if (i == pos) tmp.push_back(vr);
    tmp.push_back(a[belong[l]][i]);
  }
  if (pos == a[belong[l]].size()) tmp.push_back(vr);
  a[belong[l]] = std::deque<int>(tmp.begin(), tmp.end());
  tmp.clear();
  pos = r - (belong[r] - 1) * b;
  for (int i = 0; i < a[belong[r]].size(); ++i)
    if (i != pos) tmp.push_back(a[belong[r]][i]);
  a[belong[r]] = std::deque<int>(tmp.begin(), tmp.end());
}
int query(int l, int r, int k) {
  int ans = 0;
  if (belong[l] == belong[r]) {
    for (int i = l - (belong[l] - 1) * b - 1; i <= r - (belong[l] - 1) * b - 1;
         ++i)
      ans += (a[belong[l]][i] == k);
    return ans;
  }
  for (int i = belong[l] + 1; i < belong[r]; ++i) ans += cnt[i][k];
  for (int i = l - (belong[l] - 1) * b - 1; i < a[belong[l]].size(); ++i)
    ans += (a[belong[l]][i] == k);
  for (int i = 0; i <= r - (belong[r] - 1) * b - 1; ++i)
    ans += (a[belong[r]][i] == k);
  return ans;
}
int main() {
  std::scanf("%d", &n);
  b = std::sqrt(n + 1);
  for (int i = 1; i <= n; ++i) belong[i] = (i - 1) / b + 1;
  for (int i = 1; i <= n; ++i) {
    int tmp;
    std::scanf("%d", &tmp);
    a[belong[i]].push_back(tmp);
    ++cnt[belong[i]][tmp];
  }
  std::scanf("%d", &q);
  while (q--) {
    int opt, l, r;
    std::cin >> opt >> l >> r;
    l = (l + lastans - 1) % n + 1;
    r = (r + lastans - 1) % n + 1;
    if (l > r) std::swap(l, r);
    if (opt == 1)
      modify(l, r);
    else if (opt == 2) {
      int k;
      std::scanf("%d", &k);
      k = (k + lastans - 1) % n + 1;
      std::printf("%d\n", lastans = query(l, r, k));
    }
  }
  return 0;
}
