#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
const int T = (1 << 18) + 3;
const int K = 1111;
vector<int> t[T];
int d;
int rcq(int l, int r, int x) {
  int res = 0;
  vector<int>::iterator it;
  for (l += d, r += d; l <= r; ++l >>= 1, --r >>= 1) {
    if (l & 1)
      res += upper_bound(t[l].begin(), t[l].end(), x) -
             lower_bound(t[l].begin(), t[l].end(), x);
    if (~r & 1)
      res += upper_bound(t[r].begin(), t[r].end(), x) -
             lower_bound(t[r].begin(), t[r].end(), x);
  }
  return res;
}
int st[K * 2 + 10][3], sn;
int a[N];
int f[N], sf[N];
void add(int i, int x) {
  sf[i] += x;
  for (; i < N; i |= i + 1) f[i] += x;
}
int get(int i) {
  int s = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) s += f[i];
  return s;
}
int index(int i, int n) {
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) >> 1;
    int k = get(m);
    if (k >= i)
      r = m;
    else
      l = m + 1;
  }
  return r;
}
void build(int *a, int n, bool first) {
  memset(sf, 0, sizeof sf);
  memset(f, 0, sizeof f);
  for (d = 2; d < n; d <<= 1)
    ;
  for (int i = 0; i < n; ++i) {
    if (first) t[i + d].resize(1);
    t[i + d][0] = a[i];
    add(i + 1, 1);
  }
  for (int i = d - 1; i; --i) {
    if (first) t[i].resize(t[i * 2].size() + t[i * 2 + 1].size());
    merge(t[i * 2].begin(), t[i * 2].end(), t[i * 2 + 1].begin(),
          t[i * 2 + 1].end(), t[i].begin());
  }
}
int b[N];
void rere(int n) {
  for (int i = 0; i < n; ++i) {
    sf[i + 1] += sf[i];
    b[i] = a[i];
    a[i] = -1;
  }
  for (int i = n - 1; i >= 0; --i) a[sf[i]] = b[i];
  for (int k = 0; k < sn; ++k) a[st[k][0]] = st[k][1];
  sn = 0;
  build(a, n, false);
}
int z[N];
void br1(int i, int j) { rotate(z + i, z + j, z + j + 1); }
int br2(int i, int j, int x) {
  int r = 0;
  for (int k = i; k <= j; ++k)
    if (z[k] == x) ++r;
  return r;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    z[i] = a[i];
  }
  build(a, n, true);
  int lans = 0;
  bool SUBMIT = 1;
  int qn;
  scanf("%d", &qn);
  while (qn--) {
    int tp, l, r, x;
    scanf("%d%d%d", &tp, &l, &r);
    if (SUBMIT) {
      l = (l + lans - 1) % n + 1;
      r = (r + lans - 1) % n + 1;
    }
    if (l > r) swap(l, r);
    l--;
    r--;
    int i = index(l, n);
    int j = index(r + 1, n);
    int j2 = index(r, n);
    if (tp == 1) {
      if (!SUBMIT) br1(l, r);
      if (l < r) {
        bool fl = false;
        for (int k = 0; k < sn; ++k) {
          if (l <= st[k][0] && st[k][0] < r)
            ++st[k][0];
          else if (st[k][0] == r) {
            st[k][0] = l;
            fl = true;
          }
        }
        if (!fl && i <= j2 && get(j2) == r) {
          int x = a[j2];
          st[sn][0] = l;
          st[sn][1] = x;
          st[sn][2] = j2;
          ++sn;
        }
        if (i < j) {
          add(i, +1);
          add(j2, -1);
        }
        if (sn >= K) rere(n);
      }
    } else if (tp == 2) {
      int x;
      scanf("%d", &x);
      if (SUBMIT) x = (x + lans - 1) % n + 1;
      int ans;
      if (!SUBMIT) ans = br2(l, r, x);
      int res = 0;
      if (i < j) {
        res += rcq(i, j - 1, x);
      }
      for (int k = 0; k < sn; ++k) {
        if (l <= st[k][0] && st[k][0] <= r && st[k][1] == x) ++res;
        if (i <= st[k][2] && st[k][2] <= j - 1 && st[k][1] == x) {
          --res;
        }
      }
      printf("%d\n", res);
      if (!SUBMIT) assert(ans == res);
      lans = res;
    }
  }
  return 0;
}
