#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;

int n, arr[maxn];
char str[maxn];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++ ) {
        scanf("%d", &arr[i]);
    }
    scanf("%s", str + 1);
    for(int i = 1; i <= n; i ++ ) {
        if(str[i] == '1') {
            int j = i;
            while(str[j] == '1') {
                j ++;
            }
            sort(arr + 1 + i, arr + 2 + j);
            i = j;
        }
    }
    bool ok = 1;
    for(int i = 1; i <= n - 1; i ++ ) {
        if(arr[i] >= arr[i + 1]) {
            ok = 0;
            break;
        }
    }
    printf("%s\n", ok ? "YES" : "NO");

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;

int n, arr[maxn], mi[maxn];
char str[maxn];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++ ) {
        scanf("%d", &arr[i]);
    }
    scanf("%s", str + 1);
    mi[n + 1] = 0x7fffffff;
    for(int i = n; i >= 1; i -- ) {
        mi[i] = min(arr[i], mi[i + 1]);
    }
    bool ok = 1;
    for(int i = 1; i <= n - 1; i ++ ) {
        if(str[i] == '0' && arr[i] > mi[i + 1]) {
            ok = 0;
            break;
        }
    }
    printf("%s\n", ok ? "YES" : "NO");

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;

int n, arr[maxn];
char str[maxn];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++ ) {
        scanf("%d", &arr[i]);
    }
    scanf("%s", str + 1);
    for(int i = 1; i <= n; i ++ ) {
        if(str[i] == '1') {
            int j = i;
            while(str[j] == '1') {
                j ++;
            }
            sort(arr + 1 + i, arr + 1 + j);
            i = j - 1;
        }
    }
    bool ok = 1;
    for(int i = 1; i <= n - 1; i ++ ) {
        if(arr[i] != i) {
            ok = 0;
            break;
        }
    }
    printf("%s\n", ok ? "YES" : "NO");

    return 0;
}


#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn =200000 + 20;
typedef pair<int,int> p;
int d[maxn],need[maxn];
char ok[maxn];
int main()
{
    int n;cin >> n;

    for(int i = 1; i <= n; i++) scanf("%d",&d[i]);
    for(int i = 1; i < n ; i++) scanf("%c",&ok[i]);

    for(int i =1; i < n; i++)
    {
        int a = i, b= d[i];
        if(a==b)continue;
        if(a > b)swap(a, b);

        for(int j = a; j < b; j++)
        {
            need[j] = 1;
            if(!(ok[j] - '0'))
            {
                cout <<"NO"<<endl;
                return 0;
            }
        }
    }
    cout <<"YES"<<endl;
    return 0;

}


#include<iostream>
using namespace std;
const int maxn = 200000+ 10;
char str[maxn];
int d[maxn];
int main()
{
    int n; cin >> n;
    for(int i = 1; i <=n; i++)cin >> d[i];
    cin >> (str+1);

    int mx =0;
    for(int i = 1; i< n; i++)
    {
        mx = max(mx,d[i]);
        if(str[i] == '0')
        {
            if(mx > i)
            {
                cout <<"NO"<<endl;
                return 0;
            }
        }
    }
    cout <<"YES"<<endl;
    return 0;

}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn =200000 + 20;
typedef pair<int,int> p;
int d[maxn],need[maxn];
char ok[maxn];
int main()
{
    int n;cin >> n;

    for(int i = 0; i < n; i++) cin >> d[i];
    //for(int i = 0; i < n- 1; i++) cin >> ok[i];
    cin >> ok;
    for(int i =0; i < n; i++)
        for(int j = 0; j < n - i- 1; j++)
        {
            if(d[j] > d[j+ 1])
            {
                swap(d[j],d[j+1]);
                need[j] = 1;
            }
        }

    int res = 1;
    for(int i = 0; i < n- 1; i++) if(need[i] == 1 && (ok[i] -'0') == 0) {res = 0;break;}
    if(res)
        cout <<"YES"<<endl;
    else
        cout <<"NO"<<endl;

}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn =200000 + 20;
typedef pair<int,int> p;
int d[maxn],need[maxn];
char ok[maxn];
int main()
{
    int n;cin >> n;

    for(int i = 0; i < n; i++) cin >> d[i];
    //for(int i = 0; i < n- 1; i++) cin >> ok[i];
    cin >> ok;

    for(int i =0; i < n; i++)
        for(int j = 0; j < n - i- 1; j++)
        {
            if(d[j] > d[j+ 1])
            {
                swap(d[j],d[j+1]);
                need[j] = 1;
                if(need[j] && (ok[j] -'0') == 0)
                {
                    cout <<"NO" <<endl;
                    return 0;
                }
            }
        }

    cout <<"YES"<<endl;
    return 0;

}


#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn =200000 + 20;
typedef pair<int,int> p;
int d[maxn],need[maxn];
char ok[maxn];
int main()
{
    int n;cin >> n;

    for(int i = 1; i <= n; i++) scanf("%d",&d[i]);
    for(int i = 1; i < n ; i++) scanf("%c",&ok[i]);
    for(int i =1; i <= n; i++)
    {
        int a = i, b= d[i];
        if(a==b)continue;
        if(a > b)swap(a, b);

        for(int j = a; j < b; j++)
        {
            need[j] = 1;
            if(!(ok[j] - '0'))
            {
                cout <<"NO"<<endl;
                return 0;
            }
        }
    }
    cout <<"YES"<<endl;
    return 0;

}


#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn =200000 + 20;
typedef pair<int,int> p;
int d[maxn],need[maxn];
char ok[maxn];
int main()
{
    int n;cin >> n;

    for(int i = 0; i < n; i++) scanf("%d",&d[i]); //cin >> d[i];
    //for(int i = 0; i < n- 1; i++) cin >> ok[i];
    cin >> ok;

    for(int i =0; i < n; i++)
        for(int j = 0; j < n - i- 1; j++)
        {
            if(d[j] > d[j+ 1])
            {
                swap(d[j],d[j+1]);
                need[j] = 1;
                if(need[j] && (ok[j] -'0') == 0)
                {
                    cout <<"NO" <<endl;
                    return 0;
                }
            }
        }

    cout <<"YES"<<endl;
    return 0;

}


#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, a[200010];
char str[200010];
int mx;
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    cin >> (str+1);
    int mx = 0;
    for(int i = 1; i <= n ;i++)
    {
        mx = max(mx, a[i]);
        if(str[i] == '0')
        {
            if(mx != i)
            {
                cout <<"NO"<<endl;
                return 0;
            }
        }
    }
    cout <<"YES"<<endl;

}


#include<stdio.h>
int main()
{
    int a,c,b,d,e,f,n;b=-1;
    scanf("%d",&n);
    int ara[n];
    char xx[n+1];
    for(a=0;a<n;a++)
    {
        scanf("%d",&ara[a]);
    }
    if(ara[n-1]!=n)
    {
        printf("NO\n");
        return 0;
    }
    scanf("%s",xx);
    //printf("%s\n",xx);
    for(a=1;a<=n;a++)
    {
        if((ara[a-1]!=a || ara[a-1]<b) && !(ara[a-1]<a && a<=b))
        {
            if(xx[a-1]!='1')
            {
                printf("NO\n");
                /*printf("%d    %d     %d\n",a,b,ara[a-1]);
                printf("%c\n",xx[a-1]);*/
                return 0;
            }
            if(b<ara[a-1])
                b=ara[a-1];
        }
    }
    printf("YES\n");
    return 0;
}


//perreo a poca luz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <assert.h>
#include <limits>
using namespace std;

typedef pair<int,int> ii;
typedef pair<char, ii> cii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

void fastIO() {
    std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define EPS (1e-12)
#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define FORE(i,c) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)

#define fst first
#define snd second

template<typename T, typename U> inline void mnze(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void mxze(T &x, U y) { if(x < y) x = y; }

void _scan( int &x ) { scanf("%d",&x); }
void _scan( long long &x ) { scanf("%lld",&x); }
void _scan( double &x ) { scanf("%lf",&x); }
void _scan( long double &x ) { scanf("%Lf",&x); }
void _scan( char &x ) { scanf(" %c",&x); }
void _scan( char *x ) { scanf("%s",x); }
void scan() {}
template<typename T, typename... U>
void scan( T& head, U&... tail ) { _scan(head); scan(tail...);}

template<typename T> void _dbg(const char* sdbg, T h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename T, typename... U> void _dbg(const char* sdbg, T h, U... t) {
    while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(_i, (x)) cerr <<*_i <<", "; cerr <<"\n"; }}
#define debuga(x, sz) {{cerr <<#x <<" = "; FOR(_i, 0, sz) cerr << x[_i] <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr if(0)cout
#endif

#define N 200009

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

int main(){
    int n, arr[N], check[N], acum[N];
    scan(n);
    FOR(i,0,n)
        scan(arr[i]);
    FOR(i,0,n-1)
        scanf("%1d", &check[i]);
    memset(acum,0,sizeof(acum));
    FOR(i,1,n)
        acum[i] = acum[i-1] + check[i];
    for(int i = 0; i < n; i++){
        int pos = arr[i]-1;
        if (pos == i)
            continue;
        if ((pos > i && acum[pos-1]-acum[i]+check[i] != arr[i]-i-1) || (pos < i && acum[pos]-acum[i-1]+check[pos] != i - pos)){
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


//perreo a poca luz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <assert.h>
#include <limits>
using namespace std;

typedef pair<int,int> ii;
typedef pair<char, ii> cii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

void fastIO() {
    std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define EPS (1e-12)
#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define FORE(i,c) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)

#define fst first
#define snd second

template<typename T, typename U> inline void mnze(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void mxze(T &x, U y) { if(x < y) x = y; }

void _scan( int &x ) { scanf("%d",&x); }
void _scan( long long &x ) { scanf("%lld",&x); }
void _scan( double &x ) { scanf("%lf",&x); }
void _scan( long double &x ) { scanf("%Lf",&x); }
void _scan( char &x ) { scanf(" %c",&x); }
void _scan( char *x ) { scanf("%s",x); }
void scan() {}
template<typename T, typename... U>
void scan( T& head, U&... tail ) { _scan(head); scan(tail...);}

template<typename T> void _dbg(const char* sdbg, T h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename T, typename... U> void _dbg(const char* sdbg, T h, U... t) {
    while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(_i, (x)) cerr <<*_i <<", "; cerr <<"\n"; }}
#define debuga(x, sz) {{cerr <<#x <<" = "; FOR(_i, 0, sz) cerr << x[_i] <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr if(0)cout
#endif

#define N 200009

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

int main(){
    int n, arr[N], check[N], acum[N];
    scan(n);
    FOR(i,0,n)
        scan(arr[i]);
    FOR(i,0,n-1)
        scanf("%1d", &check[i]);
    memset(acum,0,sizeof(acum));
    acum[0] = check[0];
    FOR(i,1,n)
        acum[i] = acum[i-1] + check[i];
    for(int i = 0; i < n; i++){
        int pos = arr[i]-1;
        if (pos == i)
            continue;
        if ((pos > i && acum[pos-1]-acum[i]+check[i] != arr[i]-i-1) || (pos < i && acum[pos]-acum[i-1]+check[pos] != i - pos)){
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


//perreo a poca luz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <assert.h>
#include <limits>
using namespace std;

typedef pair<int,int> ii;
typedef pair<char, ii> cii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

void fastIO() {
    std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define EPS (1e-12)
#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define FORE(i,c) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)

#define fst first
#define snd second

template<typename T, typename U> inline void mnze(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void mxze(T &x, U y) { if(x < y) x = y; }

void _scan( int &x ) { scanf("%d",&x); }
void _scan( long long &x ) { scanf("%lld",&x); }
void _scan( double &x ) { scanf("%lf",&x); }
void _scan( long double &x ) { scanf("%Lf",&x); }
void _scan( char &x ) { scanf(" %c",&x); }
void _scan( char *x ) { scanf("%s",x); }
void scan() {}
template<typename T, typename... U>
void scan( T& head, U&... tail ) { _scan(head); scan(tail...);}

template<typename T> void _dbg(const char* sdbg, T h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename T, typename... U> void _dbg(const char* sdbg, T h, U... t) {
    while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(_i, (x)) cerr <<*_i <<", "; cerr <<"\n"; }}
#define debuga(x, sz) {{cerr <<#x <<" = "; FOR(_i, 0, sz) cerr << x[_i] <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr if(0)cout
#endif

#define N 200009

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

int main(){
    int n, arr[N], check[N], acum[N];
    scan(n);
    FOR(i,0,n)
        scan(arr[i]);
    FOR(i,0,n)
        scanf("%1d", &check[i]);
    memset(acum,0,sizeof(acum));
    FOR(i,1,n)
        acum[i] = acum[i-1] + check[i];
    for(int i = 0; i < n; i++){
        int pos = arr[i]-1;
        if (pos == i)
            continue;
        if ((pos > i && acum[pos-1]-acum[i]+check[i] != arr[i]-i-1) || (pos < i && acum[pos]-acum[i-1]+check[pos] != i - arr[i]+1)){
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


//perreo a poca luz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <assert.h>
#include <limits>
using namespace std;

typedef pair<int,int> ii;
typedef pair<char, ii> cii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

void fastIO() {
    std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define EPS (1e-12)
#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define FORE(i,c) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)

#define fst first
#define snd second

template<typename T, typename U> inline void mnze(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void mxze(T &x, U y) { if(x < y) x = y; }

void _scan( int &x ) { scanf("%d",&x); }
void _scan( long long &x ) { scanf("%lld",&x); }
void _scan( double &x ) { scanf("%lf",&x); }
void _scan( long double &x ) { scanf("%Lf",&x); }
void _scan( char &x ) { scanf(" %c",&x); }
void _scan( char *x ) { scanf("%s",x); }
void scan() {}
template<typename T, typename... U>
void scan( T& head, U&... tail ) { _scan(head); scan(tail...);}

template<typename T> void _dbg(const char* sdbg, T h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename T, typename... U> void _dbg(const char* sdbg, T h, U... t) {
    while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(_i, (x)) cerr <<*_i <<", "; cerr <<"\n"; }}
#define debuga(x, sz) {{cerr <<#x <<" = "; FOR(_i, 0, sz) cerr << x[_i] <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr if(0)cout
#endif

#define N 200009

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

int main(){
    int n, arr[N], check[N], acum[N];
    scan(n);
    FOR(i,0,n)
        scan(arr[i]);
    FOR(i,0,n)
        scanf("%1d", &check[i]);
    memset(acum,0,sizeof(acum));
    FOR(i,1,n)
        acum[i] = acum[i-1] + check[i];
    for(int i = 0; i < n; i++){
        int pos = arr[i]-1;
        if (pos == i)
            continue;
        if ((pos > i && acum[pos-1]-acum[i]+check[i] != arr[i]-i-1) || (pos < i && acum[pos]-acum[i-1]+check[pos] != i - arr[pos]+1)){
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


//perreo a poca luz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <assert.h>
#include <limits>
using namespace std;

typedef pair<int,int> ii;
typedef pair<char, ii> cii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

void fastIO() {
    std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}

#define EPS (1e-12)
#define FOR(i,f,t) for(int i=f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=f; i>(int)t; i--)
#define FORE(i,c) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0])*sz)

#define fst first
#define snd second

template<typename T, typename U> inline void mnze(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void mxze(T &x, U y) { if(x < y) x = y; }

void _scan( int &x ) { scanf("%d",&x); }
void _scan( long long &x ) { scanf("%lld",&x); }
void _scan( double &x ) { scanf("%lf",&x); }
void _scan( long double &x ) { scanf("%Lf",&x); }
void _scan( char &x ) { scanf(" %c",&x); }
void _scan( char *x ) { scanf("%s",x); }
void scan() {}
template<typename T, typename... U>
void scan( T& head, U&... tail ) { _scan(head); scan(tail...);}

template<typename T> void _dbg(const char* sdbg, T h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename T, typename... U> void _dbg(const char* sdbg, T h, U... t) {
    while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(_i, (x)) cerr <<*_i <<", "; cerr <<"\n"; }}
#define debuga(x, sz) {{cerr <<#x <<" = "; FOR(_i, 0, sz) cerr << x[_i] <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr if(0)cout
#endif

#define N 200009

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

int main(){
    int n, arr[N], check[N], acum[N];
    scan(n);
    FOR(i,0,n)
        scan(arr[i]);
    FOR(i,0,n-1)
        scanf("%1d", &check[i]);
    memset(acum,0,sizeof(acum));
    acum[0] = check[0];
    FOR(i,1,n)
        acum[i] = acum[i-1] + check[i];
    for(int i = 0; i < n; i++){
        int pos = arr[i]-1;
        if (pos == i)
            continue;
        if ((pos > i && acum[pos-1]-acum[i]+check[i] != pos-i) || (pos < i && acum[i-1]-acum[pos]+check[pos] != i - pos)){
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){

			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i);
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[0] = 0;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;){
    	//debug(i);
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}else i++;
	}
	cout<<"YES"<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[0] = 0;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
    	//debug(i);
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			//i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			//debug(t[i],i,freq[i],freq[t[i]],c);
			if(t[i]<i) c=abs(freq[i]-freq[t[i]-1]);
			else c=abs(freq[i-1]-freq[t[i]]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i);
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[0] = 0;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

bool comp(pair<int,int> p1, pair<int,int> p2){
	if(p1.first==p2.first)
		return p1.second>p2.second;
	else return p1.first>p2.first;
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	string s;
	int n,c;
	int t[n+1];
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}
	cin>>s;
	int freq[n+1];
	freq[1] = (s[0]=='0'?0:1);
	for(int i=2;i<=n;i++){
		freq[i]=freq[i-1]+(s[i-1] == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			//debug(t[i],i,freq[i],freq[t[i]],c);
			if(t[i]<i) c=abs(freq[i]-freq[t[i]-1]);
			else c=abs(freq[i-1]-freq[t[i]]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			//debug(t[i],i,freq[i],freq[t[i]],c);
			if(t[i]<i) c=abs(freq[i]-freq[t[i]-1]);
			else c=abs(freq[i-1]-freq[t[i]]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

bool comp(pair<int,int> p1, pair<int,int> p2){
	if(p1.first==p2.first)
		return p1.second>p2.second;
	else return p1.first>p2.first;
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;
	int t[n+1];
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;i++){
		if(t[i]!=i){
			//debug(t[i],i,freq[i],freq[t[i]],c);
			if(t[i]<i) c=abs(freq[i]-freq[t[i]-1]);
			else c=abs(freq[i-1]-freq[t[i]]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i)+1;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

/*1 4 3 2 1
  1 1 1
*/


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[0] = 0;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;){
    	debug(i);
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i);
		}else i++;
	}
	cout<<"YES"<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long

#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << "(" << *it << " = " << a << ") ";
    err(++it, args...);
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,c;

	cin>>n;
	int t[n+1];
	for(int i=0;i<n;i++){
		cin>>t[i+1];
	}

	int freq[n+1];
	char s;
	cin>>s;
	freq[0] = 0;
	freq[1] = (s=='0'?0:1);
	for(int i=2;i<n;i++){
		cin>>s;
		freq[i]=freq[i-1]+(s == '0'?0:1);
	}
	for(int i=1;i<=n;){
    	//debug(i);
		if(t[i]!=i){
			c=abs(freq[i-1]-freq[t[i]-1]);
			if(c!=abs(t[i]-i)){ cout<<"NO"<<endl; return 0;}
			i=max(t[i],i);
		}else i++;
	}
	cout<<"YES"<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
//cout<<a[n-1]<<" ";
if(a[n-1]!=n)
{cout<<"NO";
    exit(0);}
string s;
cin>>s;ll sum[n]={0};ll m=-1;
for(ll i=0;i<n;i++)
{m=max(m,a[i]);
if(s[i]=='0')
    {
        if(m>i+1)
        {cout<<"NO";
    exit(0);}
    }
}

cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
//cout<<a[n-1]<<" ";

string s;
cin>>s;ll sum[n]={0};ll m=-1;
for(ll i=0;i<n;i++)
{m=max(m,a[i]);
if(s[i]=='0')
    {
        if(m>i+1)
        {cout<<"NO";
    exit(0);}
    }
}

cout<<"YES";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n;
string s;
const int MAXN = 200005;
int arr[MAXN], pos[MAXN];

int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> arr[i];
		pos[arr[i]] = i;
	}
	cin >> s;

	for(int i = 0; i < n; i++)
	{
		int k = i + 1;
		while(arr[i] > arr[k] && i < (n - 1))
		{
			if(s[i] != '1')
			{
				cout << "NO\n";
				return 0;
			}
			k++;
		}
		i = k;
	}

	cout << "YES\n";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n;
string s;
const int MAXN = 200005;
int arr[MAXN];

int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		arr[x] = i;
	}
	cin >> s;

	int big = 0;
	for(int i = 1; i <= n; i++)
	{
		int x = arr[i];
		if(x < big)
		{
			big = max(x, i);
			continue;
		}
		for(int j = big; j < x; j++)
			if(s[j] != '1')
			{
				cout << "NO\n";
				return 0;
			}
		big = max(x, i);
	}

	cout << "YES\n";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n;
string s;
const int MAXN = 200005;
int arr[MAXN], pos[MAXN];

int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> arr[i];
		pos[arr[i]] = i;
	}
	cin >> s;

	for(int i = 0; i < n; i++)
	{
		int k = i + 1;
		while(arr[i] > arr[k] && k < n)
		{
			if(s[i] != '1')
			{
				cout << "NO\n";
				return 0;
			}
			k++;
		}
		i = k;
	}

	cout << "YES\n";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n;
string s;
const int MAXN = 200005;
int arr[MAXN], pos[MAXN];

int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> arr[i];
		pos[arr[i]] = i;
	}
	cin >> s;

	for(int i = 0; i < n; i++)
	{
		int k = i + 1;
		while(arr[i] > arr[k] && k < n)
		{
			if(s[i] != '1')
			{
				cout << "NO\n";
				return 0;
			}
			k++;
		}
		i = k - 1;
	}

	cout << "YES\n";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n;
string s;
const int MAXN = 200005;
int arr[MAXN];

int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> arr[i];
	cin >> s;

	int curr = 0;
	for(int i = 0; i < n; i++)
	{
		curr = max(curr, arr[i]);
		if(s[i] == '0' && curr > i + 1)
		{
			cout << "NO\n";
			return 0;
		}
	}
	cout << "YES\n";
	return 0;
}


//in his name
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n];
    string b;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cin>>b;
    int sum[2000000];
    int f=0;
    for(int i=0;i<n;i++){
        f+=(b[i]-'0');
        sum[i]=f;
    }
    bool ans=1;
    for(int i=0;i<n;i++){
        if(sum[a[i]-2]-sum[i-1]!=a[i]-i-1){
            ans=0;
        }

    }
    if(ans){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}


//in his name
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n];
    string b;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cin>>b;
    int sum[10000];
    int f=0;
    for(int i=0;i<n;i++){
        f+=(b[i]-'0');
        sum[i]=f;
    }
    bool ans=1;
    for(int i=0;i<n;i++){
        if(sum[a[i]-2]-sum[i-1]!=a[i]-i-1){
            ans=0;
        }

    }
    if(ans){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}


//in his name
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
    int n;
    cin>>n;
    int a[n];
    string b;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cin>>b;
    int sum[2000000];
    int f=0;
    for(int i=0;i<n;i++){
        f+=(b[i]-'0');
        sum[i]=f;
    }
    bool ans=1;
    for(int i=0;i<n;i++){
        if(sum[a[i]-2]-sum[i-1]!=a[i]-i-1){
            ans=0;
        }

    }
    if(ans){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}


//in his name
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
    int n;
    cin>>n;
    int p=0;
    int a[n];
    string b;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cin>>b;
    for(int i=0;i<n;i++){
        p=max(p,a[i]);
        if(b[i]=='0'&&p>i+1){
            cout<<"NO";return 0;
        }
    }
    cout<<"YES";

}


//in his name
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n];
    string b;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cin>>b;
    int sum[20000];
    int f=0;
    for(int i=0;i<n;i++){
        f+=(b[i]-'0');
        sum[i]=f;
    }
    bool ans=1;
    for(int i=0;i<n;i++){
        if(sum[a[i]-2]-sum[i-1]!=a[i]-i-1){
            ans=0;
        }

    }
    if(ans){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<"Debug\t"<<#x<<"="<<x<<endl;
#define deb2(x,y) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<endl;
#define deb3(x,y,z) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<"\t"<<#z<<"="<<z<<endl;
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define in insert
#define Test ll t;cin>>t; while(t--)
int main()
{
ios;
ll i,j,n,x,y,z,te=0,x1,y1,z1,m,d=0,k,sum=0,p;
cin>>n;
string s;
ll a[n+5];
vector<pair<ll,char>> v;
for(i=0;i<n;i++)
cin>>a[i];
cin>>s;
for(i=0;i<n-1;i++)
{
    te=0;
    j=i;
while(s[i]=='1')
{
i++;
te=1;
}
if(te==1)
{
sort(a+j,a+i);
i--;
}
}
te=0;
for(i=0;i<n-1;i++)
{
k=(a[i+1]-a[i]);
v.pb(mp(k,s[i]));
}
for(i=0;i<n-1;i++)
{
if(v[i].fi>0 && v[i].se=='0')
{
    te=1;
    break;
}
}
if(te==0)
cout<<"YES";
else
cout<<"NO";
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<"Debug\t"<<#x<<"="<<x<<endl;
#define deb2(x,y) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<endl;
#define deb3(x,y,z) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<"\t"<<#z<<"="<<z<<endl;
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define in insert
#define Test ll t;cin>>t; while(t--)
int main()
{
ios;
ll i,j,n,x,y,z,te=0,x1,y1,z1,m,d=0,k,sum=0,p;
cin>>n;
string s;
ll a[n+5];
vector<pair<ll,char>> v;
for(i=0;i<n;i++)
cin>>a[i];
cin>>s;
for(i=0;i<n-1;i++)
{
    te=0;
    j=i;
while(s[i]=='1')
{
i++;
te=1;
}
if(te==1)
{
sort(a+j,a+i+1);
}
}
te=0;
for(i=0;i<n-1;i++)
{
k=(a[i+1]-a[i]);
v.pb(mp(k,s[i]));
}
for(i=0;i<n-1;i++)
{
if(v[i].fi<0 && v[i].se=='0')
{
    te=1;
    break;
}
}
if(te==0)
cout<<"YES";
else
cout<<"NO";
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<"Debug\t"<<#x<<"="<<x<<endl;
#define deb2(x,y) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<endl;
#define deb3(x,y,z) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<"\t"<<#z<<"="<<z<<endl;
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define in insert
#define Test ll t;cin>>t; while(t--)
int main()
{
ios;
ll i,j,n,x,y,z,te=0,x1,y1,z1,m,d=0,k,sum=0,p;
cin>>n;
string s;
ll a[n+5];
vector<pair<ll,char>> v;
for(i=0;i<n;i++)
cin>>a[i];
cin>>s;
for(i=0;i<n-1;i++)
{
k=(a[i+1]-a[i]);
v.pb(mp(k,s[i]));
deb(k);
}
for(i=0;i<n-1;i++)
{
if(v[i].fi<0 && v[i].se=='0')
{
te=1;
break;
}
}
if(te==0)
cout<<"YES";
else
cout<<"NO";
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<"Debug\t"<<#x<<"="<<x<<endl;
#define deb2(x,y) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<endl;
#define deb3(x,y,z) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<"\t"<<#z<<"="<<z<<endl;
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define in insert
#define Test ll t;cin>>t; while(t--)
int main()
{
ios;
ll i,j,n,x,y,z,te=0,x1,y1,z1,m,d=0,k,sum=0,p;
cin>>n;
string s;
ll a[n+5];
vector<pair<ll,char>> v;
for(i=0;i<n;i++)
cin>>a[i];
cin>>s;
for(i=0;i<n-1;i++)
{
k=(a[i+1]-a[i]);
v.pb(mp(k,s[i]));
}
for(i=0;i<n-1;i++)
{
if(v[i].fi<0 && v[i].se=='0')
{
te=1;
break;
}
}
if(te==0)
cout<<"YES";
else
cout<<"NO";
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<"Debug\t"<<#x<<"="<<x<<endl;
#define deb2(x,y) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<endl;
#define deb3(x,y,z) cout<<"Debug\t"<<#x<<"="<<x<<"\t"<<#y<<"="<<y<<"\t"<<#z<<"="<<z<<endl;
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define in insert
#define Test ll t;cin>>t; while(t--)
int main()
{
ios;
ll i,j,n,x,y,z,te=0,x1,y1,z1,m,d=0,k,sum=0,p;
cin>>n;
string s;
ll a[n+5],b[n+5];
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
}
cin>>s;
sort(b,b+n);
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if(s[i]=='1')
{
if(a[i]>a[i+1])
{
p=a[i];
a[i]=a[i+1];
a[i+1]=p;
}
}
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
continue;
else
{
te=1;
break;
}
}
if(te==0)
cout<<"YES";
else
cout<<"NO";
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a;
    cin>>a;
    ll b[a+1];
    for(int g=1;g<=a;g++)
    cin>>b[g];
    string s;
    cin>>s;
    for(int g=1;g<=a;g++)
    {
        int j=0;
     for(int i=g-1;i>=1;i--)
     {
      if(s[i-1]=='0')
      break;
      if(b[i]==g)
      {
          j=1;
          break;
      }
     }
     int k=0;
      for(int i=g+1;i<=a;i++)
     {
      if(s[i-1]=='0'&&k==1)
      break;
      if(s[i-1]=='0')
      k++;
      if(b[i]==g)
      {
          j=1;
          break;
      }
     }
         if(b[g]==g)
         j=1;
         if(j==0)
         {
          cout<<"NO";
          return 0;
         }

    }
        cout<<"YES";

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n;
cin>>n;
ll a[n];
for(i=0;i<n;i++)
cin>>a[i];

string s;
cin>>s;
ll m=0;
for (i=0;i<n;i++)
{
    m=max(m,a[i]);
    if (s[i]=='0'&&m>i+1)
    {
            cout<<"NO";
            return 0;

    }
}
cout<<"YES";

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a;
    cin>>a;
    ll b[a+1];
    for(int g=1;g<=a;g++)
    cin>>b[g];
    string s;
    cin>>s;
    for(int g=1;g<=a;g++)
    {
        int j=0;
     for(int i=g-1;i>=1;i--)
     {
      if(s[i-1]=='0')
      break;
      if(b[i]==g)
      {
          j=1;
          break;
      }
     }

      for(int i=g+1;i<=a;i++)
     {
      if(s[i-1]=='0')
      break;
      if(b[i]==g)
      {
          j=1;
          break;
      }
     }
         if(b[g]==g)
         j=1;
         if(j==0)
         {
          cout<<"NO";
          return 0;
         }

    }
        cout<<"YES";

}


// Example program
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a;
    cin>>a;
    ll b[a+1];
    for(int g=1;g<=a;g++)
    cin>>b[g];
    string s;
    cin>>s;
    for(int g=1;g<=a;g++)
    {
        int j=0;
     for(int i=g-1;i>=1;i--)
     {
      if(s[i-1]=='0')
      break;
      if(b[i]==g)
      {
          j=1;
          break;
      }
         if(b[g]==g)
         j=1;
         if(j==0)
         {
          cout<<"NO";
          return 0;
         }

     }

    }
    cout<<"YES";
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n;
cin>>n;
ll a[n];
for(i=0;i<n;i++)
cin>>a[i];

string s;
cin>>s;
for (i=0;i<n;i++)
{
    m=max(m,a[i]);
    if (s[i]=='0'&&m>i+1)
    {
            cout<<"NO";
            return 0;

    }
}
cout<<"YES";

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a;
    cin>>a;
    ll b[a+1];
    for(int g=1;g<=a;g++)
    cin>>b[g];
    string s;
    cin>>s;
    for(int g=1;g<=a;g++)
    {
        int j=0;
     for(int i=g-1;g>=1;g++)
     {
      if(s[i-1]=='0')
      break;
      if(b[i]==g)
      {
          j=1;
          break;
      }
         if(b[g]==g)
         j=1;
         if(j==0)
         {
          cout<<"NO";
          return 0;
         }

     }
        cout<<"YES";
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++)
cin>>a[i];

string s;
cin>>s;
ll m=0;
for (int i=0;i<n;i++)
{
    m=max(m,a[i]);
    if (s[i]=='0'&&m>i+1)
    {
            cout<<"NO";
            return 0;

    }
}
cout<<"YES";

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MAX 200008

int main()
{
	char s[MAX];
	ll n,a[MAX],i,flag=0;

	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	scanf("%s",s);
	s[n-1]='1';
	for(i=0;i<n-1;i++){
		if(s[i]=='0'&& (i+1)<a[i]){
			flag=1;
			break;
		}
		else if(s[i]=='1'&&(i+1)!=a[i] && a[i]>a[i+1]){
			swap(a[i],a[i+1]);
		}
	}
	if(!flag) printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MAX 200008

int main()
{
	char s[MAX];
	ll n,a[MAX],i,flag=0;

	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	scanf("%s",s);
	s[n-1]='1';
	for(i=0;i<n-1;i++){
		if(s[i]=='0'&& (i+1)<a[i]){
			flag=1;
			break;
		}
		else if(s[i]=='1'&&(i+1)!=a[i]){
			swap(a[i],a[i+1]);
		}
	}
	if(!flag) printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MAX 200008

int main()
{
	char s[MAX];
	ll n,a[MAX],i,flag=0;

	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	scanf("%s",s);
	s[n-1]='1';
	for(i=0;i<n-1;i++){
		if(s[i]=='0'&& (i+1)!=a[i]){
			flag=1;
			break;
		}
		else if(s[i]=='1'&&(i+1)<a[i]){
			swap(a[i],a[i+1]);
		}
	}
	if(!flag) printf("YES\n");
	else printf("NO\n");
	return 0;
}


#define lli long long int
#define sf  scanf
#define pf  printf
#define d   double
#define pi                  acos(-1)
#define READ                freopen("in.txt", "r", stdin)
#define WRITE               freopen("out.txt", "w", stdout)
#define INF                 10000000000000000000
#define dist(ax,ay,bx,by)   sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by))
#define gcd(a,b)            __gcd(a,b)
#define lcm(a,b)            (a*b)/__gcd(a,b)
#define m_p(a,b)            make_pair(a,b)
#define pb                  push_back
#define pii pair< lli, lli >
#define ppi pair< pii, lli >
#define ff first
#define ss second
#define ms(name,reference) memset(name,reference,sizeof(name))
#include<bits/stdc++.h>
using namespace std;
#define MAX 2000010
//bool vis[MAX];
vector<lli>v;
int main()
{
	lli n,a,i;

	string s;
	sf("%lld",&n);
	for(i=0;i<n;i++){
		sf("%lld",&a);
		v.pb(a);
	}
	cin>>s;
	a=n-1;
	lli j,temp;
	bool flag= false;
	for( i=0;i<a;i++){
	for(j=i+1;j<a;j++){
		if(v[i]>v[j])
		{
			if(s[i]=='1')
			{
				temp=v[j];
				v[j]=v[i];
				v[i]=temp;
			}
			else
			{
				flag=true;
			}
		}
	}
	}
	if(!flag)
		cout<<"YES\n";
	else cout<<"NO\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MAX 200008

int main()
{
	char s[MAX];
	ll n,a[MAX],i,flag=0;

	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	scanf("%s",s);
	s[n-1]='1';
	for(i=0;i<n-1;i++){
		if(s[i]=='0'&& (i+1)!=a[i]){
			flag=1;
			break;
		}
		else if(s[i]=='1'&&(i+1)!=a[i]){
			swap(a[i],a[i+1]);
		}
	}
	if(!flag) printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 35001;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			//cout<<i<<"  fff"<<endl;
		}
		else if(str[i] == '1') {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			if(!vis[i+1]) {
				add(i+1,1);
				vis[i+1] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 35001;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	str[n] = '0';
	str[n+1] = '\0';
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			//cout<<i<<"  fff"<<endl;
		}
		if(str[i] == '1') {
			if(!vis[a[i]]) {
				add(a[i],1);
				vis[a[i]] = 1;
			}
			if(!vis[a[i+1]]) {
				add(a[i+1],1);
				vis[a[i+1]] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 200010;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	str[n] = '0';
	str[n+1] = '\0';
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			//cout<<i<<"  fff"<<endl;
		}
		if(str[i] == '1') {
			if(!vis[a[i]]) {
				add(a[i],1);
				vis[a[i]] = 1;
			}
			if(!vis[a[i+1]]) {
				add(a[i+1],1);
				vis[a[i+1]] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 35001;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			cout<<i<<"  fff"<<endl;
		}
		else if(str[i] == '1') {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			if(!vis[i+1]) {
				add(i+1,1);
				vis[i+1] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 35001;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	str[n] = '0';
	str[n+1] = '\0';
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			//cout<<i<<"  fff"<<endl;
		}
		if(str[i] == '1') {
			if(!vis[i]) {
				add(a[i],1);
				vis[a[i]] = 1;
			}
			if(!vis[i+1]) {
				add(a[i+1],1);
				vis[a[i+1]] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fir first
#define se second
#define pb(a) push_back(a)
const int maxn = 3e5+5;
typedef pair<int,int> pii;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;

char str[maxn];
int a[maxn],sum[maxn],vis[maxn];

int lowbit(int t) {
    return t & (-t);
}

void add(int x,int v) {
    for(int i = x;i < 35001;i += lowbit(i))
        sum[i] += v;
}

int getsum(int t) {
    int s = 0;
    while(t) {
        s += sum[t];
        t -= lowbit(t);
    }
    return s;
}

int main() {
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	scanf("%s",str+1);
	str[n] = '0';
	str[n+1] = '\0';
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i = 1;i <= n;i++) {
		if(a[i] == i) {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			//cout<<i<<"  fff"<<endl;
		}
		if(str[i] == '1') {
			if(!vis[i]) {
				add(i,1);
				vis[i] = 1;
			}
			if(!vis[i+1]) {
				add(i+1,1);
				vis[i+1] = 1;
			}
		}
 		if(str[i] == '0' && getsum(i) != i) {
			//cout<<i<<"--"<<getsum(i)<<endl;
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
int acc[400000];
string s;

int main(){
    int n;
    cin>>n;
    nums.push_back(0);
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    //string s;
    cin>>s;
    int cnt=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='1')cnt++;
        acc[i+1]=cnt;
    }
    for(int i=0;i<n;i++){
        int x = i;
        int y = nums[i];
        if(abs(x-y)!=abs(acc[x-1]-acc[y-1])){
                //cout<<abs(x-y)<<" "<<abs(acc[x]-acc[y])<<endl;
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
string s;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    //string s;
    cin>>s;
    int mx =0;
    for(int i=0;i<n-1;i++){
        if(s[i]=='0'){
            mx=max(mx,nums[i]);
            int x = i+1;
            if(mx>x){
                cout<<"NO"<<endl;
                return 0;
            }

        }
    }
    mx =99999999;
    for(int i=n-2;i>=0;i--){
        if(s[i]=='0'){
            int x = i+1;
            if(mx<x){
                    //cout<<x<<" "<<mx<<" "<<i<<endl;
                cout<<"NO"<<endl;
                return 0;
            }
            mx=min(mx,nums[i]);

        }
    }
    cout<<"YES"<<endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
string s;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    //string s;
    cin>>s;
    int mx =0;
    for(int i=0;i<n-1;i++){
        if(s[i]=='0'){
            int x = i+1;
            if(mx>x){
                cout<<"NO"<<endl;
                return 0;
            }
            mx=max(mx,nums[i]);

        }
    }
    mx =99999999;
    for(int i=n-2;i>=0;i--){
        if(s[i]=='0'){
            int x = i+1;
            mx=min(mx,nums[i]);
            if(mx<x){
                    //cout<<x<<" "<<mx<<" "<<i<<endl;
                cout<<"NO"<<endl;
                return 0;
            }

        }
    }
    cout<<"YES"<<endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
string s;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    //string s;
    cin>>s;
    for(int i=0;i<n-1;i++){
        if(s[i]=='0'){
            int x = i+1;
            for(int j=0;j<=i;j++){
                if(nums[j]>x){
                    cout<<"NO"<<endl;
                    return 0;
                }
            }

        }
    }
    cout<<"YES"<<endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
string s;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    string s;
    cin>>s;
    for(int i=0;i<n;i++){
        int x = i;
        int y = nums[i]-1;
        for(int j=min(x,y);j<max(x,y)-1;j++){
            if(s[j]=='0'){
                cout<<"NO"<<endl;
                return 0;
            }
        }
    }
    cout<<"YES"<<endl;
}


#include<bits/stdc++.h>

using namespace std;

vector<int> nums;
string s;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        nums.push_back(x);
    }
    //string s;
    cin>>s;
    int mx =0;
    for(int i=0;i<n-1;i++){
        if(s[i]=='0'){
            mx=max(mx,nums[i]);
            int x = i+1;
            if(mx>x){
                cout<<"NO"<<endl;
                return 0;
            }

        }
    }
    cout<<"YES"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int a[200200];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) scanf("%d", a+i);
	string q; cin >> q;
	int r = 0;
	for(int i = 0; i < n-1; i ++) {
	 	  if(q[i] == '0') {
 		  	sort(a+r, a+i+1);
 		  	r = i+1;
		  }
	}
	for(int i = 0; i < n-1; i ++) if(a[i] > a[i+1]) { cout << "NO"; return 0; }
	cout << "YES";
}


#include <bits/stdc++.h>
using namespace std;
int a[200200];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) scanf("%d", a+i);
	string s; cin >> s; s+='0';
	int p = 0;
	for(int i = 0; i < n; i ++) {
	 	  if(s[i] == '0') {
 		  	sort(a+p, a+i+1);
 		  	p = i+1;
		  }
	}
	for(int i = 0; i < n-1; i ++) if(a[i] > a[i+1]) { cout << "NO"; return 0; }
	cout << "YES";
}


#include <bits/stdc++.h>
using namespace std;
int a[200200], b[200200];
int main() {
	int n;
	 cin >> n;
	 for(int i = 0; i < n; i ++) scanf("%d", a+i), b[i] = a[i];
	 sort(b, b+n);
	 string s; cin >> s;
	 for(int i = 0; i < n; i ++) {
	 	for(int j = 0; j < n-1; j ++) {
	 		if(s[j] == '1' && a[j] > a[j+1]) swap(a[j], a[j+1]);
		 }
	 }
	 for(int i = 0; i < n; i ++) {
	 	if(a[i]!=b[i]) {
	 		cout << "NO";
	 		return 0;
		 }
	 }
	 cout << "YES";
}


#include <bits/stdc++.h>
using namespace std;
int a[200200];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) scanf("%d", a+i);
	string s; cin >> s; s+='0'
	int p = 0;
	for(int i = 0; i < n; i ++) {
	 	  if(s[i] == '0') {
 		  	sort(a+p, a+i+1);
 		  	p = i+1;
		  }
	}
	for(int i = 0; i < n-1; i ++) if(a[i] > a[i+1]) { cout << "NO"; return 0; }
	cout << "YES";
}


#include <bits/stdc++.h>
using namespace std;
int a[200200];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) scanf("%d", a+i);
	string s; cin >> s;
	int p = 0;
	for(int i = 0; i < n-1; i ++) {
	 	  if(s[i] == '0') {
 		  	sort(a+p, a+i+1);
 		  	p = i+1;
		  }
	}
	for(int i = 0; i < n-1; i ++) if(a[i] > a[i+1]) { cout << "NO"; return 0; }
	cout << "YES";
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];
bool isv[maxsize];

string str;

bool solve(){
	int i,j,k;
	for(i=1;i<=n;i++){
		if(isv[i]){
			for(k=min(i,a[i]);k<max(i,a[i]);k++){
				if(str[k-1]=='1'){
					continue;
				}
				else
					return 0;
			}
		}
	}
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		memset(isv,0,sizeof(isv));
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i){
				isv[i]=1;
			}
		}
		cin>>str;
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];
int tmp[maxsize],cnt;

char str[maxsize];

bool solve(){
	int i,k,j;
	for(i=0;i<cnt;i++){
		for(k=min(tmp[i],a[tmp[i]]),j=max(tmp[i],a[tmp[i]])-1;k<=j;k++,j--){
			if(str[k-1]=='1'&&str[j-1]=='1'){
				continue;
			}
			else
				return 0;
		}
	}
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		cnt=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i){
				tmp[cnt++]=i;
			}
		}
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

# define deb(x) printf("# x=%d\n",x)

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

char str[maxsize];
int a[maxsize],n;

void handle(){
	int i,len=n-1;
	int l=1,r=0;
	for(i=1;i<=len;i++){
		if(str[i]=='1'){
			r=i+1;
		}
		else{
			if(l<r)
				sort(a+l,a+r+1);//l为起点,r为终点
			l=i+1;
			r=i;
		}
	}
	if(l<r)
		sort(a+l,a+r+1);//补上结尾
}

bool check(){
	int i;
	for(i=1;i<=n;i++)
		if(a[i]!=i)
			return 0;
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%s",str+1);
		handle();
		if(check()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
		//for(i=1;i<=n;i++){
		//	deb(a[i]);
		//}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

# define deb(x) printf("# x=%d\n",x)

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

char str[maxsize];
int a[maxsize],n;

void handle(){
	int i,len=n-1;
	for(i=1;i<=len;i++){
		if(str[i]=='1'){
			sort(a+i,a+i+1+1);
		}
	}
}

bool check(){
	int i;
	for(i=1;i<=n;i++)
		if(a[i]!=i)
			return 0;
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%s",str+1);
		handle();
		if(check()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

struct node{
	int x;
	int y;
};

node pos[maxsize];

int n,len;
int a[maxsize];
int tmp[maxsize],cnt;

char str[maxsize];

bool solve(){
	int i,k,j,x,y;
	len=0;
	if(cnt>0){
		x=min(tmp[0],a[tmp[0]]);
		y=max(tmp[0],a[tmp[0]])-1;
		for(k=x,j=y;k<=j;k++,j--){
			if(str[k-1]=='1'&&str[j-1]=='1'){
				continue;
			}
			else
				return 0;
		}
	}
	for(i=1;i<cnt;i++){
		if(min(tmp[i],a[tmp[i]])>=x&&max(tmp[i],a[tmp[i]])-1<=y)
			continue;
		for(k=min(tmp[i],a[tmp[i]]),j=max(tmp[i],a[tmp[i]])-1;k<=j;k++,j--){
			if(str[k-1]=='1'&&str[j-1]=='1'){
				continue;
			}
			else
				return 0;
		}
		x=min(x,min(tmp[i],a[tmp[i]]));
		y=max(y,max(tmp[i],a[tmp[i]])-1);
	}
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		cnt=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i){
				tmp[cnt++]=i;
			}
		}
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];

char str[maxsize];

bool solve(){
	int i,j,k;
	for(i=1;i<=n;i++){
		if(a[i]!=i){
			for(k=min(i,a[i]),j=max(i,a[i])-1;k<=j;k++,j--){
				if(str[k-1]=='1'&&str[j-1]=='1'){
					continue;
				}
				else
					return 0;
			}
		}
	}
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];
int start,end;

char str[maxsize];

bool solve(){
	int i;
	for(i=start;i<end;i++){
		if(str[i-1]=='1'){
			continue;
		}
		else
			return 0;
	}
	return 1;
}

int main(void){
	int i;
	bool flag;
	while(scanf("%d",&n)!=EOF){
		flag=1;
		start=-1;
		end=-1;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i&&flag==1){
				start=i;//记录最开始需要交换的
				flag=0;
			}
			if(a[i]!=i&&i>end){
				end=i;
			}
		}
		//printf("start=%d  end=%d\n",start,end);
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

# define deb(x) printf("# x=%d\n",x)

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

char str[maxsize];
int a[maxsize],n;

void handle(){
	int i,len=n-1;
	int l=1;
	for(i=1;i<=len;i++){
		if(str[i]=='1'&&i>=l){
			sort(a+i,a+i+1+1);
			l=i+1;
		}
	}
}

bool check(){
	int i;
	for(i=1;i<=n;i++)
		if(a[i]!=i)
			return 0;
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%s",str+1);
		handle();
		if(check()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];
bool isv[maxsize];

char str[maxsize];

bool solve(){
	int i,j,k;
	for(i=1;i<=n;i++){
		if(isv[i]){
			for(k=min(i,a[i]);k<max(i,a[i]);k++){
				if(str[k-1]=='1'){
					continue;
				}
				else
					return 0;
			}
		}
	}
	return 1;
}

int main(void){
	int i;
	while(scanf("%d",&n)!=EOF){
		memset(isv,0,sizeof(isv));
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i){
				isv[i]=1;
			}
		}
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<vector>
# include<cstdio>
# include<cstdlib>

typedef long long ll;

using namespace std;

const int maxsize=2e5+10;

int n;
int a[maxsize];
int Start,End;

char str[maxsize];

bool solve(){
	int i;
	for(i=Start;i<End;i++){
		if(str[i-1]=='1'){
			continue;
		}
		else
			return 0;
	}
	return 1;
}

int main(void){
	int i;
	bool flag;
	while(scanf("%d",&n)!=EOF){
		flag=1;
		Start=-1;
		End=-1;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=i&&flag==1){
				Start=i;//记录最开始需要交换的
				flag=0;
			}
			if(a[i]!=i&&i>End){
				End=i;
			}
		}
		//printf("start=%d  end=%d\n",start,end);
		getchar();
		gets(str);
		//printf("str=%s",str);
		if(solve()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}


#include <iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
typedef long long ll;
using namespace std;
ll num[200005];
ll Right[200005]={0},rightnum=1;
ll Left[200005]={0},leftnum=1;
int main()
{
ll n,m;
cin>>n>>m;
ll I=-1;
for(int i=0;i<n;i++)
  {
        scanf("%lld",&num[i]);
        if(num[i]==m)
        {
            I=i;
            continue;
        }
        if(I!=-1)
        {
            Right[rightnum]= Right[rightnum-1]+(num[i]>m?1:-1);
            ++rightnum;
        }
  }
  for(int i=I-1;i>=0;i--)
  {
       Left[leftnum]=Left[leftnum-1]+(num[i]>m?1:-1);
            ++leftnum;
  }
  leftnum--;
  rightnum--;
 ll chack=0,ans=0;
 Left[leftnum+1]=999999;
 Right[rightnum+1]=999999;
 sort(&Left[0],&Left[leftnum+1]);
  sort(&Right[0],&Right[rightnum+1]);
  for(int i=0;i<=leftnum;i++)
  {
      ll *p=lower_bound(&Right[0],&Right[rightnum+1],0-Left[i]);
      ll *pp=lower_bound(&Right[0],&Right[rightnum+1],2-Left[i]);
      ans+=pp-p;
  }
  cout<<ans<<endl;

}


#include <iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
typedef long long ll;
using namespace std;
ll num[200005];
ll Right[200005]={0},rightnum=1;
ll Left[200005]={0},leftnum=1;
int main()
{
ll n,m;
cin>>n>>m;
ll I=-1;
for(int i=0;i<n;i++)
  {
        scanf("%lld",&num[i]);
        if(num[i]==m)
        {
            I=i;
            continue;
        }
        if(I==-1)
        {
            Left[leftnum]=Left[leftnum-1]+(num[i]>m?1:-1);
            ++leftnum;
        }
        else if(I!=-1)
        {
            Right[rightnum]= Right[rightnum-1]+(num[i]>m?1:-1);
            ++rightnum;
        }
  }
  leftnum--;
  rightnum--;
 ll chack=0,ans=0;
 Left[leftnum+1]=999999;
 Right[rightnum+1]=999999;
 sort(&Left[0],&Left[leftnum+1]);
  sort(&Right[0],&Right[rightnum+1]);
  for(int i=0;i<=leftnum;i++)
  {
      ll *p=lower_bound(&Right[0],&Right[rightnum+1],0-Left[i]);
      ll *pp=lower_bound(&Right[0],&Right[rightnum+1],2-Left[i]);
      ans+=pp-p;
  }
  cout<<ans<<endl;

}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
int a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	int ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			if(sum[j]%2==1) ans+=num[sum[j]+MAXN];
			else	ans+=num[sum[j]+MAXN]+num[sum[j]-1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
long long a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	long long ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			if(sum[j]%2==1)
				ans+=num[sum[j]+MAXN]+num[sum[j]-1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
long long a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	long long ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			ans+=num[sum[j]+MAXN]+num[sum[j]-1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 200000
using namespace std;
long long a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	long long ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			//if(sum[j]%2==1)
				ans+=num[sum[j]+MAXN]+num[sum[j]+1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
int a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; ++i)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	int ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else ans+=num[sum[j]+MAXN]+num[sum[j]-1+MAXN];
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
long long a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	long long ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			//if(sum[j]%2==1)
				ans+=num[sum[j]+MAXN]+num[sum[j]+1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
long long a[MAXN+1], sum[MAXN+1], num[MAXN<<1|1];
int main()
{
    int n,m,pos;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
    	scanf("%d",&a[i]);
    	//if(a[i]<m) b[i]=-1;
    	//if(a[i]>m) b[i]=1;
    	if(a[i]==m) pos=i;
    	//sum[i]=sum[i-1]+b[i];
	}
	memset(num,0,sizeof(num));
	num[MAXN]++;
	sum[0]=0;
	long long ans=0;
    for(int j=1;j<=n;j++)
    {
        if(a[j]<a[pos]) sum[j]=sum[j-1]-1;
        else if(a[j]==a[pos]) sum[j]=sum[j-1];
        else sum[j] = sum[j-1]+1;
        if(j < pos) num[sum[j]+MAXN]++;
        else
		{
			if(sum[j]%2==1)
				ans+=num[sum[j]+MAXN]+num[sum[j]+1+MAXN];
		}
    }
    cout<<ans<<endl;
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef int i;
typedef long int l;
typedef long long int ll;
typedef float f;
typedef long double lf;
typedef char c;
const int inf = INT_MAX, mod = 1e7 + 9;
const int MAX = 1e7;
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
int main(void) {
    FASTER;
    int n, m;
    cin >> n >> m;
    int arr[n];
    int greater = 0, less = 0;
    bool check = false;
    map <int, int> diff;
    diff[0] = 1;
    long long ans = 0;
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
        if(arr[i] > m)
            greater++;
        else if(arr[i] < m)
            less++;
        else {
            less++;
            check = true;
            //diff[greater - less]++;
        }
        //cout << greater - less << endl;
        if(!check) {
            diff[greater - less]++;
        }
        else{
            ans += diff[greater - less] + diff[greater - less + 1];
        }

    }
    cout << ans<< endl;
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef int i;
typedef long int l;
typedef long long int ll;
typedef float f;
typedef long double lf;
typedef char c;
const int inf = INT_MAX, mod = 1e7 + 9;
const int MAX = 1e7;
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
int main(void) {
    FASTER;
    int n, m;
    cin >> n >> m;
    int arr[n];
    int greater = 0, less = 0;
    bool check = false;
    map <int, int> diff;
    diff[0] = 1;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
        if(arr[i] > m)
            greater++;
        else if(arr[i] < m)
            less++;
        else {
            less++;
            check = true;
            //diff[greater - less]++;
        }
        //cout << greater - less << endl;
        if(!check) {
            diff[greater - less]++;
        }
        else{
            ans += diff[greater - less] + diff[greater - less + 1];
        }

    }
    cout << ans<< endl;
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef int i;
typedef long int l;
typedef long long int ll;
typedef float f;
typedef long double lf;
typedef char c;
const int inf = INT_MAX, mod = 1e7 + 9;
const int MAX = 1e7;
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
int main(void) {
    FASTER;
    int n, m;
    cin >> n >> m;
    int arr[n];
    int greater = 0, less = 0;
    bool check = false;
    map <int, int> diff;
    diff[0] = 1;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
        if(arr[i] > m)
            greater++;
        else if(arr[i] < m)
            less++;
        else {
            less++;
            check = true;
            //diff[greater - less]++;
        }
        cout << greater - less << endl;
        if(!check) {
            diff[greater - less]++;
        }
        else{
            ans += diff[greater - less] + diff[greater - less + 1];
        }

    }
    cout << ans<< endl;
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rpd(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
using namespace std;
const int N=2e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,l,r,k;int a[N];__int64 ans;
int big1[N],sma1[N],big2[N],sma2[N];
int main(){
	n=read();m=read();rep(i,1,n){a[i]=read();if(a[i]==m)k=i;}
	rpd(i,k-1,1){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big1[l-r]+=1;else sma1[r-l]+=1;
	}
	l=0;r=0;
	rep(i,k+1,n){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big2[l-r]+=1;else sma2[r-l]+=1;
	}
	ans+=1LL+1LL*big1[1]+1LL*big2[1]+1LL*big1[0]+1LL*big2[0];
	ans+=1LL*big1[0]*big2[1]+1LL*big1[1]*big2[0]+1LL*big1[0]*big2[0];
	ans+=1LL*big1[1]*sma2[1];ans+=1LL*big2[1]*sma1[1];
	rep(i,2,n)ans+=1LL*big1[i]*sma2[i]+1LL*big1[i]*sma2[i-1];
	rep(i,2,n)ans+=1LL*big2[i]*sma1[i]+1LL*big2[i]*sma1[i-1];
	printf("%I64d",ans);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rpd(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
using namespace std;
const int N=2e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,l,r,k;int a[N];ll ans;
int big1[N],sma1[N],big2[N],sma2[N];
int main(){
	n=read();m=read();rep(i,1,n){a[i]=read();if(a[i]==m)k=i;}
	rpd(i,k-1,1){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big1[l-r]+=1;else sma1[r-l]+=1;
	}
	l=0;r=0;
	rep(i,k+1,n){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big2[l-r]+=1;else sma2[r-l]+=1;
	}
	ans=1LL*(1+big1[1]+big2[1]+big1[0]+big2[0]);
	ans+=1LL*(big1[0]+big1[1])*(big2[1]+big2[0]);
	rep(i,1,n)ans+=1LL*(big1[i]*sma2[i]+big1[i]*sma2[i-1]);
	rep(i,1,n)ans+=1LL*(big2[i]*sma1[i]+big2[i]*sma1[i-1]);
	printf("%I64d",ans);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rpd(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
using namespace std;
const int N=2e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,l,r,k;int a[N];ll ans;
int big1[N],sma1[N],big2[N],sma2[N];
int main(){
	n=read();m=read();rep(i,1,n){a[i]=read();if(a[i]==m)k=i;}
	rpd(i,k-1,1){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big1[l-r]+=1;else sma1[r-l]+=1;
	}
	l=0;r=0;
	rep(i,k+1,n){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big2[l-r]+=1;else sma2[r-l]+=1;
	}
	ans=1LL*(1+big1[1]+big2[1]+big1[0]+big2[0]);
	ans+=1LL*(big1[0]*big2[1]+big1[1]*big2[0]+big1[0]*big2[0]);
	ans+=1LL*(big1[1]*sma2[1]);ans+=1LL*(big2[1]*sma1[1]);
	rep(i,2,n)ans+=1LL*(big1[i]*sma2[i]+big1[i]*sma2[i-1]);
	rep(i,2,n)ans+=1LL*(big2[i]*sma1[i]+big2[i]*sma1[i-1]);
	printf("%I64d",ans);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rpd(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
using namespace std;
const int N=2e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,l,r,k;int a[N];__int64 ans;
int big1[N],sma1[N],big2[N],sma2[N];
int main(){
	n=read();m=read();rep(i,1,n){a[i]=read();if(a[i]==m)k=i;}
	rpd(i,k-1,1){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big1[l-r]+=1;else sma1[r-l]+=1;
	}
	l=0;r=0;
	rep(i,k+1,n){
		if(a[i]>m)l+=1;else r+=1;
		if(l>=r)big2[l-r]+=1;else sma2[r-l]+=1;
	}
	ans=1LL*(1+big1[1]+big2[1]+big1[0]+big2[0]);
	ans+=1LL*(big1[0]*big2[1]+big1[1]*big2[0]+big1[0]*big2[0]);
	ans+=1LL*(big1[1]*sma2[1]);ans+=1LL*(big2[1]*sma1[1]);
	rep(i,2,n)ans+=1LL*(big1[i]*sma2[i]+big1[i]*sma2[i-1]);
	rep(i,2,n)ans+=1LL*(big2[i]*sma1[i]+big2[i]*sma1[i-1]);
	printf("%I64d",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct TDif
{
    long val;
    bool L;
};

struct TData
{
    long L, R;
};

long Arr[201000];
TDif Dif[201000];
TData DifAm[500000]= {0};

bool compare (TDif a, TDif b)
{
    return((a.val<b.val)||((a.val==b.val)&&(a.L>b.L)));
}

int main()
{
    long N, M;
    cin >> N >> M;
    long Pos;
    for (long i= 0; i<N; i++) {
        cin >> Arr[i];
        if (Arr[i]==M) Pos= i;
    }
    Dif[0].val= 0;
    Dif[0].L= 1;
    for (long i= 0; i<Pos; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i+1].L= 1;
    }
    Dif[Pos+1].val= Dif[Pos].val;
    Dif[Pos+1].L= 0;
    for (long i= Pos+1; i<N; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i].L= 0;
    }
    sort(Dif, Dif+N+1, compare);
    //for (long i= 0; i<=N; i++) cout << Dif[i].val << ' ' << Dif[i].L << endl;

    Dif[N+1].val= 210000;
    long cur= 0;
    for (long i= -201000; i<=201000; i++) {
        while ((Dif[cur].val==i)&&(Dif[cur].L==1)) {
            DifAm[i+201000].L++;
            cur++;
        }
        while (Dif[cur].val==i) {
            DifAm[i+201000].R++;
            cur++;
        }
    }

    long long Answ= 0;
    for (long i= 0; i<=420000; i++) {
        Answ+= DifAm[i].L*DifAm[i].R;
        Answ+= DifAm[i].L*DifAm[i+1].R;
    }
    cout << Answ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct TDif
{
    long val;
    bool L;
};

struct TData
{
    long L, R;
};

long Arr[201000];
TDif Dif[201000];
TData DifAm[410000]= {0};

bool compare (TDif a, TDif b)
{
    return((a.val<b.val)||((a.val==b.val)&&(a.L>=b.L)));
}

int main()
{
    long N, M;
    cin >> N >> M;
    long Pos;
    for (long i= 0; i<N; i++) {
        cin >> Arr[i];
        if (Arr[i]==M) Pos= i;
    }
    Dif[0].val= 0;
    Dif[0].L= 1;
    for (long i= 0; i<Pos; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i+1].L= 1;
    }
    Dif[Pos+1].val= Dif[Pos].val;
    Dif[Pos+1].L= 0;
    for (long i= Pos+1; i<N; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i].L= 0;
    }
    sort(Dif, Dif+N+1, compare);
    //for (long i= 0; i<=N; i++) cout << Dif[i].val << ' ' << Dif[i].L << endl;

    Dif[N+1].val= 210000;
    long cur= 0;
    for (long i= -200000; i<=200000; i++) {
        while ((Dif[cur].val==i)&&(Dif[cur].L==1)) {
            DifAm[i+200000].L++;
            cur++;
        }
        while (Dif[cur].val==i) {
            DifAm[i+200000].R++;
            cur++;
        }
    }

    long long Answ= 0;
    for (long i= 0; i<=400000; i++) {
        Answ+= DifAm[i].L*DifAm[i].R;
        Answ+= DifAm[i].L*DifAm[i+1].R;
    }
    cout << Answ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct TDif
{
    long long val;
    bool L;
};

struct TData
{
    long long L, R;
};

long long Arr[201000];
TDif Dif[201000];
TData DifAm[500000]= {0};

bool compare (TDif a, TDif b)
{
    return((a.val<b.val)||((a.val==b.val)&&(a.L>b.L)));
}

int main()
{
    long N, M;
    cin >> N >> M;
    long Pos;
    for (long i= 0; i<N; i++) {
        cin >> Arr[i];
        if (Arr[i]==M) Pos= i;
    }
    Dif[0].val= 0;
    Dif[0].L= 1;
    for (long i= 0; i<Pos; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i+1].L= 1;
    }
    Dif[Pos+1].val= Dif[Pos].val;
    Dif[Pos+1].L= 0;
    for (long i= Pos+1; i<N; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i].L= 0;
    }
    sort(Dif, Dif+N+1, compare);
    //for (long i= 0; i<=N; i++) cout << Dif[i].val << ' ' << Dif[i].L << endl;

    Dif[N+1].val= 210000;
    long cur= 0;
    for (long i= -201000; i<=201000; i++) {
        while ((Dif[cur].val==i)&&(Dif[cur].L==1)) {
            DifAm[i+201000].L++;
            cur++;
        }
        while (Dif[cur].val==i) {
            DifAm[i+201000].R++;
            cur++;
        }
    }

    long long Answ= 0;
    for (long i= 0; i<=420000; i++) {
        Answ+= DifAm[i].L*DifAm[i].R;
        Answ+= DifAm[i].L*DifAm[i+1].R;
    }
    cout << Answ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct TDif
{
    long val;
    bool L;
};

struct TData
{
    long L, R;
};

long Arr[201000];
TDif Dif[201000];
TData DifAm[500000]= {0};

bool compare (TDif a, TDif b)
{
    return((a.val<b.val)||((a.val==b.val)&&(a.L>=b.L)));
}

int main()
{
    long N, M;
    cin >> N >> M;
    long Pos;
    for (long i= 0; i<N; i++) {
        cin >> Arr[i];
        if (Arr[i]==M) Pos= i;
    }
    Dif[0].val= 0;
    Dif[0].L= 1;
    for (long i= 0; i<Pos; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i+1].L= 1;
    }
    Dif[Pos+1].val= Dif[Pos].val;
    Dif[Pos+1].L= 0;
    for (long i= Pos+1; i<N; i++) {
        if (Arr[i]<M) Dif[i+1].val= Dif[i].val-1;
        else Dif[i+1].val= Dif[i].val+1;
        Dif[i].L= 0;
    }
    sort(Dif, Dif+N+1, compare);
    //for (long i= 0; i<=N; i++) cout << Dif[i].val << ' ' << Dif[i].L << endl;

    Dif[N+1].val= 210000;
    long cur= 0;
    for (long i= -201000; i<=201000; i++) {
        while ((Dif[cur].val==i)&&(Dif[cur].L==1)) {
            DifAm[i+201000].L++;
            cur++;
        }
        while (Dif[cur].val==i) {
            DifAm[i+201000].R++;
            cur++;
        }
    }

    long long Answ= 0;
    for (long i= 0; i<=420000; i++) {
        Answ+= DifAm[i].L*DifAm[i].R;
        Answ+= DifAm[i].L*DifAm[i+1].R;
    }
    cout << Answ;
    return 0;
}


#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 200005;
int main(){
//    freopen("case.txt", "r", stdin);
    int n, m, sum, a, c[2*maxn];
    long long res = 0;
    memset(c, 0, sizeof(c));
    scanf("%d%d", &n,&m);
    sum = n;
    c[n] = 1;
    bool flag = false;
    for(int i=0; i<n; i++){
        scanf("%d", &a);
        if(a < m){
            sum--;
        }
        else if(a > m){
            sum++;
        }
        if(a == m) flag = true;
        if(!flag) c[sum]++;
        else res += c[sum] + c[sum-1];
    }
    printf("%64d\n", res);
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
int main(){
//    freopen("case.txt", "r", stdin);
    int n,m;
    scanf("%d%d", &n, &m);
    map<int, LL> mp;
    int sum=0;
    mp[0]=1;
    bool f=false;
    LL ans=0;
    for( int i=0; i<n; i++){
        int x;
        scanf("%d", &x);
        if(x>m) sum++;
        else if(x<m) sum--;
        if(x==m) f=true;
        if(f) ans+=mp[sum]+mp[sum-1];
        else mp[sum]++;
    }
    printf("%I64d", ans);
}


#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 200005;
int main(){
//    freopen("case.txt", "r", stdin);
    int n, m, sum, a, c[2*maxn];
    long long res = 0;
    memset(c, 0, sizeof(c));
    scanf("%d%d", &n,&m);
    sum = n;
    c[n] = 1;
    bool flag = false;
    for(int i=0; i<n; i++){
        scanf("%d", &a);
        if(a < m){
            sum--;
        }
        else if(a > m){
            sum++;
        }
        if(a == m) flag = true;
        if(!flag) c[sum]++;
        else res += c[sum] + c[sum-1];
    }
    printf("%I64d\n", res);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
typedef long long LL;
using namespace std;

map<int, LL> mp;
int n,m,pos;
int main(){
//    freopen("case.txt", "r", stdin);
    scanf("%d%d", &n,&m);
    int *a = new int[n];
    for( int i=0; i<n; i++){
        scanf("%d", &a[i]);
        if(a[i]==m) pos=i;
    }
    int cnt=0;
    mp[cnt]++;
    for( int i=pos+1; i<n; i++){
        if(a[i]>m) cnt++;
        else cnt--;
        mp[cnt]++;
    }
    LL ans=mp[0]+mp[1];
    cnt=0;
    for( int i=pos-1; i>=0; i--){
        if(a[i]<m) cnt++;
        else cnt--;
        ans += mp[cnt]+mp[cnt+1];
    }
    cout << ans << endl;
    return 0;

}


#include <cstdio>
#include <cstring>
typedef long long LL;
using namespace std;
int main(){
//    freopen("case.txt", "r", stdin);
    int n, m;
    scanf("%d%d", &n,&m);
    LL *a = new LL [2*n], ans=0;
    for( int i=0; i<2*n; i++){
        a[i]=0;
    }
    int sum=0, flag=0;
    a[n]=1;
    for( int i=0; i<n; i++){
        int x;
        scanf("%d", &x);
        if(x>m) sum++;
        else if(x<m) sum--;
        if(x==m) flag=1;
        if(flag) ans+=a[sum+n]+a[sum-1+n];
        else a[sum+n]++;

    }
    printf("%I64d ",ans);
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FOROP(i,a,b,op) for(int i=a;i<b;op)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int s = 0, p;
    umap<int, ll> ls[2], gr[2];
    bool seen = false;
    FOR(i,0,n){
        cin >> p;
        if (p >= k) s--;
        else s++;
        if (!seen){
            if(ls[i & 1].count(s) == 0) ls[i & 1][s] = 1;
            else ls[i & 1][s]++;
        }
        if (p == k){
            s += 2;
            seen = true;
        }
        if (seen){
            if(gr[i & 1].count(s) == 0) gr[i & 1][s] = 1;
            else gr[i & 1][s]++;
        }
    }
    ll ans = 0;
    FOR(x,0,2){
        for(auto pr : ls[x]){
            if (gr[x].count(pr.FI) > 0)
            ans += pr.SE * gr[x][pr.FI];
            if (gr[1 - x].count(pr.FI + 1) > 0)
            ans += gr[1 - x][pr.FI + 1] * pr.SE;
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FOROP(i,a,b,op) for(int i=a;i<b;op)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    if (n == 1){
        cout << 1 << endl;
        return 0;
    }
    int s = 0, p;
    umap<int, ll> ls[2], gr[2];
    bool seen = false;
    FOR(i,0,n){
        cin >> p;
        if (p >= k) s--;
        else s++;
        if (!seen){
            if(ls[i & 1].count(s) == 0) ls[i & 1][s] = 1;
            else ls[i & 1][s]++;
        }
        if (p == k){
            s += 2;
            seen = true;
        }
        if (seen){
            if(gr[i & 1].count(s) == 0) gr[i & 1][s] = 1;
            else gr[i & 1][s]++;
        }
    }
    ll ans = 0;
    FOR(x,0,2){
        for(auto pr : ls[x]){
            if (gr[x].count(pr.FI) > 0)
            ans += pr.SE * gr[x][pr.FI];
            if (gr[1 - x].count(pr.FI + 1) > 0)
            ans += gr[1 - x][pr.FI + 1] * pr.SE;
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FOROP(i,a,b,op) for(int i=a;i<b;op)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;

int n;
vi V(200002);

ll getCount(int m){
    //cout << "GETCNT FOR m = " << m << endl;
    umap<int, ll> ls, ps;
    int s = 0;
    ll sans = 0, ans = 0;
    ls[0] = 1;
    ls[-1] = 0;
    FOR(i,0,n){
        if (V[i] >= m){
            sans += ls[s];
            //cout << "ADD FOR s=" << s << " value " << ls[s] << ", make sans = " << sans << endl;
            s++;
        }
        else {
            s--;
            sans -= ls[s];
            //cout << "SUB FOR s=" << s << " value " << ls[s] << ", make sans = " << sans << endl;
            //sans++;
        }
        if(ls.count(s) == 0) ls[s] = 1;
        else ls[s]++;
        ans += sans;
    }
    //cout << "END GETCNT FOR m = " << m  << ", ans = " << ans << endl;
    return ans;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int k;
    cin >> n >> k;
    FOR(i,0,n) cin >> V[i];
    cout << getCount(k) - getCount(k + 1) << endl;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FOROP(i,a,b,op) for(int i=a;i<b;op)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;

int n;
vi V(200002);

int getCount(int m){
    //cout << "GETCNT FOR m = " << m << endl;
    umap<int, ll> ls, ps;
    int s = 0;
    ll sans = 0, ans = 0;
    ls[0] = 1;
    ls[-1] = 0;
    FOR(i,0,n){
        if (V[i] >= m){
            sans += ls[s];
            //cout << "ADD FOR s=" << s << " value " << ls[s] << ", make sans = " << sans << endl;
            s++;
        }
        else {
            s--;
            sans -= ls[s];
            //cout << "SUB FOR s=" << s << " value " << ls[s] << ", make sans = " << sans << endl;
            //sans++;
        }
        if(ls.count(s) == 0) ls[s] = 1;
        else ls[s]++;
        ans += sans;
    }
    //cout << "END GETCNT FOR m = " << m  << ", ans = " << ans << endl;
    return ans;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int k;
    cin >> n >> k;
    FOR(i,0,n) cin >> V[i];
    cout << getCount(k) - getCount(k + 1) << endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(int i = int(a); i <= int(b); i++)
#define FORD(i, a, b) for(int i = int(a); i >= int(b); i--)
#define REP(i, r) for(int i = 0; i < r; i++)
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define READFILE freopen("input.txt", "r", stdin)
#define WRITEFILE freopen("output.txt", "w", stdout);
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;

const int MAX=2e5+66;

int n,x,a[MAX], sum, Ans;
map<int,int> cnt;
bool Found;

void Solve()
{
	cnt[0]=1;
	FOR(i,1,n){
		if (a[i]<x) sum--;
		else sum++;
		if (a[i]==x) Found=true;
		if (Found) Ans+=cnt[sum]+cnt[sum-1];
		else cnt[sum]++;
	}
	cout << Ans;
}

int main()
{
    #ifndef ONLINE_JUDGE
        READFILE;
    #endif
    FAST;

 	cin >> n >> x;
 	FOR(i,1,n) cin >> a[i];
 	Solve();
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(int i = int(a); i <= int(b); i++)
#define FORD(i, a, b) for(int i = int(a); i >= int(b); i--)
#define REP(i, r) for(int i = 0; i < r; i++)
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define READFILE freopen("input.txt", "r", stdin)
#define WRITEFILE freopen("output.txt", "w", stdout);
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;

const int MAX=2e5+66;

int n,x,a[MAX], sum; ll Ans;
map<int,int> cnt;
bool Found;

void Solve()
{
	cnt[0]=1;
	FOR(i,1,n){
		if (a[i]<x) sum--;
		if (a[i]>x) sum++;
		if (a[i]==x) Found=true;
		if (Found) Ans+=cnt[sum]+cnt[sum-1];
		else cnt[sum]++;
	}
	cout << Ans;
}

int main()
{
    #ifndef ONLINE_JUDGE
        READFILE;
    #endif
    FAST;

 	cin >> n >> x;
 	FOR(i,1,n) cin >> a[i];
 	Solve();
    return 0;
}