#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

bool bsf2(int high)
{
    int req = a[high + 1].first, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].first == req)
            return true;
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

bool bss2(int high)
{
    int req = a[high + 1].second, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].second == req)
            return true;
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first == req)
        {
            if (bsf2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].first <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second == req)
        {
            if (bss2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = max(a[i].first * 2,ans);
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[mid].first == req)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[mid].second == req)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    if (s[0] == '1')
        a[0].first++;
    else
        a[0].second++;
    for (int i = 1; i < n; i++)
    {
        if (s[i] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first + 1 - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first + 1 - a[i].second) + 1;
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bsf(i - 1, a[i].second + 1 - a[i].first) != -1)
                temp = i - bsf(i - 1, a[i].second + 1 - a[i].first) + 1;
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

bool bsf2(int high)
{
    int req = a[high + 1].first, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].first == req)
            return true;
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

bool bss2(int high)
{
    int req = a[high + 1].second, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].second == req)
            return true;
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first == req)
        {
            if (bsf2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].first <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second == req)
        {
            if (bss2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
        {
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                ans = max(bss(i - 1, a[i].second - a[i].first), ans);
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                ans = max(bsf(i - 1, a[i].first - a[i].second), ans);
        }
        else if (a[i].first > a[i].second)
        {
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                ans = max(i - bsf(i - 1, a[i].first - a[i].second), ans);
        }
        else
        {
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                ans = max(i - bss(i - 1, a[i].second - a[i].first), ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int ans, n, x;
    char y;
    stack <int> s;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> y;
        x = y - '0';
        if (s.empty())
        {
            s.push(x);
            continue;
        }
        if (s.top() < 2)
        {
            if (s.top() != x)
            {
                s.pop();
                if (!s.empty() && s.top() > 1)
                    s.top() += 2;
                else
                    s.push(2);
            }
            else
                s.push(x);
        }
        else
        {
            if (!s.empty())
            {
                ans = s.top();
                s.pop();
                if (s.empty())
                    s.push(ans);
                else if (s.top() == x)
                {
                    s.push(ans);
                    s.push(x);
                }
                else
                {
                    s.pop();
                    s.push(ans + 2);
                }
            }
        }
        if (s.top() > 1)
        {
            ans = s.top();
            s.pop();
            if (!s.empty())
                if (s.top() > 1)
                    ans += s.top();
            s.push(ans);
        }
    }
    ans = 0;
    while (!s.empty())
    {
        if (s.top() != 1)
            ans = max(ans, s.top());
        s.pop();
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);

int main()
{
    int n, mini1 = 0, mini0 = 0;
    pair<int, int> a[100005];
    string s;
    cin>>n>>s;
    a[0].first = 0;
    a[0].second = 0;
    if (s[0] == '1')
        a[0].first++;
    else
        a[0].second++;
    mini1 = max(mini1, a[0].first);
    mini0 = max(mini0, a[0].second);
    for (int i = 1; i < n; i++)
    {
        if (s[i] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        mini1 = max(mini1, a[i].first);
        mini0 = max(mini0, a[i].second);
    }
    cout<<min(mini1, mini0) * 2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first == req)
        {
            if (a[i].second - a[mid].second < a[i].first - a[mid].first)
                low = mid + 1;
            else
                high = mid - 1;
        }
        else if (a[mid].first <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second == req)
        {
            if (a[i].first - a[mid].first < a[i].second - a[mid].second)
                low = mid + 1;
            else
                high = mid - 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
        cout<<ans<<" "<<i<<endl;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int ans = 0, n;
    map <int,int> mp;
    pair <int, pair<int,int>> a[100005];
    char y;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> y;
        if (i == 0)
        {
            if (y == '1')
            {
                a[i].second.second = 1;
                a[i].second.first = 0;
            }
            else
            {
                a[i].second.second = 0;
                a[i].second.first = 1;
            }
        }
        else
        {
            if (y == '1')
            {
                a[i].second.second = a[i - 1].second.second + 1;
                a[i].second.first = a[i - 1].second.first;
            }
            else
            {
                a[i].second.second = a[i - 1].second.second;
                a[i].second.first = a[i - 1].second.first + 1;
            }
        }
        a[i].first = a[i].second.second - a[i].second.first;
        mp[a[i].first] = -1;
    }
    for (int i = 0; i < n; i++)
    {
        if (mp[a[i].first] == -1)
            mp[a[i].first] = i;
        else
            ans = max(ans, i - mp[a[i].first]);
        if (a[i].first == 0)
            ans = max(ans, a[i].second.first * 2);
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int> > a;

bool bsf2(int high)
{
    int req = a[high + 1].first, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].first == req)
            return true;
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

bool bss2(int high)
{
    int req = a[high + 1].second, low = 0;
    while (low <= high)
    {
        int mid = (high + low) / 2;
        if (a[mid].second == req)
            return true;
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first == req)
        {
            if (bsf2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].first <= req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second == req)
        {
            if (bss2(mid - 1))
                high = mid - 1;
            else
                low = mid + 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a.push_back({0,0});
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
            a.push_back({a[i - 1].first + 1, a[i - 1].second});
        else
            a.push_back({a[i - 1].first, a[i - 1].second + 1});
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define sz(a) a.size()
#define endl "\n"
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff1(x, y) scanf("%f%f",&x &y)
#define sff2(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define sall s.begin(),s.end()
#define vall v.begin(),v.end()
#define mod 10000000007
#define pi 3.1415926536
#define LLMX 1e18
#define LLMN -1e18
#define NotinRange i < 0 || i >= n || j < 0 || j >= m
//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in", "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
pair<int, int> a[100005];

int bsf(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].second - a[mid].second == a[i].first - a[mid].first && a[mid].first == req)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].first < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}

int bss(int high, int req)
{
    int low = 0, ans = -1, i = high + 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (a[i].first - a[mid].first == a[i].second - a[mid].second && a[mid].second == req)
        {
            ans = mid;
            high = mid - 1;
        }
        else if (a[mid].second < req)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return ans;
}
int main()
{
    int n, mini1, mini0, ans = 0;
    string s;
    cin >> n >> s;
    a[0].first = 0;
    a[0].second = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i - 1] == '1')
        {
            a[i].first = a[i - 1].first + 1;
            a[i].second = a[i - 1].second;
        }
        else
        {
            a[i].first = a[i - 1].first;
            a[i].second = a[i - 1].second + 1;
        }
        if (a[i].first == a[i].second)
            ans = a[i].first * 2;
        else if (a[i].first > a[i].second)
        {
            int temp = 0;
            if (bsf(i - 1, a[i].first - a[i].second) != -1)
                temp = i - bsf(i - 1, a[i].first - a[i].second);
            ans = max(temp, ans);
        }
        else
        {
            int temp = 0;
            if (bss(i - 1, a[i].second - a[i].first) != -1)
                temp = i - bss(i - 1, a[i].second - a[i].first);
            ans = max(temp, ans);
        }
    }
    cout<<ans;
    return 0;
}


/*************************************************************************
    > File Name: D.cpp
    > Author: Roundgod
    > Mail: wcysai@foxmail.com
    > Created Time: 2018-08-14 15:58:24
 ************************************************************************/

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,q,a[MAXN],num;
const int blocks=300;
int cnt[505][MAXN];
vector<int> arr[505];
void rebuild()
{
    int tot=0;
    for(int i=1;i<=num;i++)
        for(int j=0;j<(int)arr[i].size();j++)
        {
            cnt[i][arr[i][j]]--;
            a[++tot]=arr[i][j];
        }
    assert(tot==n);
    for(int i=1;i<=num;i++) arr[i].clear();
    for(int i=1;i<=num;i++)
    {
        for(int j=(i-1)*blocks+1;j<=min(i*blocks,n);j++)
        {
            arr[i].push_back(a[j]);
            cnt[i][a[j]]++;
        }
    }
}
int del(int pos)
{
    int v;
    for(int i=1;i<=num;i++)
    {
        if((int)arr[i].size()<pos)
        {
            pos-=(int)arr[i].size();
            continue;
        }
        v=arr[i][pos-1];
        cnt[i][v]--;
        arr[i].erase(arr[i].begin()+pos-1);
    }
    return v;
}
void insert(int pos,int v)
{
    for(int i=1;i<=num;i++)
    {
        if((int)arr[i].size()<pos)
        {
            pos-=(int)arr[i].size();
            continue;
        }
        cnt[i][v]++;
        arr[i].insert(arr[i].begin()+pos,v);
    }
}
int query(int l,int r,int k)
{
    int now=1,ans=0;
    for(int i=1;i<=num;i++)
    {
        if(now>=l&&now+(int)arr[i].size()-1<=r)
        {
            ans+=cnt[i][k];
            now+=(int)arr[i].size();
            continue;
        }
        if(now+(int)arr[i].size()-1<l) continue;
        for(int j=0;j<(int)arr[i].size();j++)
        {
            if(now>=l&&now<=r&&arr[i][j]==k) ans++;
            now++;
        }
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    num=(n-1)/blocks+1;
    for(int i=1;i<=num;i++)
    {
        for(int j=(i-1)*blocks+1;j<=min(i*blocks,n);j++)
        {
            arr[i].push_back(a[j]);
            cnt[i][a[j]]++;
        }
    }
    scanf("%d",&q);
    int t,l,r,k,lastans=0;
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d",&l,&r);
            l=(l+lastans-1)%n+1;r=(r+lastans-1)%n+1;
            if(l>r) swap(l,r);
            int v=del(r);
            insert(l-1,v);
        }
        else
        {
            scanf("%d%d%d",&l,&r,&k);
            l=(l+lastans-1)%n+1;r=(r+lastans-1)%n+1;k=(k+lastans-1)%n+1;
            if(l>r) swap(l,r);
            lastans=query(l,r,k);
            printf("%d\n",lastans);
        }
        if(i%blocks==0) rebuild();
    }
    return 0;
}


/*************************************************************************
    > File Name: D.cpp
    > Author: Roundgod
    > Mail: wcysai@foxmail.com
    > Created Time: 2018-08-14 15:58:24
 ************************************************************************/

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,q,a[MAXN],num;
const int blocks=300;
int cnt[405][MAXN];
vector<int> arr[405];
void rebuild()
{
    int tot=0;
    for(int i=1;i<=num;i++)
        for(int j=0;j<(int)arr[i].size();j++)
        {
            cnt[i][arr[i][j]]--;
            a[++tot]=arr[i][j];
        }
    assert(tot==n);
    for(int i=1;i<=num;i++) arr[i].clear();
    for(int i=1;i<=num;i++)
    {
        for(int j=(i-1)*blocks+1;j<=min(i*blocks,n);j++)
        {
            arr[i].push_back(a[j]);
            cnt[i][a[j]]++;
        }
    }
}
int del(int pos)
{
    int v;
    for(int i=1;i<=num;i++)
    {
        if((int)arr[i].size()<pos)
        {
            pos-=(int)arr[i].size();
            continue;
        }
        v=arr[i][pos-1];
        cnt[i][v]--;
        arr[i].erase(arr[i].begin()+pos-1);
        break;
    }
    return v;
}
void insert(int pos,int v)
{
    for(int i=1;i<=num;i++)
    {
        if((int)arr[i].size()<pos)
        {
            pos-=(int)arr[i].size();
            continue;
        }
        cnt[i][v]++;
        arr[i].insert(arr[i].begin()+pos,v);
        break;
    }
}
int query(int l,int r,int k)
{
    int now=1,ans=0;
    for(int i=1;i<=num;i++)
    {
        if(now>=l&&now+(int)arr[i].size()-1<=r)
        {
            ans+=cnt[i][k];
            now+=(int)arr[i].size();
            continue;
        }
        if(now+(int)arr[i].size()-1<l) continue;
        for(int j=0;j<(int)arr[i].size();j++)
        {
            if(now>=l&&now<=r&&arr[i][j]==k) ans++;
            now++;
        }
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    num=(n-1)/blocks+1;
    for(int i=1;i<=num;i++)
    {
        for(int j=(i-1)*blocks+1;j<=min(i*blocks,n);j++)
        {
            arr[i].push_back(a[j]);
            cnt[i][a[j]]++;
        }
    }
    scanf("%d",&q);
    int t,l,r,k,lastans=0;
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d",&l,&r);
            l=(l+lastans-1)%n+1;r=(r+lastans-1)%n+1;
            if(l>r) swap(l,r);
            int v=del(r);
            insert(l-1,v);
        }
        else if(t==2)
        {
            scanf("%d%d%d",&l,&r,&k);
            l=(l+lastans-1)%n+1;r=(r+lastans-1)%n+1;k=(k+lastans-1)%n+1;
            if(l>r) swap(l,r);
            lastans=query(l,r,k);
            printf("%d\n",lastans);
        }
        else
        {
            rebuild();
            for(int i=1;i<=n;i++) printf("%d ",a[i]);
            puts("");
        }
        if(i%blocks==0) rebuild();
    }
    return 0;
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 317 ;
ll SQRTcnt[320][N] , n , q , lastans;
deque<int> SQRT[320];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>=rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=tmp.back();
	tmp.pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	for(int i=pack1 ; i<pack2 ; i++)
	{
		int x = SQRT[i].back();
		SQRT[i].pop_back();

		SQRT[i+1].push_front(x);

		SQRTcnt[i][x]--;
		SQRTcnt[i+1][x]++;
	}

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

}

int getres(int s , int e , int k)
{
	int sum = 0 , mins=0 , pack1 = s/pack , pack2 = 2/pack;

	for(int i=pack1 ; i<=pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=0 ; i<s%pack ; i++)
		if(SQRT[pack1][i]==k)
			mins++;

	for(int i=SQRT[pack2].size()-1 ; i>e%pack ; i--)
		if(SQRT[pack2][i]==k)
			mins++;
	//cout << sum << " " << mins << " " << SQRT[pack1][k] << "<- " << endl ;
	return sum - mins ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 316+10 ;
ll SQRTcnt[pack][N] , n , q , lastans;
deque<int> SQRT[pack];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=SQRT[pack2].back();
	SQRT[pack2].pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

	while(SQRT[pack1].size() > pack)
	{
		int x = SQRT[pack1].back();
		SQRT[pack1].pop_back();
		SQRT[pack1+1].push_front(x);

		SQRTcnt[pack1][x]--;
		SQRTcnt[pack1+1][x]++;

		pack1++;
	}
}

int getres(int s , int e , int k)
{
	int sum = 0 , pack1 = s/pack , pack2 = e/pack;

	if(pack1 == pack2)
	{
		for(int i=s%pack ; i<=e%pack ; i++)
			if(SQRT[pack1][i]==k)
				sum++;

		return sum ;
	}

	for(int i=pack1+1 ; i<pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=s%pack ; i<SQRT[pack1].size() ; i++)
		if(SQRT[pack1][i]==k)
			sum++;

	for(int i=0 ; i<=e%pack ; i++)
		if(SQRT[pack2][i]==k)
			sum++;

	return sum ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}

	return 0;
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 316 ;
ll SQRTcnt[320][N] , n , q , lastans;
deque<int> SQRT[320];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>=rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=tmp.back();
	tmp.pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	for(int i=pack1 ; i<pack2 ; i++)
	{
		int x = SQRT[i].back();
		SQRT[i].pop_back();

		SQRT[i+1].push_front(x);

		SQRTcnt[i][x]--;
		SQRTcnt[i+1][x]++;
	}

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

}

int getres(int s , int e , int k)
{
	int sum = 0 , mins=0 , pack1 = s/pack , pack2 = e/pack;

	for(int i=pack1 ; i<=pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=0 ; i<s%pack ; i++)
		if(SQRT[pack1][i]==k)
			mins++;

	for(int i=SQRT[pack2].size()-1 ; i>e%pack ; i--)
		if(SQRT[pack2][i]==k)
			mins++;
	//cout << sum << " " << mins << " " << SQRT[pack1][k] << "<- " << endl ;
	return sum - mins ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 316+10 ;
ll SQRTcnt[pack][N] , n , q , lastans;
deque<int> SQRT[pack];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>=rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=tmp.back();
	tmp.pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

	for(int i=pack1 ; i<pack2 ; i++)
	{
		if(SQRT[i].size() > pack)
		{
			int x = SQRT[i].back();
			SQRT[i].pop_back();

			SQRT[i+1].push_front(x);

			SQRTcnt[i][x]--;
			SQRTcnt[i+1][x]++;
		}
	}
}

int getres(int s , int e , int k)
{
	int sum = 0 , mins=0 , pack1 = s/pack , pack2 = 2/pack;

	for(int i=pack1 ; i<=pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=0 ; i<s%pack ; i++)
		if(SQRT[pack1][i]==k)
			mins++;

	for(int i=SQRT[pack2].size()-1 ; i>e%pack ; i--)
		if(SQRT[pack2][i]==k)
			mins++;
	//cout << sum << " " << mins << " " << SQRT[pack1][k] << "<- " << endl ;
	return sum - mins ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}

	return 0;
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 316 ;
ll SQRTcnt[320][N] , n , q , lastans;
deque<int> SQRT[320];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>=rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=tmp.back();
	tmp.pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	for(int i=pack1 ; i<pack2 ; i++)
	{
		int x = SQRT[i].back();
		SQRT[i].pop_back();

		SQRT[i+1].push_front(x);

		SQRTcnt[i][x]--;
		SQRTcnt[i+1][x]++;
	}

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

}

int getres(int s , int e , int k)
{
	int sum = 0 , pack1 = s/pack , pack2 = e/pack;

	if(pack1 == pack2)
	{
		for(int i=s%pack ; i<=e%pack ; i++)
			if(SQRT[pack1][i]==k)
				sum++;

		return sum ;
	}

	for(int i=pack1+1 ; i<pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=s%pack ; i<SQRT[pack1].size() ; i++)
		if(SQRT[pack1][i]==k)
			sum++;

	for(int i=0 ; i<=e%pack ; i++)
		if(SQRT[pack2][i]==k)
			sum++;

	return sum ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}
}


#include <map>
#include <deque>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define mp make_pair

typedef long long ll ;
typedef pair<ll , ll> PII;

const int N=1e5+10 , pack = 316 ;
ll SQRTcnt[320][N] , n , q , lastans;
deque<int> SQRT[320];

void modify(int L , int R)
{
	int pack1 = L/pack , pack2 = R/pack , rem = (R%pack) , add;
	vector<int> tmp ;

	for(int i=SQRT[pack2].size()-1 ; i>=rem ; i--)
	{
		tmp.push_back(SQRT[pack2].back());
		SQRT[pack2].pop_back();
	}

	add=tmp.back();
	tmp.pop_back();

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack2].push_back(tmp[i]);

	for(int i=pack1 ; i<pack2 ; i++)
	{
		int x = SQRT[i].back();
		SQRT[i].pop_back();

		SQRT[i+1].push_front(x);

		SQRTcnt[i][x]--;
		SQRTcnt[i+1][x]++;
	}

	tmp.clear();

	for(int i=0 ; i<(L%pack) ; i++)
	{
		tmp.push_back(SQRT[pack1].front());
		SQRT[pack1].pop_front();
	}

	SQRT[pack1].push_front(add);

	for(int i=tmp.size()-1 ; i>=0 ; i--)
		SQRT[pack1].push_front(tmp[i]);

	SQRTcnt[pack1][add]++;
	SQRTcnt[pack2][add]--;

}

int getres(int s , int e , int k)
{
	int sum = 0 , mins=0 , pack1 = s/pack , pack2 = 2/pack;

	for(int i=pack1 ; i<=pack2 ; i++)
		sum+=SQRTcnt[i][k];

	for(int i=0 ; i<s%pack ; i++)
		if(SQRT[pack1][i]==k)
			mins++;

	for(int i=SQRT[pack2].size()-1 ; i>e%pack ; i--)
		if(SQRT[pack2][i]==k)
			mins++;
	//cout << sum << " " << mins << " " << SQRT[pack1][k] << "<- " << endl ;
	return sum - mins ;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n ;

	for(int i=0 ; i<n ; i++)
	{
		int x;
		cin >> x;
		SQRT[i/pack].push_back(x);
		SQRTcnt[i/pack][x]++;
	}

	cin >> q;

	while(q--)
	{
		int t , s , e, k;
		cin >> t >> s >> e ;

		s = (s+lastans-1)%n ;
		e = (e+lastans-1)%n ;

		if(s>e)
			swap(s , e);

		if(t==1)
			modify(s , e);

		else
		{
			cin >> k ;
			k = (k+lastans-1)%n +1;

			lastans = getres(s , e , k);
			cout << lastans << endl ;
		}
	}
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 100020
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 340;

struct Bloco
{
	deque<int> nums;
	int freq[MAXN];

	Bloco()
	{
		memset(freq, 0, sizeof freq);
	}
};

vector<Bloco> blocos;
int N, M,no_bloco[MAXN];

void print();

void shift(int L, int R)
{
	int l = no_bloco[L];
	int r = no_bloco[R];

	int base = BLOCK * r;
	int pos = 0;

	while(base + pos < R) pos++;

	int x = blocos[r].nums[pos];
	blocos[r].freq[x]--;

	blocos[r].nums.erase(blocos[r].nums.begin() + pos);

	for(int i = l; i < r; i++)
	{
		blocos[i + 1].nums.push_front(blocos[i].nums.back());
		blocos[i + 1].freq[blocos[i].nums.back()]++;

		blocos[i].freq[blocos[i].nums.back()]--;
		blocos[i].nums.pop_back();
	}

	pos = 0;
	base = l * BLOCK;

	while(base + pos < L) pos++;

	blocos[l].nums.insert(blocos[l].nums.begin() + pos + 1, x);
	blocos[l].freq[x]++;

	// print();
}

int query(int L, int R, int k)
{

	int l = no_bloco[L];
	int r = no_bloco[R];

	int ans = 0;

	int base = l * BLOCK;
	int pos = 0;

	while(base + pos < L)
	{
		ans -= (blocos[l].nums[pos] == k);
		pos++;
	}

	base = r * BLOCK;
	pos = 0;

	while(base + pos <= R)
	{
		ans += (blocos[r].nums[pos] == k);
		pos++;
	}

	for(int i = l; i < r; i++)
		ans += blocos[i].freq[k];

	return ans;

}

void print()
{
	int qtd = 0, i = 0;

	while(qtd < N)
	{
		int sum = 0;
		while(qtd < N && sum < BLOCK)
		{
			cout << blocos[i].nums[sum] << " ";

			sum++;
			qtd++;
		}

		i++;
		qtd++;
	}

	cout << endl;
}

int main(int argc, char const *argv[])
{
	optimize;

	int lastans = 0, x, qtd = 0, op, l, r, k;

	cin >> N;

	blocos.push_back(Bloco());

	for(int i = 0; i < N; i++)
	{
		cin >> x;

		if(qtd == BLOCK) blocos.push_back(Bloco()), qtd = 0;

		qtd++;

		no_bloco[i] = i / BLOCK;

		blocos.back().nums.push_back(x);
		blocos.back().freq[x]++;
	}

	cin >> M;

	while(M--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;

		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 100020
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 333;

int freq[BLOCK][MAXN];

struct Bloco
{
	deque<int> nums;
};

vector<Bloco> blocos;
int N, M,no_bloco[MAXN];

void print();

void shift(int L, int R)
{
	int l = no_bloco[L];
	int r = no_bloco[R];

	int base = BLOCK * r;
	int pos = 0;

	while(base + pos < R) pos++;

	int x = blocos[r].nums[pos];
	freq[r][x]--;

	blocos[r].nums.erase(blocos[r].nums.begin() + pos);

	for(int i = l; i < r; i++)
	{
		blocos[i + 1].nums.push_front(blocos[i].nums.back());
		freq[i + 1][blocos[i].nums.back()]++;

		freq[i][blocos[i].nums.back()]--;
		blocos[i].nums.pop_back();
	}

	pos = 0;
	base = l * BLOCK;

	while(base + pos < L) pos++;

	blocos[l].nums.insert(blocos[l].nums.begin() + pos, x);
	freq[l][x]++;

	// print();
}

int query(int L, int R, int k)
{

	int l = no_bloco[L];
	int r = no_bloco[R];

	int ans = 0;

	int base = l * BLOCK;
	int pos = 0;

	while(base + pos < L)
	{
		ans -= (blocos[l].nums[pos] == k);
		pos++;
	}

	base = r * BLOCK;
	pos = 0;

	while(base + pos <= R)
	{
		ans += (blocos[r].nums[pos] == k);
		pos++;
	}

	for(int i = l; i < r; i++)
		ans += freq[i][k];

	return ans;

}

void print()
{
	int qtd = 0, i = 0;

	while(qtd < N)
	{
		int sum = 0;
		while(qtd < N && sum < BLOCK)
		{
			cout << blocos[i].nums[sum] << " ";

			sum++;
			qtd++;
		}

		i++;
		qtd++;
	}

	cout << endl;
}

int main(int argc, char const *argv[])
{
	optimize;

	int lastans = 0, x, qtd = 0, op, l, r, k;

	cin >> N;

	blocos.push_back(Bloco());

	for(int i = 0; i < N; i++)
	{
		cin >> x;

		if(qtd == BLOCK) blocos.push_back(Bloco()), qtd = 0;

		qtd++;

		no_bloco[i] = i / BLOCK;

		blocos.back().nums.push_back(x);
		freq[no_bloco[i]][x]++;
	}

	cin >> M;

	while(M--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;

		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 100010
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 333;

int N, nums[MAXN], freq[BLOCK][MAXN], no_bloco[MAXN];

struct No
{
	int V;

	No *prox, *ant;

	No(int v = 0)
	{
		V = v;
		prox = ant = NULL;
	}
};

struct Lista
{
	No *inicio, *fim;

	Lista()
	{
		inicio = fim = NULL;
	}

	void push_back(No &a)
	{
		if(inicio == NULL)
		{
			inicio = fim = &a;
		}
		else
		{
			fim->prox = &a;
			a.ant = fim;
			fim = &a;
		}
	}

	void print()
	{
		No *it = inicio;

		while(it != NULL)
		{
			cout << it->V << " ";
			it = it->prox;
		}

		cout << endl;
	}

	void printr()
	{
		No *it = fim;

		while(it != NULL)
		{
			cout << it->V << " ";
			it = it->ant;
		}

		cout << endl;
	}
};

Lista lista;

No *inicios[MAXN];
int qtd_blocos;

void print_inicios()
{
	for(int i = 0; i < qtd_blocos; i++)
		cout << inicios[i]->V << " ";
	cout << endl;
}

void shift(int L, int R)
{
	if(L == R) return;

	No *l = inicios[no_bloco[L]];
	No *r = inicios[no_bloco[R]];

	bool ok = false;

	int base = no_bloco[L] * BLOCK;
	if(base == L) ok = true;
	else while(base++ < L) l = l->prox;

	base = no_bloco[R] * BLOCK;
	while(base++ < R) r = r->prox;

	//O inicio da lista mudou
	if(L == 0)
		lista.inicio = r;

	//o fim da lista mudou
	if(R == N - 1)
		lista.fim = lista.fim->ant;

	//O(sqrt)
	for(int i = no_bloco[L] + 1; i <= no_bloco[R]; i++)
	{
		inicios[i] = inicios[i]->ant;

		freq[i - 1][inicios[i]->V]--;
		freq[i][inicios[i]->V]++;
	}

	freq[no_bloco[R]][r->V]--;
	freq[no_bloco[L]][r->V]++;

	if(ok)
		inicios[no_bloco[L]] = r;

	if(R != 0)
		r->ant->prox = r->prox;

	if(R != N - 1)
		r->prox->ant = r->ant;

	if(L != 0)
	{
		r->ant = l->ant;
		l->ant->prox = r;
	}

	r->prox = l;
	l->ant = r;

	// lista.print();
	// lista.printr();
	// print_inicios();
}

int query(int L, int R, int k)
{
	int ans = 0;

	No *l = inicios[no_bloco[L]];
	No *r = inicios[no_bloco[R]];

	int base = no_bloco[L] * BLOCK;
	while(base++ < L)
	{
		ans -= (l->V == k);
		l = l->prox;
	}

	base = no_bloco[R] * BLOCK;
	while(base++ < R)
	{
		ans += (r->V == k);
		r = r->prox;
	}
	ans += (r->V == k);

	for(int i = no_bloco[L]; i < no_bloco[R]; i++)
		ans += freq[i][k];

	return ans;
}

No nos[MAXN];

int main(int argc, char const *argv[])
{
	optimize;

	cin >> N;

	for(int i = 0; i < N; i++)
	{
		cin >> nos[i].V;

		lista.push_back(nos[i]);

		no_bloco[i] = i / BLOCK;

		//Esse cara é o inicio de um novo bloco
		if((BLOCK * qtd_blocos) == i)
			inicios[qtd_blocos++] = lista.fim;

		freq[no_bloco[i]][nos[i].V]++;
	}

	int Q, op, l, r, k;

	cin >> Q;

	int lastans = 0;

	while(Q--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;
		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			// cout << l << " " << r << " " << k << ": ";

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 100020
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 333;

struct Bloco
{
	deque<int> nums;
	int freq[MAXN];

	Bloco()
	{
		memset(freq, 0, sizeof freq);
	}
};

vector<Bloco> blocos;
int N, M,no_bloco[MAXN];

void print();

void shift(int L, int R)
{
	int l = no_bloco[L];
	int r = no_bloco[R];

	int base = BLOCK * r;
	int pos = 0;

	while(base + pos < R) pos++;

	int x = blocos[r].nums[pos];
	blocos[r].freq[x]--;

	blocos[r].nums.erase(blocos[r].nums.begin() + pos);

	for(int i = l; i < r; i++)
	{
		blocos[i + 1].nums.push_front(blocos[i].nums.back());
		blocos[i + 1].freq[blocos[i].nums.back()]++;

		blocos[i].freq[blocos[i].nums.back()]--;
		blocos[i].nums.pop_back();
	}

	pos = 0;
	base = l * BLOCK;

	while(base + pos < L) pos++;

	blocos[l].nums.insert(blocos[l].nums.begin() + pos + 1, x);
	blocos[l].freq[x]++;

	// print();
}

int query(int L, int R, int k)
{

	int l = no_bloco[L];
	int r = no_bloco[R];

	int ans = 0;

	int base = l * BLOCK;
	int pos = 0;

	while(base + pos < L)
	{
		ans -= (blocos[l].nums[pos] == k);
		pos++;
	}

	base = r * BLOCK;
	pos = 0;

	while(base + pos <= R)
	{
		ans += (blocos[r].nums[pos] == k);
		pos++;
	}

	for(int i = l; i < r; i++)
		ans += blocos[i].freq[k];

	return ans;

}

void print()
{
	int qtd = 0, i = 0;

	while(qtd < N)
	{
		int sum = 0;
		while(qtd < N && sum < BLOCK)
		{
			cout << blocos[i].nums[sum] << " ";

			sum++;
			qtd++;
		}

		i++;
		qtd++;
	}

	cout << endl;
}

int main(int argc, char const *argv[])
{
	optimize;

	int lastans = 0, x, qtd = 0, op, l, r, k;

	cin >> N;

	blocos.push_back(Bloco());

	for(int i = 0; i < N; i++)
	{
		cin >> x;

		if(qtd == BLOCK) blocos.push_back(Bloco()), qtd = 0;

		qtd++;

		no_bloco[i] = i / BLOCK;

		blocos.back().nums.push_back(x);
		blocos.back().freq[x]++;
	}

	cin >> M;

	while(M--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;

		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 100020
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 333;

int freq[BLOCK][MAXN];

struct Bloco
{
	deque<int> nums;
};

vector<Bloco> blocos;
int N, M,no_bloco[MAXN];

void print();

void shift(int L, int R)
{
	int l = no_bloco[L];
	int r = no_bloco[R];

	int base = BLOCK * r;
	int pos = 0;

	while(base + pos < R) pos++;

	int x = blocos[r].nums[pos];
	freq[r][x]--;

	blocos[r].nums.erase(blocos[r].nums.begin() + pos);

	for(int i = l; i < r; i++)
	{
		blocos[i + 1].nums.push_front(blocos[i].nums.back());
		freq[i + 1][blocos[i].nums.back()]++;

		freq[i][blocos[i].nums.back()]--;
		blocos[i].nums.pop_back();
	}

	pos = 0;
	base = l * BLOCK;

	while(base + pos < L) pos++;

	blocos[l].nums.insert(blocos[l].nums.begin() + pos + 1, x);
	freq[l][x]++;

	// print();
}

int query(int L, int R, int k)
{

	int l = no_bloco[L];
	int r = no_bloco[R];

	int ans = 0;

	int base = l * BLOCK;
	int pos = 0;

	while(base + pos < L)
	{
		ans -= (blocos[l].nums[pos] == k);
		pos++;
	}

	base = r * BLOCK;
	pos = 0;

	while(base + pos <= R)
	{
		ans += (blocos[r].nums[pos] == k);
		pos++;
	}

	for(int i = l; i < r; i++)
		ans += freq[i][k];

	return ans;

}

void print()
{
	int qtd = 0, i = 0;

	while(qtd < N)
	{
		int sum = 0;
		while(qtd < N && sum < BLOCK)
		{
			cout << blocos[i].nums[sum] << " ";

			sum++;
			qtd++;
		}

		i++;
		qtd++;
	}

	cout << endl;
}

int main(int argc, char const *argv[])
{
	optimize;

	int lastans = 0, x, qtd = 0, op, l, r, k;

	cin >> N;

	blocos.push_back(Bloco());

	for(int i = 0; i < N; i++)
	{
		cin >> x;

		if(qtd == BLOCK) blocos.push_back(Bloco()), qtd = 0;

		qtd++;

		no_bloco[i] = i / BLOCK;

		blocos.back().nums.push_back(x);
		freq[no_bloco[i]][x]++;
	}

	cin >> M;

	while(M--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;

		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define INF 2000000020
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define PB push_back
#define EB emplace_back
#define MOD 1000000007
#define PRIME 101
#define MAXN 200020
#define MAXL 23
#define endl '\n'

using namespace std;

// (1 + sqrt(5)) / 2
const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625;
const ld PI = 2.0 * acos(0.0);
const int BLOCK = 333;

int N, nums[MAXN], freq[BLOCK][MAXN], no_bloco[MAXN];

struct No
{
	int V;

	No *prox, *ant;

	No(int v = 0)
	{
		V = v;
		prox = ant = NULL;
	}
};

struct Lista
{
	No *inicio, *fim;

	Lista()
	{
		inicio = fim = NULL;
	}

	void push_back(No &a)
	{
		if(inicio == NULL)
		{
			inicio = fim = &a;
		}
		else
		{
			fim->prox = &a;
			a.ant = fim;
			fim = &a;
		}
	}

	void print()
	{
		No *it = inicio;

		while(it != NULL)
		{
			cout << it->V << " ";
			it = it->prox;
		}

		cout << endl;
	}

	void printr()
	{
		No *it = fim;

		while(it != NULL)
		{
			cout << it->V << " ";
			it = it->ant;
		}

		cout << endl;
	}
};

Lista lista;

No *inicios[MAXN];
int qtd_blocos;

void print_inicios()
{
	for(int i = 0; i < qtd_blocos; i++)
		cout << inicios[i]->V << " ";
	cout << endl;
}

void shift(int L, int R)
{
	if(L == R) return;

	No *l = inicios[no_bloco[L]];
	No *r = inicios[no_bloco[R]];

	bool ok = false;

	int base = no_bloco[L] * BLOCK;
	if(base == L) ok = true;
	else while(base++ < L) l = l->prox;

	base = no_bloco[R] * BLOCK;
	while(base++ < R) r = r->prox;

	//O inicio da lista mudou
	if(L == 0)
		lista.inicio = r;

	//o fim da lista mudou
	if(R == N - 1)
		lista.fim = lista.fim->ant;

	//O(sqrt)
	for(int i = no_bloco[L] + 1; i <= no_bloco[R]; i++)
	{
		inicios[i] = inicios[i]->ant;

		freq[i - 1][inicios[i]->V]--;
		freq[i][inicios[i]->V]++;
	}

	freq[no_bloco[R]][r->V]--;
	freq[no_bloco[L]][r->V]++;

	if(ok)
		inicios[no_bloco[L]] = r;

	if(R != 0)
		r->ant->prox = r->prox;

	if(R != N - 1)
		r->prox->ant = r->ant;

	if(L != 0)
	{
		r->ant = l->ant;
		l->ant->prox = r;
	}

	r->prox = l;
	l->ant = r;

	// lista.print();
	// lista.printr();
	// print_inicios();
}

int query(int L, int R, int k)
{
	int ans = 0;

	No *l = inicios[no_bloco[L]];
	No *r = inicios[no_bloco[R]];

	int base = no_bloco[L] * BLOCK;
	while(base++ < L)
	{
		ans -= (l->V == k);
		l = l->prox;
	}

	base = no_bloco[R] * BLOCK;
	while(base++ < R)
	{
		ans += (r->V == k);
		r = r->prox;
	}
	ans += (r->V == k);

	for(int i = no_bloco[L]; i < no_bloco[R]; i++)
		ans += freq[i][k];

	return ans;
}

No nos[MAXN];

int main(int argc, char const *argv[])
{
	optimize;

	cin >> N;

	for(int i = 0; i < N; i++)
	{
		cin >> nos[i].V;

		lista.push_back(nos[i]);

		no_bloco[i] = i / BLOCK;

		//Esse cara é o inicio de um novo bloco
		if((BLOCK * qtd_blocos) == i)
			inicios[qtd_blocos++] = lista.fim;

		freq[no_bloco[i]][nos[i].V]++;
	}

	int Q, op, l, r, k;

	cin >> Q;

	int lastans = 0;

	while(Q--)
	{
		cin >> op >> l >> r;

		l = ((l + lastans - 1) % N) + 1;
		r = ((r + lastans - 1) % N) + 1;
		if(l > r) swap(l, r);

		if(op == 1)
			shift(l - 1, r - 1);
		else
		{
			cin >> k;

			k = ((k + lastans - 1) % N) + 1;

			// cout << l << " " << r << " " << k << ": ";

			cout << (lastans = query(l - 1, r - 1, k)) << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=500;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++)
		v[i/M].push_back(s[i]),H[i/M][s[i]]++;
}

void Del (pair < int , int  > x) {
	H[x.F][v[x.F][x.S]]--;
	v[x.F].erase(v[x.F].begin()+x.S);
}

void Ins (pair < int  , int > x, int val) {
	v[x.F].insert(v[x.F].begin()+x.S,val);
	H[x.F][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n);
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l--,r--;
			if (l!=r) {
				lb=Get_Block(l),rb=Get_Block(r);
				int x=v[rb.F][rb.S];
				Del(rb),Ins(lb,x);
			}
			if (T%M==0) Restart();
		}
			else {
			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);

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) { if (v[lb.F][lb.S]==k) res++; lb.S++; }
				while (0<=rb.S) { if (v[rb.F][rb.S]==k) res++; rb.S--; }

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=325;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++)
		v[i/M].push_back(s[i]),H[i/M][s[i]]++;
}

void Del (pair < int , int  > x) {
	H[x.F][v[x.F][x.S]]--;
	v[x.F].erase(v[x.F].begin()+x.S);
}

void Ins (pair < int  , int > x, int val) {
	v[x.F].insert(v[x.F].begin()+x.S,val);
	H[x.F][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n),Ms;
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l--,r--;
			if (l==r) continue;

			lb=Get_Block(l),rb=Get_Block(r);
			Del(rb),Ins(lb,v[rb.F][rb.S]);
			if (T%M==0) Restart();
		}
			else {
			scanf("%d %d %d",&l,&r,&k);
			l=(l+lastans-1+n)%n,r=(r+lastans-1+n)%n,k=(k+lastans-1+n)%n+1;
			if (l>r) swap(l,r);

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) { if (v[lb.F][lb.S]==k) res++; lb.S++; }
				while (0<=rb.S) { if (v[rb.F][rb.S]==k) res++; rb.S--; }

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=325;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++)
		v[i/M].push_back(s[i]),H[i/M][s[i]]++;
}

void Del (pair < int , int  > x) {
	H[x.F][v[x.F][x.S]]--;
	v[x.F].erase(v[x.F].begin()+x.S);
}

void Ins (pair < int  , int > x, int val) {
	v[x.F].insert(v[x.F].begin()+x.S,val);
	H[x.F][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n);
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l=(l+lastans-1)%n,r=(r+lastans-1)%n;
			if (l>r) swap(l,r);

			if (l!=r) {
				lb=Get_Block(l),rb=Get_Block(r);
				int x=v[rb.F][rb.S];
				Del(rb),Ins(lb,x);
			}
			if (T%M==0) Restart();
		}
			else {
			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);

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) { if (v[lb.F][lb.S]==k) res++; lb.S++; }
				while (0<=rb.S) { if (v[rb.F][rb.S]==k) res++; rb.S--; }

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=500;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++) {
		v[i/M].push_back(s[i]);
		H[i/M][s[i]]++;
	}
}

void Del (pair < int , int  > x) {
	int block=x.F;
	int i=x.S;
	H[block][v[block][i]]--;
	v[block].erase(v[block].begin()+i);
}

void Ins (pair < int  , int > x, int val) {
	int block=x.F;
	int i=x.S;
	v[block].insert(v[block].begin()+i,val);
	H[block][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n),Ms;
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l--,r--;

			lb=Get_Block(l),rb=Get_Block(r);
			int x=v[rb.F][rb.S];
			Del(rb),Ins(lb,x);
			if (T%M==0) Restart();
		}
			else {
			scanf("%d %d %d",&l,&r,&k);
			l=(l+lastans-1)%n+1;
			r=(r+lastans-1)%n+1;
			k=(k+lastans-1)%n+1;
			if (l>r) swap(l,r);
			l--,r--;

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) {
					if (v[lb.F][lb.S]==k) res++;
					lb.S++;
				}

				while (0<=rb.S) {
					if (v[rb.F][rb.S]==k) res++;
					rb.S--;
				}

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=325;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++)
		v[i/M].push_back(s[i]),H[i/M][s[i]]++;
}

void Del (pair < int , int  > x) {
	H[x.F][v[x.F][x.S]]--;
	v[x.F].erase(v[x.F].begin()+x.S);
}

void Ins (pair < int  , int > x, int val) {
	v[x.F].insert(v[x.F].begin()+x.S,val);
	H[x.F][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n);
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l=(l+lastans-1)%n,r=(r+lastans-1)%n;
			if (l!=r) {
				lb=Get_Block(l),rb=Get_Block(r);
				int x=v[rb.F][rb.S];
				Del(rb),Ins(lb,x);
			}
			if (T%M==0) Restart();
		}
			else {
			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);

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) { if (v[lb.F][lb.S]==k) res++; lb.S++; }
				while (0<=rb.S) { if (v[rb.F][rb.S]==k) res++; rb.S--; }

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int Sq=500;
const int N=100005;

int n,q,M,Ms,lastans;

vector < int > v[Sq];
int H[Sq][N];

pair < int , int > Get_Block(int x) {
	int res=0;
	while (v[res].size()<=x)
		x-=v[res++].size();
	return {res,x};
}

void Restart () {
	vector < int >  s;

	for (int i=0; i<Ms; i++)  {
		for (int j=0; j<v[i].size(); j++)
			s.push_back(v[i][j]),H[i][v[i][j]]--;

		v[i].clear();
	}

	for (int i=0; i<s.size(); i++) {
		v[i/M].push_back(s[i]);
		H[i/M][s[i]]++;
	}
}

void Del (pair < int , int  > x) {
	int block=x.F;
	int i=x.S;
	H[block][v[block][i]]--;
	v[block].erase(v[block].begin()+i);
}

void Ins (pair < int  , int > x, int val) {
	int block=x.F;
	int i=x.S;
	v[block].insert(v[block].begin()+i,val);
	H[block][val]++;
}

main () {
	scanf("%d",&n);

	M=sqrt(n),Ms;
	M+=(M*M!=n);
	Ms=(n+M-1)/M;

	for (int i=0; i<n; i++)  {
		int x;
		scanf("%d",&x);
		v[i/M].push_back(x);
		H[i/M][x]++;
	}

	scanf("%d",&q);

	int T=0;

	for (int i=0; i<q; i++){
		int type,l,r,k;
		pair < int , int > lb,rb;
		scanf("%d",&type);

		if (type==1) {
			T++;
			scanf("%d %d",&l,&r);
			l--,r--;

			lb=Get_Block(l),rb=Get_Block(r);
			int x=v[rb.F][rb.S];
			Del(rb),Ins(lb,x);
			if (T%M==0) Restart();
		}
			else {
			scanf("%d %d %d",&l,&r,&k);
			l=(l+lastans-1+n)%n,r=(r+lastans-1+n)%n,k=(k+lastans-1+n)%n+1;
			if (l>r) swap(l,r);

			lb=Get_Block(l),rb=Get_Block(r);

			if (lb.F==rb.F) {
				int block=lb.F,res=0;
				for (int j=lb.S; j<=rb.S; j++)
					if (v[block][j]==k) res++;
				printf("%d\n",res);
				lastans=res;
			}
				else {
				int res=0;

				while (lb.S<v[lb.F].size()) { if (v[lb.F][lb.S]==k) res++; lb.S++; }
				while (0<=rb.S) { if (v[rb.F][rb.S]==k) res++; rb.S--; }

				for (int j=lb.F+1; j<rb.F; j++)
					res+=H[j][k];

				printf("%d\n",res);
				lastans=res;
			}
		}
	}
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = min(n - 1, l + SQ - 1); i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            printf("%d\n", lst = get(l, r, x));
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = (min(n - 1, l + SQ - 1) & tof); i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (l == r) return;
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i & tof] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            lst = get(l, r, x);
            if (n == 100000) assert(0);
            printf("%d\n", lst = get(l, r, x));
            if (n == 100000 && A[1] == 65899) assert(lst);
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {/*
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = min(n - 1, l + SQ - 1); i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;*/
}

void update(ll l, ll r) {
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            printf("%d\n", lst = get(l, r, x));
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = (min(n - 1, l + SQ - 1) & tof); i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (l == r) return;
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i & tof] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            lst = get(l, r, x);
            if (n == 100000 && lst == 0) assert(0);
            printf("%d\n", lst = get(l, r, x));
            if (n == 100000 && A[1] == 65899) assert(lst);
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][(r & tof) + 1];
    for (ll i = (r & tof); i >= 0; i--) dq[r / SQ][i + 1] = dq[r / SQ][i];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = SQ - 1; i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (l == r) return;
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i & tof] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            lst = get(l, r, x);
            printf("%d\n", lst);
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = min(n - 1, l + SQ - 1); i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (i > sz(dq[i / SQ]) - 1) return 0;
            if (dq[i / SQ][i] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            printf("%d\n", lst = get(l, r, x));
        }
    }
    return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}
const ll MAXN = 1e5 + 10;
//const ll INF  = 8e18;
const ll MOD  = 1e9 + 7;
const ll SQ = 512;
const ll tof = 511;
template<class T , int SZ>
struct FastDeque{
    T Q[SZ * 2 + 10];
    int l = SZ , r = SZ;
    void push_back(T x)     {Q[r++] = x;}
    void push_front(T x)    {Q[--l] = x;}
    void pop_front()    {l++;}
    void pop_back()     {r--;}
    T front()   {return Q[l];}
    T back()    {return Q[r - 1];}
    void clear()    {l = r = SZ;}
    bool empty()    {return (l == r);}
    T get(ll id) {return Q[l + id];}
};
deque<ll> dq[SQ];
ll cnt[SQ][MAXN], A[MAXN], n, q, lst;

void updsame(ll l, ll r) {
    ll x = dq[l / SQ][r & tof];
    for (ll i = r & tof; i > (l & tof); i--) {
        dq[l / SQ][i] = dq[l / SQ][i - 1];
    }
    dq[l / SQ][l & tof] = x;
}

void updnot(ll l, ll r) {
    for (ll i = l - (l & tof) + SQ - 1; i < r; i += SQ) {
        ll x = dq[i / SQ].back();
        dq[i / SQ].pop_back();
        dq[i / SQ + 1].push_front(x);
        cnt[i / SQ][x]--;
        cnt[i / SQ + 1][x]++;
    }
    ll x = dq[r / SQ][r & tof];
    for (ll i = (r & tof); i > 0; i--) dq[r / SQ][i] = dq[r / SQ][i - 1];
    dq[r / SQ].pop_front();
    dq[l / SQ].push_back(x);
    for (ll i = min(n, SQ) - 1; i > (l & tof); i--) dq[l / SQ][i] = dq[l / SQ][i - 1];
    cnt[r / SQ][x]--, cnt[l / SQ][x]++;
    dq[l / SQ][l & tof] = x;
}

void update(ll l, ll r) {
    if (l == r) return;
    if (r / SQ == l / SQ) updsame(l, r);
    else updnot(l, r);
}

ll get(ll l, ll r, ll x, ll ans = 0) {
    for (ll i = l; i <= r;) {
        if ((i & tof) == 0 && i + SQ - 1 <= r) {
            ans += cnt[i / SQ][x];
            i += SQ;
        } else {
            if (dq[i / SQ][i & tof] == x) ans++;
            i++;
        }
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (ll i = 0; i < n; i++) {
        scanf("%d", &A[i]);
        cnt[i / SQ][A[i]]++;
        dq[i / SQ].push_back(A[i]);
    }
    scanf("%d", &q);
    while (q--) {
        ll l, r, t, x;
        scanf("%d%d%d", &t, &l, &r);
        l = (l + lst - 1) % n + 1;
        r = (r + lst - 1) % n + 1;
        if (r < l) swap(l, r);
        l--, r--;
        if (t == 1) {
            update(l, r);
        } else {
            scanf("%d", &x);
            x = (x + lst - 1) % n + 1;
            lst = get(l, r, x);
            printf("%d\n", lst);
        }
    }
    return 0;
}