#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int a[MAX],b[MAX];
int nex[MAX][21];
int A[MAX][21];
int B[MAX][21];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=n-1;i>=1;i--)
    {
        nex[i][0]=i+1;
        for(int j=1;j<=20;j++)nex[i][j]=nex[nex[i][j-1]][j-1];
    }
    A[n][0]=a[n];
    B[n][0]=b[n];
    for(int i=n-1;i>=1;i--)
    {
        A[i][0]=max(a[i],a[i+1]);
        B[i][0]=min(b[i],b[i+1]);
        for(int j=1;j<=20;j++)A[i][j]=max(A[i][j-1],A[nex[i][j-1]][j-1]);
        for(int j=1;j<=20;j++)B[i][j]=min(B[i][j-1],B[nex[i][j-1]][j-1]);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int now=i;
        int L=n+1,R=0;
        int ma=a[i],mi=b[i];
        //printf("i=%d\n",i);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);

            if(max(A[now][j],ma)==min(mi,B[now][j]))
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(max(A[now][j],ma)<min(mi,B[now][j]))
            {
                ma=max(ma,A[now][j]);
                mi=min(mi,B[now][j]);
                now=nex[now][j];
            }
        }
        if(a[now]==b[now])L=min(L,now),R=max(R,now);
        now=i;
        ma=a[i],mi=b[i];
        //printf("L=%d\n",L);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);

            if(max(A[now][j],ma)==min(mi,B[now][j]))
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(max(A[now][j],ma)<=min(mi,B[now][j]))
            {
                ma=max(ma,A[now][j]);
                mi=min(mi,B[now][j]);
                now=nex[now][j];
            }
        }
        //printf("R=%d\n",R);
        if(a[now]==b[now])L=min(L,now),R=max(R,now);
        ans+=max(0,(R-L+1));
        //if(R-L+1>0)printf("i=%d L=%d R=%d\n",i,L,R);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int a[MAX],b[MAX];
int nex[MAX][21];
int A[MAX][21];
int B[MAX][21];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=n-1;i>=1;i--)
    {
        nex[i][0]=i+1;
        for(int j=1;j<=20;j++)nex[i][j]=nex[nex[i][j-1]][j-1];
    }
    A[n][0]=a[n];
    B[n][0]=b[n];
    for(int i=n-1;i>=1;i--)
    {
        A[i][0]=max(a[i],a[i+1]);
        B[i][0]=min(b[i],b[i+1]);
        for(int j=1;j<=20;j++)A[i][j]=max(A[i][j-1],A[nex[i][j-1]][j-1]);
        for(int j=1;j<=20;j++)B[i][j]=min(B[i][j-1],B[nex[i][j-1]][j-1]);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int now=i;
        int L=n+1,R=0;
        //printf("i=%d\n",i);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);

            if(A[now][j]==B[now][j])
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(A[now][j]<B[now][j])now=nex[now][j];
        }
        now=i;
        //printf("L=%d\n",L);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);
            if(A[now][j]==B[now][j])
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(A[now][j]<=B[now][j])now=nex[now][j];
        }
        //printf("R=%d\n",R);
        if(a[i]==b[i])ans++;
        ans+=max(0,(R-L+1));
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int a[MAX],b[MAX];
int nex[MAX][21];
int A[MAX][21];
int B[MAX][21];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=n-1;i>=1;i--)
    {
        nex[i][0]=i+1;
        for(int j=1;j<=20;j++)nex[i][j]=nex[nex[i][j-1]][j-1];
    }
    A[n][0]=a[n];
    B[n][0]=b[n];
    for(int i=n-1;i>=1;i--)
    {
        A[i][0]=max(a[i],a[i+1]);
        B[i][0]=min(b[i],b[i+1]);
        for(int j=1;j<=20;j++)A[i][j]=max(A[i][j-1],A[nex[i][j-1]][j-1]);
        for(int j=1;j<=20;j++)B[i][j]=min(B[i][j-1],B[nex[i][j-1]][j-1]);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int now=i;
        int L=n+1,R=0;
        //printf("i=%d\n",i);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);

            if(A[now][j]==B[now][j])
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(A[now][j]<B[now][j])now=nex[now][j];
        }
        if(a[now]==b[now])L=min(L,now),R=max(R,now);
        now=i;
        //printf("L=%d\n",L);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);
            if(A[now][j]==B[now][j])
            {
                L=min(L,nex[now][j]);
                R=max(R,nex[now][j]);
            }
            if(A[now][j]<=B[now][j])now=nex[now][j];
        }
        //printf("R=%d\n",R);
        if(a[now]==b[now])L=min(L,now),R=max(R,now);
        //if(a[i]==b[i])ans++;
        ans+=max(0,(R-L+1));
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int a[MAX],b[MAX];
int nex[MAX][21];
int A[MAX][21];
int B[MAX][21];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=n-1;i>=1;i--)
    {
        nex[i][0]=i+1;
        for(int j=1;j<=20;j++)nex[i][j]=nex[nex[i][j-1]][j-1];
    }
    A[n][0]=a[n];
    B[n][0]=b[n];
    for(int i=n-1;i>=1;i--)
    {
        A[i][0]=max(a[i],a[i+1]);
        B[i][0]=min(b[i],b[i+1]);
        for(int j=1;j<=20;j++)A[i][j]=max(A[i][j-1],A[nex[i][j-1]][j-1]);
        for(int j=1;j<=20;j++)B[i][j]=min(B[i][j-1],B[nex[i][j-1]][j-1]);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int now=i;
        int L=n+1,R=0;
        //printf("i=%d\n",i);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);

            if(A[now][j]==B[now][j])L=min(L,nex[now][j]);
            if(A[now][j]<B[now][j])now=nex[now][j];
        }
        now=i;
        //printf("L=%d\n",L);
        for(int j=20;j>=0;j--)
        {
            if(nex[now][j]==0)continue;
            //printf("A[%d][%d]=%d\n",now,j,A[now][j]);
            //printf("B[%d][%d]=%d\n",now,j,B[now][j]);
            if(A[now][j]==B[now][j])
            {
                R=max(R,nex[now][j]);
                now=nex[now][j];
            }
            if(A[now][j]<=B[now][j])now=nex[now][j];
        }
        //printf("R=%d\n",R);
        if(a[i]==b[i])ans++;
        ans+=max(0,(R-L+1));
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             5e13
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>
#define all(a)          a.begin(),a.end()
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 200005;
int treeA[4 * N];
int treeB[4 * N];

inline void updateA(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeA[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateA(2*at,L,mid,pos,val);
    updateA(2*at+1,mid+1,R,pos,val);
    treeA[at]=max(treeA[2*at],treeA[2*at+1]);
}

inline int queryA(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return -1e9;
    if(l<=L and R<=r)return treeA[at];

    int mid = (L+R)/2;
    int x = queryA(2*at,L,mid,l,r);
    int y = queryA(2*at+1,mid+1,R,l,r);

    return max(x,y);
}
inline void updateB(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeB[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateB(2*at,L,mid,pos,val);
    updateB(2*at+1,mid+1,R,pos,val);
    treeB[at]=min(treeB[2*at],treeB[2*at+1]);
}

inline int queryB(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return 1e9;
    if(l<=L and R<=r)return treeB[at];

    int mid = (L+R)/2;
    int x = queryB(2*at,L,mid,l,r);
    int y = queryB(2*at+1,mid+1,R,l,r);

    return min(x,y);
}
int n,ara[N],bara[N];
int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",&ara[i]);
        updateA(1,1,n,i,ara[i]);
    }
    for(int i = 1;i <= n;i++){
        scanf("%d",&bara[i]);
        updateB(1,1,n,i,bara[i]);
    }
    LL ans = 0;
    for(int i = 1;i <= n;i++){
        int lo = i, hi = n,mid,ans1 = -1,ans2 = -1;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a >= b){
                ans1 = mid;
                hi = mid - 1;
            }else{
                lo = mid + 1;
            }
        }
        if(ans1 == -1)continue;
        lo = ans1 , hi = n;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a <= b){
                ans2 = mid;
                lo = mid + 1;
            }else{
                hi = mid - 1;
            }
        }
        if(ans2 == -1)continue;
        if(ans1 <= ans2){
            ans += ans2 - ans1 + 1;
        }
    }
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             5e13
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>
#define all(a)          a.begin(),a.end()
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 200005;
int treeA[4 * N];
int treeB[4 * N];

void updateA(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeA[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateA(2*at,L,mid,pos,val);
    updateA(2*at+1,mid+1,R,pos,val);
    treeA[at]=max(treeA[2*at],treeA[2*at+1]);
}

int queryA(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return -1e9;
    if(l<=L and R<=r)return treeA[at];

    int mid = (L+R)/2;
    int x = queryA(2*at,L,mid,l,r);
    int y = queryA(2*at+1,mid+1,R,l,r);

    return max(x,y);
}
void updateB(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeB[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateB(2*at,L,mid,pos,val);
    updateB(2*at+1,mid+1,R,pos,val);
    treeB[at]=min(treeB[2*at],treeB[2*at+1]);
}

int queryB(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return 1e9;
    if(l<=L and R<=r)return treeB[at];

    int mid = (L+R)/2;
    int x = queryB(2*at,L,mid,l,r);
    int y = queryB(2*at+1,mid+1,R,l,r);

    return min(x,y);
}
int n,ara[N],bara[N];
int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",&ara[i]);
        updateA(1,1,n,i,ara[i]);
    }
    for(int i = 1;i <= n;i++){
        scanf("%d",&bara[i]);
        updateB(1,1,n,i,bara[i]);
    }
    LL ans = 0;
    for(int i = 1;i <= n;i++){
        int lo = i, hi = n,mid,ans1 = -1,ans2 = -1;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a >= b){
                ans1 = mid;
                hi = mid - 1;
            }else{
                lo = mid + 1;
            }
        }
        lo = i , hi = n;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a <= b){
                ans2 = mid;
                lo = mid + 1;
            }else{
                hi = mid - 1;
            }
        }
        if(ans1 == -1 or ans2 == -1)continue;
        if(ans1 <= ans2){
            ans += ans2 - ans1 + 1;
        }
    }
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             5e13
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>
#define all(a)          a.begin(),a.end()
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 200005;
int treeA[4 * N];
int treeB[4 * N];

void updateA(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeA[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateA(2*at,L,mid,pos,val);
    updateA(2*at+1,mid+1,R,pos,val);
    treeA[at]=max(treeA[2*at],treeA[2*at+1]);
}

int queryA(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return -1e9;
    if(l<=L and R<=r)return treeA[at];

    int mid = (L+R)/2;
    int x = queryA(2*at,L,mid,l,r);
    int y = queryA(2*at+1,mid+1,R,l,r);

    return max(x,y);
}
void updateB(int at,int L,int R,int pos,int val)
{
    if(pos<L or R<pos)return;

    if(pos==L and pos==R)
    {
        treeB[at] = val;
        return;
    }

    int mid = (L+R)/2;
    updateB(2*at,L,mid,pos,val);
    updateB(2*at+1,mid+1,R,pos,val);
    treeB[at]=min(treeB[2*at],treeB[2*at+1]);
}

int queryB(int at,int L,int R,int l,int r)
{
    if(r<L or R<l)return 1e9;
    if(l<=L and R<=r)return treeB[at];

    int mid = (L+R)/2;
    int x = queryB(2*at,L,mid,l,r);
    int y = queryB(2*at+1,mid+1,R,l,r);

    return min(x,y);
}
int n,ara[N],bara[N];
int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",&ara[i]);
        updateA(1,1,n,i,ara[i]);
    }
    for(int i = 1;i <= n;i++){
        scanf("%d",&bara[i]);
        updateB(1,1,n,i,bara[i]);
    }
    LL ans = 0;
    for(int i = 1;i <= n;i++){
        int lo = i, hi = n,mid,ans1 = -1,ans2 = -1;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a >= b){
                ans1 = mid;
                hi = mid - 1;
            }else{
                lo = mid + 1;
            }
        }
        lo = i , hi = n;
        while(lo <= hi){
            mid = (lo + hi)/2;
            LL a = queryA(1,1,n,i,mid);
            LL b = queryB(1,1,n,i,mid);
            if(a <= b){
                ans2 = mid;
                lo = mid + 1;
            }else{
                hi = mid - 1;
            }
        }
        if(ans1 <= ans2){
            ans += ans2 - ans1 + 1;
        }
    }
    printf("%lld\n",ans);
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<stack>
#include<queue>
#include<unordered_set>
#include<unordered_map>
#define MAXN 200010
#define MAXM 20
using namespace std;
int arr[MAXN+1],arrx[MAXN+1],sptMax[MAXM+1][MAXN+1],sptMin[MAXM+1][MAXN+1],fn[MAXN+1];
void init(int n){
	for(int i=0;(i<<i)<=n;i++)
		fn[(1<<i)]=i;
	for(int i=3;i<=n;i++)
		fn[i]=max(fn[i],fn[i-1]);
	for(int i=0;i<n;i++){
		sptMax[0][i]=arr[i];
		sptMin[0][i]=arrx[i];
	}
	for(int i=1;(1<<i)<=n;i++)
		for(int j=(1<<i)-1;j<n;j++){
			sptMax[i][j]=max(sptMax[i-1][j],sptMax[i-1][j-(1<<(i-1))]);
			sptMin[i][j]=min(sptMin[i-1][j],sptMin[i-1][j-(1<<(i-1))]);
		}
}
int getMaxQuery(int l,int r){
	return max(sptMax[fn[r-l+1]][r],sptMax[fn[r-l+1]][l+(1<<fn[r-l+1])-1]);
}
int getMinQuery(int l,int r){
	return min(sptMin[fn[r-l+1]][r],sptMin[fn[r-l+1]][l+(1<<fn[r-l+1])-1]);
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,l,r,mid,midx;
	long long sol=0LL;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>arr[i];
	for(int i=0;i<n;i++)
		cin>>arrx[i];
	init(n);
	for(int i=0;i<n;i++)
		if(getMaxQuery(0,i)>=getMinQuery(0,i)){
			l=0;
			r=i;
			mid=l+((r-l)/2);
			while(l<r){
				if(getMaxQuery(mid,i)<=getMinQuery(mid,i))
					r=mid;
				else
					l=mid+1;
				mid=l+((r-l)/2);
			}
			if(getMaxQuery(mid,i)==getMinQuery(mid,i)){
				l=mid;
				r=i;
				midx=l+((r-l+1)/2);
				while(l<r){
					if(getMaxQuery(midx,i)==getMinQuery(midx,i))
						l=midx;
					else
						r=midx-1;
					midx=l+((r-l+1)/2);
				}
				sol+=(midx-mid+1);
			}
		}
	cout<<sol<<"\n";
	return 0;
}


 #include<cstdio>
#include<set>
const int N=2e5+1;
std::multiset<int>MA,MI;
long long ans;
int i,n,l[N],r[N],a[N],b[N];
int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%d",a+i);
	for(i=1;i<=n;++i)scanf("%d",b+i);
	for(l[0]=i=1;i<=n;++i)
	{
		MA.insert(a[i]),MI.insert(b[i]);
		for(l[i]=l[i-1];!MA.empty()&&*MA.rbegin()>*MI.begin();MA.erase(MA.find(a[l[i]++])))MI.erase(MI.find(b[l[i]]));
	}
	MA.clear(),MI.clear();
	for(r[0]=i=1;i<=n;ans+=r[i]-l[i],++i)
	{
		MA.insert(a[i]),MI.insert(b[i]);
		for(r[i]=r[i-1];!MA.empty()&&*MA.rbegin()>=*MI.begin();MA.erase(MA.find(a[r[i]++])))MI.erase(MI.find(b[r[i]]));
	}
	printf("%lld",ans);
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<stack>
#include<queue>
#include<unordered_set>
#include<unordered_map>
#define MAXN 200010
#define MAXM 20
using namespace std;
int arr[MAXN+1],arrx[MAXN+1],sptMax[MAXM+1][MAXN+1],sptMin[MAXM+1][MAXN+1],fn[MAXN+1];
void init(int n){
	for(int i=0;(i<<i)<=n;i++)
		fn[(1<<i)]=i;
	for(int i=3;i<=n;i++)
		fn[i]=max(fn[i],fn[i-1]);
	for(int i=0;i<n;i++){
		sptMax[0][i]=arr[i];
		sptMin[0][i]=arrx[i];
	}
	for(int i=1;(1<<i)<=n;i++)
		for(int j=(1<<i)-1;j<n;j++){
			sptMax[i][j]=max(sptMax[i-1][j],sptMax[i-1][j-(1<<(i-1))]);
			sptMin[i][j]=min(sptMin[i-1][j],sptMin[i-1][j-(1<<(i-1))]);
		}
}
int getMaxQuery(int l,int r){
	return max(sptMax[fn[r-l+1]][r],sptMax[fn[r-l+1]][l+(1<<fn[r-l+1])-1]);
}
int getMinQuery(int l,int r){
	return min(sptMin[fn[r-l+1]][r],sptMin[fn[r-l+1]][l+(1<<fn[r-l+1])-1]);
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,l,r,mid,midx;
	long long sol=0LL;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>arr[i];
	for(int i=0;i<n;i++)
		cin>>arrx[i];
	init(n);
	for(int i=0;i<n;i++)
		if(arr[i]<=arrx[i]&&getMaxQuery(0,i)>=getMinQuery(0,i)){
			l=0;
			r=i;
			mid=l+((r-l)/2);
			while(l<r){
				if(getMaxQuery(mid,i)<=getMinQuery(mid,i))
					r=mid;
				else
					l=mid+1;
				mid=l+((r-l)/2);
			}
			if(getMaxQuery(mid,i)==getMinQuery(mid,i)){
				l=mid;
				r=i;
				midx=l+((r-l+1)/2);
				while(l<r){
					if(getMaxQuery(midx,i)==getMinQuery(midx,i))
						l=midx;
					else
						r=midx-1;
					midx=l+((r-l+1)/2);
				}
				sol+=(midx-mid+1);
			}
		}
	cout<<sol<<"\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 2e5 + 5;
const ll INF = 1e9 + 7;

ll n, ans = 0, a[N], b[N], rA[N][25], rB[N][25];

inline ll A(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return max(rA[u][k], rA[v - (1 << k) + 1][k]);
}

inline ll B(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return min(rB[u][k], rB[v - (1 << k) + 1][k]);
}

int main() {
    //freopen("689D.INP", "r", stdin);
    //freopen("689D.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    for (ll i = 1; i <= n; i++) cin >> b[i];
    //********************
    for (ll i = 1; i <= n; i++)
        rA[i][0] = a[i], rB[i][0] = b[i];
    for (ll k = 1; 1 << k <= n; k++)
        for (ll i = 1; i + (1 << k) - 1 <= n; i++) {
            rA[i][k] = max(rA[i + (1 << (k - 1))][k - 1], rA[i][k - 1]);
            rB[i][k] = min(rB[i + (1 << (k - 1))][k - 1], rB[i][k - 1]);
        }
    for (ll i = 1; i <= n; i++) {
        ll l = i, r = n, pos1 = -1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) > B(i, mid))
                pos1 = mid, r = mid - 1;
            else l = mid + 1;
        }

        l = i; r = n; ll pos2 = i - 1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(mid, i) < B(mid, i))
                pos2 = mid, l = mid + 1;
            else r = mid - 1;
        }
        if (pos1 != -1 && pos2 != -1)
            ans += max(0LL, pos1 - pos2 - 1);
    }
    //********************
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 2e5 + 5;
const ll INF = 1e9 + 7;

ll n, ans = 0, a[N], b[N], rA[N][31], rB[N][31];

inline ll A(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return max(rA[u][k], rA[v - (1 << k) + 1][k]);
}

inline ll B(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return min(rB[u][k], rB[v - (1 << k) + 1][k]);
}

int main() {
    //freopen("689D.INP", "r", stdin);
    //freopen("689D.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    for (ll i = 1; i <= n; i++) cin >> b[i];
    //********************
    for (ll i = 1; i <= n; i++)
        rA[i][0] = a[i], rB[i][0] = b[i];
    for (ll k = 1; 1 << k <= n; k++)
        for (ll i = 1; i + (1 << k) - 1 <= n; i++) {
            rA[i][k] = max(rA[i + (1 << (k - 1))][k - 1], rA[i][k - 1]);
            rB[i][k] = min(rB[i + (1 << (k - 1))][k - 1], rB[i][k - 1]);
        }
    for (ll i = 1; i <= n; i++) {
        ll l = i, r = n, pos1 = -1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) > B(i, mid))
                pos1 = mid, r = mid - 1;
            else l = mid + 1;
        }

        l = i; r = n; ll pos2 = i - 1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(mid, i) < B(mid, i))
                pos2 = mid, l = mid + 1;
            else r = mid - 1;
        }
        if (pos1 != -1 && pos2 != -1)
            ans += max(0LL, pos1 - pos2 - 1);
    }
    //********************
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 2e5 + 5;
const ll INF = 1e9 + 7;

ll n, ans = 0, a[N], b[N], rA[N][25], rB[N][25];

inline ll A(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return max(rA[u][k], rA[v - (1 << k) + 1][k]);
}

inline ll B(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return min(rB[u][k], rB[v - (1 << k) + 1][k]);
}

int main() {
    //freopen("689D.INP", "r", stdin);
    //freopen("689D.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    for (ll i = 1; i <= n; i++) cin >> b[i];
    //********************
    for (ll i = 1; i <= n; i++)
        rA[i][0] = a[i], rB[i][0] = b[i];
    for (ll k = 1; 1 << k <= n; k++)
        for (ll i = 1; i + (1 << k) - 1 <= n; i++) {
            rA[i][k] = max(rA[i + (1 << (k - 1))][k - 1], rA[i][k - 1]);
            rB[i][k] = min(rB[i + (1 << (k - 1))][k - 1], rB[i][k - 1]);
        }
    for (ll i = 1; i <= n; i++) {
        ll l = i, r = n, pos1 = n + 1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) > B(i, mid))
                pos1 = mid, r = mid - 1;
            else l = mid + 1;
        }

        l = i; r = n; ll pos2 = i - 1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) < B(i, mid))
                pos2 = mid, l = mid + 1;
            else r = mid - 1;
        }
        if (pos1 != -1 && pos2 != -1)
            ans += max(0LL, pos1 - pos2 - 1);
    }
    //********************
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 2e5 + 5;
const ll INF = 1e9 + 7;

ll n, ans = 0, a[N], b[N], rA[N][25], rB[N][25];

inline ll A(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return max(rA[u][k], rA[v - (1 << k) + 1][k]);
}

inline ll B(const ll &u, const ll &v) {
    ll k = 31 - __builtin_clz(v - u + 1);
    return min(rB[u][k], rB[v - (1 << k) + 1][k]);
}

int main() {
    //freopen("689D.INP", "r", stdin);
    //freopen("689D.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    for (ll i = 1; i <= n; i++) cin >> b[i];
    //********************
    for (ll i = 1; i <= n; i++)
        rA[i][0] = a[i], rB[i][0] = b[i];
    for (ll k = 1; 1 << k <= n; k++)
        for (ll i = 1; i + (1 << k) - 1 <= n; i++) {
            rA[i][k] = max(rA[i + (1 << (k - 1))][k - 1], rA[i][k - 1]);
            rB[i][k] = min(rB[i + (1 << (k - 1))][k - 1], rB[i][k - 1]);
        }
    for (ll i = 1; i <= n; i++) {
        ll l = i, r = n, pos1 = -1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) > B(i, mid))
                pos1 = mid, r = mid - 1;
            else l = mid + 1;
        }

        l = i; r = n; ll pos2 = i - 1;
        while (l <= r) {
            ll mid = (l + r) >> 1;
            if (A(i, mid) < B(i, mid))
                pos2 = mid, l = mid + 1;
            else r = mid - 1;
        }
        if (pos1 != -1 && pos2 != -1)
            ans += max(0LL, pos1 - pos2 - 1);
    }
    //********************
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<=a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>=b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		int fi = tmp[i].first;
		int sz = sz(p[fi]);
		for(int j=0; j<sz; )
		{
			int x = p[fi][j];
			if(i<n && tmp[i+1].first==fi)
			{
				if(x>r[tmp[i].second])
				{
					i++;
					continue;
				}
			}
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
			}
			j++;
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int j=0; j<sz(p[tmp[i].first]); )
		{
			int x = p[tmp[i].first][j];
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
				j++;
			}
			else
			{
				if(i<n && tmp[i+1].first==tmp[i].first)
				{
					if(x>r[tmp[i].second])
					{
						i++;
					}
				}
				else j++;
			}
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		int fi = tmp[i].first;
		int sz = sz(p[fi]);
		for(int j=0; j<sz; )
		{
			int x = p[fi][j];
			if(i<n && tmp[i+1].first==fi)
			{
				if(x>r[tmp[i].second])
				{
					i++;
					continue;
				}
				else break;
			}
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
			}
			j++;
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int j=0; j<sz(p[tmp[i].first]); )
		{
			int x = p[tmp[i].first][j];
			if(i+1<n && tmp[i+1].first==tmp[i].first)
			{
				if(x>r[tmp[i].second])
				{
					i++;
					continue;
				}
			}
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
			}
			j++;
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		int fi = tmp[i].first;
		int sz = sz(p[fi]);
		for(int j=0; j<sz; )
		{
			int x = p[fi][j];
			if(i<n && tmp[i+1].first==fi)
			{
				if(x>r[tmp[i].second])
				{
					i++;
					continue;
				}
				//else break;
			}
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
			}
			j++;
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N], dp1[N][25], dp2[N][25];
void ST(int n)
{
	for(int i=1;i<=n;i++) dp1[i][0] = a[i], dp2[i][0] = b[i];
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i+(1<<j)-1<=n;i++)
		{
			dp1[i][j] = max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
			dp2[i][j] = min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
		}
}
int RMQ1(int l,int r)
{
	int k = 0;
	while((1<<(k+1))<=r-l+1) k++;
	return max(dp1[l][k],dp1[r-(1<<k)+1][k]);
}
int RMQ2(int l,int r)
{
	int k = 0;
	while((1<<(k+1))<=r-l+1) k++;
	return min(dp2[l][k],dp2[r-(1<<k)+1][k]);
}
int findl(int x)
{
	int l = x, r = n, y = 0;
	while(l<=r)
	{
		int mid = (l+r)>>1;
		int cur = RMQ1(x, mid)-RMQ2(x, mid);
		if(cur>0) r = mid - 1;
		else if(cur==0) r = mid - 1, y = mid;
		else l = mid + 1;
	}
	return y;
}
int findr(int x)
{
	int l = x, r = n, y = 0;
	while(l<=r)
	{
		int mid = (l+r)>>1;
		int cur = RMQ1(x, mid)-RMQ2(x, mid);
		if(cur>0) r = mid - 1;
		else if(cur==0) l = mid + 1, y = mid;
		else l = mid + 1;
	}
	return y;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	ST(n);
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		int l = findl(i), r = findr(i);
		if(l && r) ans += r - l + 1;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N];
map <int, vector<int>> p;
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++) p[b[i]].pb(i);
	sort(a+1, a+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int j=0; j<sz(p[a[i]]); )
		{

			int x = p[a[i]][j];
			int L = max(l[i], l2[x]), R = min(r[i], r2[x]);
			if(L<=R && L<=min(i, x) && R>=max(i,x))
			{
				ans += 1ll * (min(i,x)-L+1) * (R-max(i,x)+1);
				j++;
			}
			else
			{
				if(i<n && a[i+1]==a[i]) i++;
				else break;
			}
		}
		while(i<n && a[i+1]==a[i]) i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N];
map <int, vector<int>> p;
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++) p[b[i]].pb(i);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int x : p[a[i]])
		{
			int L = max(l[i], l2[x]), R = min(r[i], r2[x]);
			if(L<=R && L<=i && L<=x && R>=i && R>=x) ans += 1ll * (i-L+1) * (R-x+1);
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N];
map <int, vector<int>> p;
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++) p[b[i]].pb(i);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int x : p[a[i]])
		{
			int L = max(l[i], l2[x]), R = min(r[i], r2[x]);
			if(L<=R && L<=min(i, x) && R>=max(i,x)) ans += 1ll * (min(i,x)-L+1) * (R-max(i,x)+1);
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
int n, a[N], b[N];
int l[N], r[N], sta[N], top;
int l2[N], r2[N], id[N];
map <int, vector<int>> p;
pii tmp[N];
void init()
{
	for(int i=1;i<=n;i++)
	{
		while(top&&a[sta[top]]<a[i]) r[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&a[sta[top]]<=a[i]) l[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l[sta[top--]] = 1;

	for(int i=1;i<=n;i++)
	{
		while(top&&b[sta[top]]>b[i]) r2[sta[top--]] = i - 1;
		sta[++top] = i;
	}
	while(top) r2[sta[top--]] = n;
	for(int i=n;i;i--)
	{
		while(top&&b[sta[top]]>=b[i]) l2[sta[top--]] = i + 1;
		sta[++top] = i;
	}
	while(top) l2[sta[top--]] = 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++) cin >> b[i];
	for(int i=1; i<=n; i++)
	{
		p[b[i]].pb(i);
		tmp[i] = {a[i], i};
	}
	sort(tmp+1, tmp+n+1);
	init();
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int j=0; j<sz(p[tmp[i].first]); )
		{

			int x = p[tmp[i].first][j];
			int L = max(l[tmp[i].second], l2[x]), R = min(r[tmp[i].second], r2[x]);
			if(L<=R && L<=min(tmp[i].second, x) && R>=max(tmp[i].second,x))
			{
				ans += 1ll * (min(tmp[i].second,x)-L+1) * (R-max(tmp[i].second,x)+1);
				j++;
			}
			else
			{
				if(i<n && tmp[i+1].first==tmp[i].first) i++;
				else break;
			}
		}
		while(i<n && tmp[i+1].first==tmp[i].first) i++;
	}
	cout << ans;
	return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];
int slatinaru_si_andries[30];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

int main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>rmqmax[0][i];
    for(int i=1;i<=n;i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1;(1<<i)<=n;i++)
    {   for(int j=1;j+(1<<i)<=n;j++)
        {   rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<i)-1]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<i)-1]);
            ans+=(rmqmax[i][j]==rmqmin[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[20][200005];
int rmqmax[20][200005];
int slatinaru_si_andries[101];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

int main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>rmqmax[0][i];
    for(int i=1;i<=n;i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1;i<=n;i++)
        ans+=(rmqmax[0][i]==rmqmin[0][i]);
    for(int i=1;(1<<i)<=n;i++)
    {   for(int j=1;j+(1<<i)<=n;j++)
        {   rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<i)]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<i)]);
            ans+=(rmqmax[i][j]==rmqmin[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>rmqmax[0][i];
    for(int i=1;i<=n;i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1;i<=n;i++)
        ans+=(rmqmax[0][i]==rmqmin[0][i]);
    for(int i=1;(1<<i)<=n;i++)
    {   for(int j=1;j+(1<<i)<=n;j++)
        {   rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<i)-1]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<i)-1]);
            ans+=(rmqmax[i][j]==rmqmin[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];
int slatinaru_si_andries[200001];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

int main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[20][200005];
int rmqmax[20][200005];
int slatinaru_si_andries[200001];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

int main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>rmqmax[0][i];
    for(int i=1;i<=n;i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1;i<=n;i++)
        ans+=(rmqmax[0][i]==rmqmin[0][i]);
    for(int i=1;(1<<i)<=n;i++)
    {   for(int j=1;j+(1<<(i-1))<=n;j++)
        {   rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
            ans+=(rmqmax[i][j]==rmqmin[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];
int slatinaru_si_andries[200001];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[20][200005];
int rmqmax[20][200005];
int slatinaru_si_andries[30];

int query1(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return min(rmqmin[l][a],rmqmin[l][b-(1<<l)+1]);
}
int query2(int a,int b)
{
    int l=slatinaru_si_andries[b-a+1];
    return max(rmqmax[l][a],rmqmax[l][b-(1<<l)+1]);
}

int main()
{
    cin>>n;
    for(int i=2; i<=n; i++) slatinaru_si_andries[i]=slatinaru_si_andries[i/2]+1;
    for(int i=1; i<=n; i++) cin>>rmqmax[0][i];
    for(int i=1; i<=n; i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
        }
    }
    for(int i=1; i<=n; i++)
    {
        int st=i;
        int dr=n;
        int ansst=i,ansdr=i;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<query1(i,mij)) st=mij+1;
            else ansst=mij,dr=mij-1;
        }
        st=i,dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)>>1;
            if( query2(i,mij)<=query1(i,mij)) ansdr=mij,st=mij+1;
            else dr=mij-1;
        }
        if(query1(i,ansst)==query2(i,ansst)) ans+=ansdr-ansst+1;
    }
    cout<<ans;
    return 0;
}


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

int n,rmqmin[30][200005];
int rmqmax[30][200005];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>rmqmax[0][i];
    for(int i=1;i<=n;i++) cin>>rmqmin[0][i];
    int ans=0;
    for(int i=1;i<=n;i++)
        ans+=(rmqmax[0][i]==rmqmin[0][i]);
    for(int i=1;(1<<i)<=n;i++)
    {   for(int j=1;j+(1<<i)<=n;j++)
        {   rmqmax[i][j]=max(rmqmax[i-1][j],rmqmax[i-1][j+(1<<(i-1))]);
            rmqmin[i][j]=min(rmqmin[i-1][j],rmqmin[i-1][j+(1<<(i-1))]);
            ans+=(rmqmax[i][j]==rmqmin[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e15;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

ll getMn(ll r)
{
	ll result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

ll getMx(ll r)
{
	ll result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	addMn(N, -Inf);
	addMx(N, Inf);

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef int ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

int getMn(int r)
{
	int result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

int getMx(int r)
{
	int result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e15;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

ll getMn(ll r)
{
	ll result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

ll getMx(ll r)
{
	ll result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e15;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

ll getMn(ll r)
{
	ll result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

ll getMx(ll r)
{
	ll result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	addMn(N+1, -Inf);
	addMx(N+1, Inf);

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e15;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

ll getMn(ll r)
{
	ll result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

ll getMx(ll r)
{
	ll result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(ll i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	addMn(N, -Inf);
	addMx(N, Inf);

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

int getMn(int r)
{
	int result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

int getMx(int r)
{
	int result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e15;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

int getMn(int r)
{
	ll result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(int i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

int getMx(int r)
{
	ll result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(int i, ll new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	fill(Mx, Mx + MaxN, -Inf);
	fill(Mn, Mn + MaxN, Inf);
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN * 4];
ll Mn[MaxN * 4];

ll At[MaxN];
ll Bt[MaxN];

void Build(ll l, ll r, ll id) {
	if (l == r) {
		Mn[id] = Bt[l];
		Mx[id] = At[l];
	}
	else {
		ll mid = (l + r) / 2;
		Build(l, mid, id * 2 + 1);
		Build(mid + 1, r, id * 2 + 2);
		Mn[id] = min(Mn[id * 2 + 1], Mn[id * 2 + 2]);
		Mx[id] = max(Mx[id * 2 + 1], Mx[id * 2 + 2]);
	}
}

ll getMn(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mn[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMn(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMn(mid + 1, r, x, y, id * 2 + 2);
		else {
			return min(getMn(l, mid, x, mid, id * 2 + 1), getMn(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}
ll getMx(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mx[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMx(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMx(mid + 1, r, x, y, id * 2 + 2);
		else {
			return max(getMx(l, mid, x, mid, id * 2 + 1), getMx(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	Build(0, N - 1, 0);
	ll Ans = 0;
	for (int i = 0;i < N;i++) {
		ll l = i;
		ll r = N - 1;
		while (l < r) {
			ll mid = (l + r) / 2;
			if (getMx(0, N - 1, i, mid, 0) - getMn(0, N - 1, i, mid, 0) <= 0)l = mid + 1;
			else r = mid;
		}
		ll k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			ll mid = (l + r) / 2;
			if (getMx(0, N - 1, i, mid, 0) - getMn(0, N - 1, i, mid, 0) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += k - l;
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 1e3 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN * 4];
ll Mn[MaxN * 4];

ll At[MaxN];
ll Bt[MaxN];

void Build(ll l, ll r, ll id) {
	if (l == r) {
		Mn[id] = Bt[l];
		Mx[id] = At[l];
	}
	else {
		ll mid = (l + r) / 2;
		Build(l, mid, id * 2 + 1);
		Build(mid + 1, r, id * 2 + 2);
		Mn[id] = min(Mn[id * 2 + 1], Mn[id * 2 + 2]);
		Mx[id] = max(Mx[id * 2 + 1], Mx[id * 2 + 2]);
	}
}

ll getMn(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mn[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMn(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMn(mid + 1, r, x, y, id * 2 + 2);
		else {
			return min(getMn(l, mid, x, mid, id * 2 + 1), getMn(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}
ll getMx(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mx[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMx(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMx(mid + 1, r, x, y, id * 2 + 2);
		else {
			return max(getMx(l, mid, x, mid, id * 2 + 1), getMx(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	Build(0, N - 1, 0);
	ll l = 0;
	ll Ans = 0;
	ll r;
	for ( r = 0;r < N;r++) {
		while (l <= r && getMx(0, N - 1, l, r, 0) - getMn(0, N - 1, l, r, 0) > 0) {
			if(l < r && getMx(0, N - 1, l, r-1, 0) - getMn(0, N - 1, l, r-1, 0) == 0)
				Ans += r - l;
			l++;
		}
	}
	while (l < r) {
		if (getMx(0, N - 1, l, r-1, 0) - getMn(0, N - 1, l, r-1, 0) == 0)
			Ans += r - l;
		l++;
	}
	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef int ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

int getMn(int r)
{
	int result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

int getMx(int r)
{
	int result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	memset(Mx, -63, sizeof(Mx));
	memset(Mn, 63, sizeof(Mn));
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	ll Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += k - l;
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef int ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 2e5 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN];
ll Mn[MaxN];

ll At[MaxN];
ll Bt[MaxN];

int getMn(int r)
{
	int result = Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = min(result, Mn[r]);
	return result;
}

void addMn(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mn[i] = min(Mn[i], new_val);
}

int getMx(int r)
{
	int result = -Inf;
	for (; r >= 0; r = (r & (r + 1)) - 1)
		result = max(result, Mx[r]);
	return result;
}

void addMx(int i, int new_val)
{
	for (; i < MaxN; i = (i | (i + 1)))
		Mx[i] = max(Mx[i], new_val);
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	memset(Mx, -63, sizeof(Mx));
	memset(Mn, 63, sizeof(Mn));
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}

	long long Ans = 0;
	ll r, l, k;
	ll mid;
	for (int i = N-1;i >= 0;i--) {
		addMn(i, Bt[i]);
		addMx(i, At[i]);
		 l = i;
		 r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) <= 0)l = mid + 1;
			else r = mid;
		}
		 k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			mid = (l + r) / 2;
			if (getMx(mid) - getMn(mid) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += 1ll * (k - l);
	}

	cout << Ans;
	return 0;

}


#include <bits/stdc++.h>

typedef long long ll;

typedef long double ld;

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
#define sqr(x) (x) * (x)
#define sz(x) (x).size()
//#define min(a,b) ((a) < (b)? (a) : (b))
//#define max(a,b) ((a) > (b)? (a) : (b))

const ll MaxN = 1e3 + 100;
const ll LogN = 20;
const ll Inf = 2e9;
double eps = 1e-3;
ll md = 1e9 + 7;
ll dx[4] = { 1,0,0,-1 };
ll dy[4] = { 0,1,-1,0 };

ll Mx[MaxN * 4];
ll Mn[MaxN * 4];

ll At[MaxN];
ll Bt[MaxN];

void Build(ll l, ll r, ll id) {
	if (l == r) {
		Mn[id] = Bt[l];
		Mx[id] = At[l];
	}
	else {
		ll mid = (l + r) / 2;
		Build(l, mid, id * 2 + 1);
		Build(mid + 1, r, id * 2 + 2);
		Mn[id] = min(Mn[id * 2 + 1], Mn[id * 2 + 2]);
		Mx[id] = max(Mx[id * 2 + 1], Mx[id * 2 + 2]);
	}
}

ll getMn(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mn[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMn(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMn(mid + 1, r, x, y, id * 2 + 2);
		else {
			return min(getMn(l, mid, x, mid, id * 2 + 1), getMn(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}
ll getMx(ll l, ll r, ll x, ll y, ll id) {
	if (x == l && y == r) {
		return Mx[id];
	}
	else {
		ll mid = (l + r) / 2;
		if (y <= mid) return getMx(l, mid, x, y, id * 2 + 1);
		else if (x >= mid + 1) return getMx(mid + 1, r, x, y, id * 2 + 2);
		else {
			return max(getMx(l, mid, x, mid, id * 2 + 1), getMx(mid + 1, r, mid + 1, y, id * 2 + 2));
		}
	}
}

int main()
{
#ifdef LOCAL
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#else
	//ifstream cin("next.in");
	//ofstream cout("next.out");
#endif
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);

	ll N;
	cin >> N;
	for (int i = 0;i < N;i++) {
		cin >> At[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> Bt[i];
	}
	Build(0, N - 1, 0);
	ll Ans = 0;
	for (int i = 0;i < N;i++) {
		ll l = i;
		ll r = N - 1;
		while (l < r) {
			ll mid = (l + r) / 2;
			if (getMx(0, N - 1, i, mid, 0) - getMn(0, N - 1, i, mid, 0) <= 0)l = mid + 1;
			else r = mid;
		}
		ll k = l;
		l = i;
		r = N - 1;
		while (l < r) {
			ll mid = (l + r) / 2;
			if (getMx(0, N - 1, i, mid, 0) - getMn(0, N - 1, i, mid, 0) < 0)l = mid + 1;
			else r = mid;
		}
		Ans += k - l;
	}

	cout << Ans;
	return 0;

}


/*
    Problem: 689D. Friends and Subsequences

    Solution: binary search, sparse table, O(n log(n))

    Author: Dmitry Kozyrev, github: dmkz, e-mail: dmkozyrev@rambler.ru
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
    const int PMAX = 18, NMAX = 256 * 1024;
    static int max[PMAX][NMAX], min[PMAX][NMAX], log[NMAX];
    for (int i = 2; i < NMAX; ++i) {
        log[i] = log[i / 2] + 1;
    }
    int n;
    while (std::cin >> n) {
        for (int i = 0; i < n; ++i) {
            std::cin >> max[0][i];
        }
        for (int i = 0; i < n; ++i) {
            std::cin >> min[0][i];
        }
        for (int p = 1; p < PMAX; ++p) {
            for (int i = 0; i + (1 << p) - 1 < n; ++i) {
                max[p][i] = std::max(max[p-1][i], max[p-1][i+(1<<(p-1))]);
                min[p][i] = std::min(min[p-1][i], min[p-1][i+(1<<(p-1))]);
            }
        }
        std::function<int(int,int)> get_min = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::min(min[pow][l], min[pow][r-(1 << pow)+1]);
        };
        std::function<int(int,int)> get_max = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::max(max[pow][l], max[pow][r-(1 << pow)+1]);
        };
        int64_t answ = 0;
        for (int i = 0; i < n; ++i) {
            int begin, end;
            {
                int low = i-1, high = n;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) < get_max(i, mid)) {
                        high = mid;
                    } else {
                        low = mid;
                    }
                }
                end = high;
            }
            {
                int low = i-1, high = end;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) > get_max(i, mid)) {
                        low = mid;
                    } else {
                        high = mid;
                    }
                }
                begin = high;
            }
            answ += end - begin;
        }
        std::cout << answ << std::endl;
    }

    return 0;
}


/*
    Problem: 689D. Friends and Subsequences

    Solution: binary search, sparse table, O(n log(n))

    Author: Dmitry Kozyrev, github: dmkz, e-mail: dmkozyrev@rambler.ru
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
    const int PMAX = 18, NMAX = 256 * 1024;
    static int max[PMAX][NMAX], min[PMAX][NMAX], log[NMAX];
    for (int i = 2; i < NMAX; ++i) {
        log[i] = log[i / 2] + 1;
    }
    int n;
    while (std::cin >> n) {
        for (int i = 0; i < n; ++i) {
            std::cin >> max[0][i];
        }
        for (int i = 0; i < n; ++i) {
            std::cin >> min[0][i];
        }
        for (int p = 1; p < PMAX; ++p) {
            for (int i = 0; i + (1 << p) - 1 < n; ++i) {
                max[p][i] = std::max(max[p-1][i], max[p-1][i+(1<<(p-1))]);
                min[p][i] = std::min(min[p-1][i], min[p-1][i+(1<<(p-1))]);
            }
        }
        std::function<int(int,int)> get_min = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::min(min[pow][l], min[pow][r-(1 << pow)+1]);
        };
        std::function<int(int,int)> get_max = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::max(max[pow][l], max[pow][r-(1 << pow)+1]);
        };
        int64_t answ = 0;
        for (int i = 0; i < n; ++i) {
            if (min[0][i] < max[0][i]) continue;
            int begin, end;
            // min >= max
            // find equal range
            {
                int low = i, high = n;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) < get_max(i, mid)) {
                        high = mid;
                    } else {
                        low = mid;
                    }
                }
                end = high;
            }
            {
                int low = i-1, high = end;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) > get_max(i, mid)) {
                        low = mid;
                    } else {
                        high = mid;
                    }
                }
                begin = high;
            }
            answ += end - begin;
        }
        std::cout << answ << std::endl;
    }

    return 0;
}


/*
    Problem: 689D. Friends and Subsequences

    Solution: binary search, sparse table, O(n log(n))

    Author: Dmitry Kozyrev, github: dmkz, e-mail: dmkozyrev@rambler.ru
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
    const int PMAX = 18, NMAX = 256 * 1024;
    static int max[PMAX][NMAX], min[PMAX][NMAX], log[NMAX];
    for (int i = 2; i < NMAX; ++i) {
        log[i] = log[i / 2] + 1;
    }
    int n;
    while (std::cin >> n) {
        for (int i = 0; i < n; ++i) {
            std::cin >> max[0][i];
        }
        for (int i = 0; i < n; ++i) {
            std::cin >> min[0][i];
        }
        for (int p = 1; p < PMAX; ++p) {
            for (int i = 0; i + (1 << p) - 1 < n; ++i) {
                max[p][i] = std::max(max[p-1][i], max[p-1][i+(1<<(p-1))]);
                min[p][i] = std::min(min[p-1][i], min[p-1][i+(1<<(p-1))]);
            }
        }
        std::function<int(int,int)> get_min = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::min(min[pow][l], min[pow][r-(1 << pow)+1]);
        };
        std::function<int(int,int)> get_max = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::max(max[pow][l], max[pow][r-(1 << pow)+1]);
        };
        int64_t answ = 0;
        for (int i = 0; i < n; ++i) {
            if (min[0][i] < max[0][i]) continue;
            int begin, end;
            // min >= max
            // find equal range
            {
                int low = i, high = n;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) < get_max(i, mid)) {
                        high = mid;
                    } else {
                        low = mid;
                    }
                }
                end = high;
            }
            if (min[0][i] == max[0][i]) {
                begin = i;
            } else {
                int low = i, high = end;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) > get_max(i, mid)) {
                        low = mid;
                    } else {
                        high = mid;
                    }
                }
                begin = high;
            }
            answ += end - begin;
        }
        std::cout << answ << std::endl;
    }

    return 0;
}


/*
    Problem: 689D. Friends and Subsequences

    Solution: binary search, sparse table, O(n log(n))

    Author: Dmitry Kozyrev, github: dmkz, e-mail: dmkozyrev@rambler.ru
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
    // Sparse tables' parameters:
    const int PMAX = 18, NMAX = 256 * 1024;
    static int max[PMAX][NMAX], min[PMAX][NMAX], log[NMAX];
    for (int i = 2; i < NMAX; ++i) {
        log[i] = log[i / 2] + 1;
    }
    int n;
    while (std::cin >> n) {
        // Input `a` sequence:
        for (int i = 0; i < n; ++i) {
            std::cin >> max[0][i];
        }
        // Input `b` sequence:
        for (int i = 0; i < n; ++i) {
            std::cin >> min[0][i];
        }
        // Build sparse tables for `max` over `a` and `min` over `b`:
        for (int p = 1; p < PMAX; ++p) {
            for (int i = 0; i + (1 << p) - 1 < n; ++i) {
                max[p][i] = std::max(max[p-1][i], max[p-1][i+(1<<(p-1))]);
                min[p][i] = std::min(min[p-1][i], min[p-1][i+(1<<(p-1))]);
            }
        }
        // Queries to min / max:
        std::function<int(int,int)> get_min = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::min(min[pow][l], min[pow][r-(1 << pow)+1]);
        };
        std::function<int(int,int)> get_max = [&](int l, int r) {
            int len = r - l + 1; int pow = log[len];
            return std::max(max[pow][l], max[pow][r-(1 << pow)+1]);
        };
        // Calculate number of equal subsegments using binary search:
        int64_t answ = 0;
        for (int i = 0; i < n; ++i) {
            int begin, end;
            { // upper bound:
                int low = i-1, high = n;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) < get_max(i, mid)) {
                        high = mid;
                    } else {
                        low = mid;
                    }
                }
                end = high;
            }
            { // lower bound:
                int low = i-1, high = end;
                while (high - low > 1) {
                    int mid = (low + high) / 2;
                    if (get_min(i, mid) > get_max(i, mid)) {
                        low = mid;
                    } else {
                        high = mid;
                    }
                }
                begin = high;
            }
            // Equal range:
            answ += end - begin;
        }
        std::cout << answ << std::endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N];

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	ll ans = 0;
	for(int i=1;i<=n;i++){
		set<int> st[2];
		for(int j=i;j<=n;j++){
			st[0].insert(-a[j]);
			st[1].insert(b[j]);
			if(-*st[0].begin() > *st[1].begin())break;
			if(-*st[0].begin() == *st[1].begin())ans++;
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N],l,r;
pair<int,int> seg[4*N];

void build(int n,int s,int e){
	if(s == e){
		seg[n] = {a[s],b[s]};
		return;
	}
	build(n*2,s,(s+e)/2);
	build(n*2+1,(s+e)/2+1,e);
	seg[n] = {max(seg[n*2].first,seg[n*2+1].first),min(seg[n*2].second,seg[n*2+1].second)};
}

pair<int,int> get(int n,int s,int e){
	if(s > r || e < l)return {-2e9,2e9};
	if(s >= l && e <= r)return seg[n];
	pair<int,int> a = get(n*2,s,(s+e)/2);
	pair<int,int> b = get(n*2+1,(s+e)/2+1,e);
	return {max(a.first,b.first),min(a.second,b.second)};
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	build(1,1,n);
	ll ans = 0;
	l = 1;
	r = n;
	for(int i=1;i<=n;i++){
		l = r = i;
		int x=i-1,y=i;
		pair<int,int> cur = get(1,1,n);
		if(cur.first <= cur.second){
			for(int k=30;k>=0;k--){
				int m = (1<<k);
				if(x+m <= n){
					l = i;
					r = x+m;
					cur = get(1,1,n);
					if(cur.first < cur.second){
						x+=m;
					}
				}
				if(y+m <= n){
					l = i;
					r = y+m;
					cur = get(1,1,n);
					if(cur.first <= cur.second)y+=m;
				}
			}
			ans += y-x;
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N],l,r;
pair<int,int> seg[N];

void build(int n,int s,int e){
	if(s == e){
		seg[n] = {a[s],b[s]};
		return;
	}
	build(n*2,s,(s+e)/2);
	build(n*2+1,(s+e)/2+1,e);
	seg[n] = {max(seg[n*2].first,seg[n*2+1].first),min(seg[n*2].second,seg[n*2+1].second)};
}

pair<int,int> get(int n,int s,int e){
	if(s > r || e < l)return {0,2e9};
	if(s >= l && e <= r)return seg[n];
	pair<int,int> a = get(n*2,s,(s+e)/2);
	pair<int,int> b = get(n*2+1,(s+e)/2+1,e);
	return {max(a.first,b.first),min(a.second,b.second)};
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	build(1,1,n);
	ll ans = 0;
	l = 1;
	r = n;
	for(int i=1;i<=n;i++){
		l = r = i;
		int x=i-1,y=i;
		pair<int,int> cur = get(1,1,n);
		if(cur.first <= cur.second){
			for(int k=30;k>=0;k--){
				int m = (1<<k);
				if(x+m <= n){
					l = i;
					r = x+m;
					cur = get(1,1,n);
					if(cur.first < cur.second){
						x+=m;
					}
				}
				if(y+m <= n){
					l = i;
					r = y+m;
					cur = get(1,1,n);
					if(cur.first <= cur.second)y+=m;
				}
			}
			ans += y-x;
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N];

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	ll ans = 0;
	for(int i=1;i<=n;i++){
		set<int> st[2];
		for(int j=i;j<=n;j++){
			st[0].insert(-a[j]);
			st[1].insert(b[j]);
			if(-*st[0].begin() == *st[1].begin())ans++;
		}
	}
	cout << ans << endl;
}