#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <numeric>
#include <complex>

using namespace std;

typedef long long ll;

#define mp make_pair
#define pb push_back
#define PI 3.1415926535897932384626433832795

#define fill(x, v)  fillchar(x, v, sizeof(x))
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector< pii >   vpii;

#define MAXN 10500
#define MOD 1000000007

int  f[6][MAXN];
char s[MAXN];
int  n, ox, oy;
ll   ans;
map<pii, int> m;
map<pii, int>::iterator it;

void dbg(int k = n) {
    for(int i=1; i<=3; ++i) {
        char c;
        for(int j=1; j<=k; ++j) {
            if (f[i][j]) c = '#'; else c = '.';
            if (i == oy && j == ox) c = 'O';
            printf("%c", c);
        }
        printf("\n");
    }
    printf("\n");
}

int rec(int x, int y) {
    int nx, ny, mask;

    //dbg();

    if (x == n + 1) return 1;
    if (y == 1) {
        mask = f[1][x] | (f[2][x] << 1) | (f[3][x] << 2);// | (f[1][x + 1] << 3) | (f[2][x + 1] << 4) | (f[3][x + 1] << 5);

        it = m.find( mp(x, mask) );
        if (it != m.end()) return it->second;
    }

    //printf("x=%i y=%i\n", x, y);
    //dbg();
    int tmp = 0;

    if (f[y][x] == 0) {
        if (f[y][x] == 0 && f[y][x + 1] == 0) {
            f[y][x] = f[y][x + 1] = 1;
            ny = y + 1;
            nx = x;
            if (ny == 4) {
                nx++;
                ny = 1;
            }

            tmp = tmp + rec(nx, ny);
            if (tmp >= MOD) tmp -= MOD;

            f[y][x] = f[y][x + 1] = 0;
        }

        if (f[y][x] == 0 && f[y + 1][x] == 0) {
            f[y][x] = f[y + 1][x] = 1;
            ny = y + 2;
            nx = x;
            if (ny == 4) {
                nx++;
                ny = 1;
            }

            tmp = tmp + rec(nx, ny);

            if (tmp >= MOD) tmp -= MOD;

            f[y][x] = f[y + 1][x] = 0;
        }

    } else {
        ny = y + 1;
        nx = x;
        if (ny == 4) ny = 1, nx++;
        tmp = rec(nx, ny);
    }

    if (y == 1) {
        m[mp(x, mask)] = tmp;
    }

    //printf("tmp=%i\n", tmp);
    //dbg();

    return tmp;
}

int solve() {
    m.clear();
    return rec(1, 1);
}

ll go5(int cnt) {
    if (cnt > 0) {
        //dbg();
        return solve();
    } else {
        return 0;
    }
}

ll go4(int cnt) {
    ll tmp = 0;

    if (oy <= 1 && !f[oy + 1][ox] && !f[oy + 2][ox]) {
        f[oy + 1][ox] = f[oy + 2][ox] = 1;
        tmp += go5(cnt + 1);
        f[oy + 1][ox] = f[oy + 2][ox] = 0;
    }

    if (f[oy + 1][ox]) tmp += go5(cnt);

    if (!f[oy + 1][ox] && !f[oy + 1][ox - 1]) {
        f[oy + 1][ox] = f[oy + 1][ox - 1] = 1;
        tmp += go5(cnt);
        f[oy + 1][ox] = f[oy + 1][ox - 1] = 0;
    }

    if (!f[oy + 1][ox] && !f[oy + 1][ox + 1]) {
        f[oy + 1][ox] = f[oy + 1][ox + 1] = 1;
        tmp += go5(cnt);
        f[oy + 1][ox] = f[oy + 1][ox + 1] = 0;
    }

    return tmp;
}

ll go3(int cnt) {
    ll tmp = 0;

    if (oy >= 3 && !f[oy - 1][ox] && !f[oy - 2][ox]) {
        f[oy - 1][ox] = f[oy - 2][ox] = 1;
        tmp += go4(cnt + 1);
        f[oy - 1][ox] = f[oy - 2][ox] = 0;
    }

    if (f[oy - 1][ox]) tmp += go4(cnt);

    if (!f[oy - 1][ox] && !f[oy - 1][ox - 1]) {
        f[oy - 1][ox] = f[oy - 1][ox - 1] = 1;
        tmp += go4(cnt);
        f[oy - 1][ox] = f[oy - 1][ox - 1] = 0;
    }

    if (!f[oy - 1][ox] && !f[oy - 1][ox + 1]) {
        f[oy - 1][ox] = f[oy - 1][ox + 1] = 1;
        tmp += go4(cnt);
        f[oy - 1][ox] = f[oy - 1][ox + 1] = 0;
    }

    return tmp;
}

ll go2(int cnt) {
    ll tmp = 0;

    if (!f[oy][ox + 1] && !f[oy][ox + 2]) {
        f[oy][ox + 1] = f[oy][ox + 2] = 1;
        tmp += go3(cnt + 1);
        f[oy][ox + 1] = f[oy][ox + 2] = 0;
    }

    if (f[oy][ox + 1]) tmp += go3(cnt);

    if (oy >= 2 && !f[oy][ox + 1] && !f[oy - 1][ox + 1]) {
        f[oy][ox + 1] = f[oy - 1][ox + 1] = 1;
        tmp += go3(cnt);
        f[oy][ox + 1] = f[oy - 1][ox + 1] = 0;
    }

    if (oy <= 2 && !f[oy][ox + 1] && !f[oy + 1][ox + 1]) {
        f[oy][ox + 1] = f[oy + 1][ox + 1] = 1;
        tmp += go3(cnt);
        f[oy][ox + 1] = f[oy + 1][ox + 1] = 0;
    }

    return tmp;
}

ll go1(int cnt) {
    ll tmp = 0;

    if (ox >= 3 && !f[oy][ox - 1] && !f[oy][ox - 2]) {
        f[oy][ox - 1] = f[oy][ox - 2] = 1;
        tmp += go2(cnt + 1);
        f[oy][ox - 1] = f[oy][ox - 2] = 0;
    }

    if (f[oy][ox - 1]) tmp += go2(cnt);

    if (oy >= 2 && !f[oy][ox - 1] && !f[oy - 1][ox - 1]) {
        f[oy][ox - 1] = f[oy - 1][ox - 1] = 1;
        tmp += go2(cnt);
        f[oy][ox - 1] = f[oy - 1][ox - 1] = 0;
    }

    if (oy <= 2 && !f[oy][ox - 1] && !f[oy + 1][ox - 1]) {
        f[oy][ox - 1] = f[oy + 1][ox - 1] = 1;
        tmp += go2(cnt);
        f[oy][ox - 1] = f[oy + 1][ox - 1] = 0;
    }

    return tmp;
}

int main() {
    int tc;
    scanf("%i", &n);

    for(int i=0; i<6; ++i)
        for(int j=0; j<MAXN; ++j) f[i][j] = 1;

    for(int i=1; i<=3; ++i) {
        scanf("%s", s);

        for(int j=0; j<n; ++j) {
            if (s[j] == '.') {
                f[i][j + 1] = 0;
            } else if (s[j] == 'X') {
                f[i][j + 1] = 1;
            } else if (s[j] == 'O') {
                f[i][j + 1] = 1;
                oy = i;
                ox = j + 1;
            }
        }
    }

    //cout << solve() << endl;
    //cout << endl;

    int x = go1(0) % MOD;
    printf("%i\n", x);

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <numeric>
#include <complex>

using namespace std;

typedef long long ll;

#define mp make_pair
#define pb push_back
#define PI 3.1415926535897932384626433832795

#define fill(x, v)  fillchar(x, v, sizeof(x))
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector< pii >   vpii;

#define MAXN 10500
#define MOD 1000000007

int  f[6][MAXN];
char s[MAXN];
int  n, ox, oy;
ll   ans;

void dbg() {
    for(int i=1; i<=3; ++i) {
        char c;
        for(int j=1; j<=n; ++j) {
            if (f[i][j]) c = '#'; else c = '.';
            printf("%c", c);
        }
        printf("\n");
    }
}

int rec(int x, int y) {
    int nx, ny;

    if (x == n + 1) return 1;

    //printf("x=%i y=%i\n", x, y);
    //dbg();
    int tmp = 0;

    if (f[y][x] == 0) {
        if (f[y][x] == 0 && f[y][x + 1] == 0) {
            f[y][x] = f[y][x + 1] = 1;
            ny = y + 1;
            nx = x;
            if (ny == 4) {
                nx++;
                ny = 1;
            }

            tmp = tmp + rec(nx, ny);
            if (tmp >= MOD) tmp -= MOD;

            f[y][x] = f[y][x + 1] = 0;
        }

        if (f[y][x] == 0 && f[y + 1][x] == 0) {
            f[y][x] = f[y + 1][x] = 1;
            ny = y + 2;
            nx = x;
            if (ny == 4) {
                nx++;
                ny = 1;
            }

            tmp = tmp + rec(nx, ny);

            if (tmp >= MOD) tmp -= MOD;

            f[y][x] = f[y + 1][x] = 0;
        }

    } else {
        ny = y + 1;
        nx = x;
        if (ny == 4) ny = 1, nx++;
        return rec(nx, ny);
    }

    return tmp;
}

int main() {
    int tc;
    scanf("%i", &n);

    memset(f, 1, sizeof(f));

    for(int i=1; i<=3; ++i) {
        scanf("%s", s);

        for(int j=0; j<n; ++j) {
            if (s[j] == '.') {
                f[i][j + 1] = 0;
            } else if (s[j] == 'X') {
                f[i][j + 1] = 2;
            } else if (s[j] == 'O') {
                f[i][j + 1] = 3;
                oy = i;
                ox = j + 1;
            }
        }
    }

    if (f[oy][ox + 1] == 0 && f[oy][ox + 2] == 0) {
        f[oy][ox + 1] = 1;
        f[oy][ox + 2] = 1;

        if (oy >= 2 && f[oy][ox - 1] == 0 && f[oy - 1][ox - 1] == 0) {
            f[oy][ox - 1] = 1;
            f[oy - 1][ox - 1] = 1;

            ans += rec(1, 1);

            f[oy][ox - 1] = 0;
            f[oy - 1][ox - 1] = 0;
        }

        if (oy <= 2 && f[oy][ox - 1] == 0 && f[oy + 1][ox - 1] == 0) {
            f[oy][ox - 1] = 1;
            f[oy + 1][ox - 1] = 1;

            ans += rec(1, 1);

            f[oy][ox - 1] = 0;
            f[oy + 1][ox - 1] = 0;
        }

        f[oy][ox + 1] = 0;
        f[oy][ox + 2] = 0;
    }

    if (ox > 2 && f[oy][ox - 1] == 0 && f[oy][ox - 2] == 0) {
        f[oy][ox - 1] = 1;
        f[oy][ox - 2] = 1;

        if (oy >= 2 && f[oy][ox + 1] == 0 && f[oy - 1][ox + 1] == 0) {
            f[oy][ox + 1] = 1;
            f[oy - 1][ox + 1] = 1;

            ans += rec(1, 1);

            f[oy][ox + 1] = 0;
            f[oy - 1][ox + 1] = 0;
        }

        if (oy <= 2 && f[oy][ox + 1] == 0 && f[oy + 1][ox + 1] == 0) {
            f[oy][ox + 1] = 1;
            f[oy + 1][ox + 1] = 1;

            ans += rec(1, 1);

            f[oy][ox + 1] = 0;
            f[oy + 1][ox + 1] = 0;
        }

        f[oy][ox - 1] = 0;
        f[oy][ox - 2] = 0;
    }

    if (oy == 1 && f[2][ox] == 0 && f[3][ox] == 0 && f[1][ox - 1] == 0 && f[2][ox - 1] == 0 && f[1][ox + 1] == 0 && f[2][ox + 1] == 0) {
        f[2][ox] = 1;
        f[3][ox] = 1;
        f[1][ox - 1] = 1;
        f[2][ox - 1] = 1;
        f[1][ox + 1] = 1;
        f[2][ox + 1] = 1;

        ans += rec(1, 1);

        f[2][ox] = 0;
        f[3][ox] = 0;
        f[1][ox - 1] = 0;
        f[2][ox - 1] = 0;
        f[1][ox + 1] = 0;
        f[2][ox + 1] = 0;

    }

    if (oy == 3 && f[2][ox] == 0 && f[1][ox] == 0 && f[3][ox - 1] == 0 && f[2][ox - 1] == 0 && f[3][ox + 1] == 0 && f[2][ox + 1] == 0) {
        f[2][ox] = 1;
        f[1][ox] = 1;
        f[3][ox - 1] = 1;
        f[2][ox - 1] = 1;
        f[3][ox + 1] = 1;
        f[2][ox + 1] = 1;

        ans += rec(1, 1);

        f[2][ox] = 0;
        f[3][ox] = 0;
        f[3][ox - 1] = 0;
        f[2][ox - 1] = 0;
        f[3][ox + 1] = 0;
        f[2][ox + 1] = 0;

    }

    int x = ans % MOD;
    printf("%i\n", x);

    return 0;
}


#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
#include<algorithm>
#include<list>
#include<cstdio>
#include<stack>
#include<cstring>
#include <climits>
#include<cmath>
#include<string>
#include <functional>
#include<sstream>
#include<map>
using namespace std;
#define     For(i,a,b)        for (int i=a; i<b; i++)
#define     Rep(i,a)          for (int i=0; i<a; i++)
#define     FIT(it,v)         for (typeof((v).begin())it=(v).begin(); it!=(v).end(); ++it)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     GSORT(x)          sort(ALL(x), greater<typeof(*((x).begin()))>())
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     MP                make_pair
#define     SF                scanf
#define     PF                printf
#define     MAXN               10005
#define     MAXM               1001
#define     MOD               1000000007
#define     Dbug             cout<<"";
#define     hsh              2654435761 // %1<<x (x=60)
#define     EPS              1e-15
typedef long long ll;
typedef pair<int,int> pii;
ll n, dp[MAXN][1<<3][2];
string st[3];
ll rec(int p, int bit, int good)
{
	if(p==n)
	{
		if(bit==0) return good;
		return 0;
	}
	if(st[0][p]!='.') bit|=1;
	if(st[1][p]!='.') bit|=2;
	if(st[2][p]!='.') bit|=4;
	if(~dp[p][bit][good]) return dp[p][bit][good];
	ll ret=0;
	if(bit==7) ret=rec(p+1, 0, good);
	if(bit==0 && st[0][p+1]=='.' && st[1][p+1]=='.' && st[2][p+1]=='.')
	{
		if((p+2<n && (st[0][p+2]=='O' || st[2][p+2]=='O' || st[1][p+2]=='O'))||(p>0 && (st[0][p-1]=='O' || st[2][p-1]=='O' || st[1][p-1]=='O')))
			ret=(ret+rec(p+1, 7, 1))%MOD;
		else ret=(ret+rec(p+1, 7, good))%MOD;
	}
	if(bit==0 && st[0][p+1]=='.')
	{
		if((p+2<n && st[0][p+2]=='O')||(p>0 && st[0][p-1]=='O')) ret=(ret+rec(p+1, 1, 1))%MOD;
		else ret=(ret+rec(p+1, 1, good))%MOD;
	}
	if(bit==0 && st[2][p+1]=='.')
	{
		if((p+2<n && st[2][p+2]=='O')||(p>0 && st[2][p-1]=='O')) ret=(ret+rec(p+1, 4, 1))%MOD;
		else ret=(ret+rec(p+1, 4, good))%MOD;
	}
	if(bit==6 && st[0][p+1]=='.')
	{
		if((p+2<n && st[0][p+2]=='O')||(p>0 && st[0][p-1]=='O')) ret=(ret+rec(p+1, 1, 1))%MOD;
		else ret=(ret+rec(p+1, 1, good))%MOD;
	}
	if(bit==3 && st[2][p+1]=='.')
	{
		if((p+2<n && st[2][p+2]=='O') ||(p>0 && st[2][p-1]=='O')) ret=(ret+rec(p+1, 4, 1))%MOD;
		else ret=(ret+rec(p+1, 4, good))%MOD;
	}
	if(bit==5 && st[1][p+1]=='.')
	{
		if((p+2<n && st[1][p+2]=='O')||(p>0 && st[1][p-1]=='O')) ret=(ret+rec(p+1, 2, 1))%MOD;
		else ret=(ret+rec(p+1, 2, good))%MOD;
	}
	if(bit==1)
	{
		if(st[1][p+1]=='.' && st[2][p+1]=='.')
		{
			if((p+2<n && (st[1][p+2]=='O' || st[2][p+2]=='O'))||(p>0 && (st[1][p-1]=='O' || st[2][p-1]=='O')))
				ret=(ret+rec(p+1, 6, 1))%MOD;
			else ret=(ret+rec(p+1, 6, good))%MOD;
		}
		if(st[0][p]=='O') ret=(ret+rec(p+1, 0, 1))%MOD;
		else ret=(ret+rec(p+1, 0, good))%MOD;
	}
	if(bit==2 && st[0][p+1]=='.' && st[2][p+1]=='.')
	{
		if((p+2<n && (st[0][p+2]=='O' || st[2][p+2]=='O')) ||(p>0 && (st[0][p-1]=='O' || st[2][p-1]=='O'))) ret=(ret+rec(p+1, 5, 1))%MOD;
		else ret=(ret+rec(p+1, 5, good))%MOD;
	}
	if(bit==4)
	{
		if(st[1][p+1]=='.' && st[0][p+1]=='.')
		{
			if((p+2<n && (st[1][p+2]=='O' || st[0][p+2]=='O'))||(p>0 && (st[0][p-1]=='O' || st[1][p-1]=='O')))  ret=(ret+rec(p+1, 3, 1))%MOD;
			else ret=(ret+rec(p+1, 3, good))%MOD;
		}
		if(st[2][p]=='O') ret=(ret+rec(p+1, 0, 1))%MOD;
		else ret=(ret+rec(p+1, 0, good))%MOD;
	}
	return ret;
}
int main(){
	//ios_base::sync_with_stdio(false);
	//freopen("a.in","r",stdin);
	cin>>n;
	Set(dp, -1);
	cin>>st[0]>>st[1]>>st[2];
	int r, c;
	Rep(i, 3) Rep(j, n) if(st[i][j]=='O')
	{
		r=i, c=j;
		break;
	}
	ll ans=rec(0, 0, 0);
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
#include<algorithm>
#include<list>
#include<cstdio>
#include<stack>
#include<cstring>
#include <climits>
#include<cmath>
#include<string>
#include <functional>
#include<sstream>
#include<map>
using namespace std;
#define     For(i,a,b)        for (int i=a; i<b; i++)
#define     Rep(i,a)          for (int i=0; i<a; i++)
#define     FIT(it,v)         for (typeof((v).begin())it=(v).begin(); it!=(v).end(); ++it)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     GSORT(x)          sort(ALL(x), greater<typeof(*((x).begin()))>())
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     MP                make_pair
#define     SF                scanf
#define     PF                printf
#define     MAXN               10005
#define     MAXM               1001
#define     MOD               1000000007
#define     Dbug             cout<<"";
#define     hsh              2654435761 // %1<<x (x=60)
#define     EPS              1e-15
typedef long long ll;
typedef pair<int,int> pii;
ll n, dp[MAXN][1<<3][2];
string st[3];
ll rec(int p, int bit, int good)
{
	if(p==n)
	{
		if(bit==0) return good;
		return 0;
	}
	if(st[0][p]!='.') bit|=1;
	if(st[1][p]!='.') bit|=2;
	if(st[2][p]!='.') bit|=4;
	if(~dp[p][bit][good]) return dp[p][bit][good];
	ll ret=0;
	if(bit==7) ret=rec(p+1, 0, good);
	if(bit==0 && st[0][p+1]=='.' && st[1][p+1]=='.' && st[2][p+1]=='.')
	{
		if((p+2<n && (st[0][p+2]=='O' || st[2][p+2]=='O' || st[1][p+2]=='O'))||(p>0 && (st[0][p-1]=='O' || st[2][p-1]=='O' || st[1][p-1]=='O')))
			ret=(ret+rec(p+1, 7, 1))%MOD;
		else ret=(ret+rec(p+1, 7, good))%MOD;
	}
	if(bit==0 && st[0][p+1]=='.')
	{
		if((p+2<n && st[0][p+2]=='O')||(p>0 && st[0][p-1]=='O')) ret=(ret+rec(p+1, 1, 1))%MOD;
		else ret=(ret+rec(p+1, 1, good))%MOD;
	}
	if(bit==0 && st[2][p+1]=='.')
	{
		if((p+2<n && st[2][p+2]=='O')||(p>0 && st[2][p-1]=='O')) ret=(ret+rec(p+1, 4, 1))%MOD;
		else ret=(ret+rec(p+1, 4, good))%MOD;
	}
	if(bit==6 && st[0][p+1]=='.')
	{
		if((p+2<n && st[0][p+2]=='O')||(p>0 && st[0][p-1]=='O')) ret=(ret+rec(p+1, 1, 1))%MOD;
		else ret=(ret+rec(p+1, 1, good))%MOD;
	}
	if(bit==3 && st[2][p+1]=='.')
	{
		if((p+2<n && st[2][p+2]=='O') ||(p>0 && st[2][p-1]=='O')) ret=(ret+rec(p+1, 4, 1))%MOD;
		else ret=(ret+rec(p+1, 4, good))%MOD;
	}
	if(bit==5 && st[1][p+1]=='.')
	{
		if((p+2<n && st[1][p+2]=='O')||(p>0 && st[1][p-1]=='O')) ret=(ret+rec(p+1, 2, 1))%MOD;
		else ret=(ret+rec(p+1, 2, good))%MOD;
	}
	if(bit==1)
	{
		if(st[1][p+1]=='.' && st[2][p+1]=='.')
		{
			if((p+2<n && (st[1][p+2]=='O' || st[2][p+2]=='O'))||(p>0 && (st[1][p-1]=='O' || st[2][p-1]=='O')))
				ret=(ret+rec(p+1, 6, 1))%MOD;
			else ret=(ret+rec(p+1, 6, good))%MOD;
		}
		if(st[0][p]=='O') ret=(ret+rec(p+1, 0, 1))%MOD;
		else ret=(ret+rec(p+1, 0, good))%MOD;
	}
	if(bit==2 && st[0][p+1]=='.' && st[2][p+1]=='.')
	{
		if((p+2<n && (st[0][p+2]=='O' || st[2][p+2]=='O')) ||(p>0 && (st[0][p-1]=='O' || st[2][p-1]=='O'))) ret=(ret+rec(p+1, 5, 1))%MOD;
		else ret=(ret+rec(p+1, 5, good))%MOD;
	}
	if(bit==4)
	{
		if(st[1][p+1]=='.' && st[0][p+1]=='.')
		{
			if((p+2<n && (st[1][p+2]=='O' || st[0][p+2]=='O'))||(p>0 && (st[0][p-1]=='O' || st[1][p-1]=='O')))  ret=(ret+rec(p+1, 3, 1))%MOD;
			else ret=(ret+rec(p+1, 3, good))%MOD;
		}
		if(st[2][p]=='O') ret=(ret+rec(p+1, 0, 1))%MOD;
		else ret=(ret+rec(p+1, 0, good))%MOD;
	}
	return dp[p][bit][good]=ret;
}
int main(){
	//ios_base::sync_with_stdio(false);
	//freopen("a.in","r",stdin);
	cin>>n;
	Set(dp, -1);
	cin>>st[0]>>st[1]>>st[2];
	int r, c;
	Rep(i, 3) Rep(j, n) if(st[i][j]=='O')
	{
		r=i, c=j;
		break;
	}
	ll ans=rec(0, 0, 0);
	cout<<ans<<endl;
	return 0;
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(int arr[],int l1,int r1,int l2,int r2) {
        int tmp[]=new int[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(long arr[],int l1,int r1,int l2,int r2) {
        long tmp[]=new long[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int n=input.scanInt();
        int arr[]=new int[n];
        int brr[]=new int[n];
        for(int i=0;i<n;i++) {
            arr[i]=input.scanInt();
        }
        for(int i=0;i<n;i++) {
            brr[i]=input.scanInt();
        }
        coord_comp(n,arr,brr);
        ArrayList<Integer> arrli[]=new ArrayList[2*n];
        for(int i=0;i<arrli.length;i++) {
            arrli[i]=new ArrayList<>();
        }
        for(int i=0;i<n;i++) {
            arrli[brr[i]].add(i);
        }

        int arr_lft[]=lft_greater_eq(arr,n);
        int arr_rgt[]=rgt_greater(arr,n);
        int brr_lft[]=lft_smaller(brr,n);
        int brr_rgt[]=rgt_smaller(brr,n);

//        for(int i=0;i<n;i++) {
//            System.out.print(arr_lft[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(arr_rgt[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(brr_lft[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(brr_rgt[i]+" ");
//        }
//        System.out.println();
        long ans=0;
        for(int i=0;i<n;i++) {
            int a_lft=arr_lft[i];
            int a_rgt=arr_rgt[i];
            int indx=jlt(arrli[arr[i]],i);
            if(indx==-1) {
                indx=jgt(arrli[arr[i]],i);
            }
            if(indx==-1) {
                continue;
            }
            int b_lft=brr_lft[indx];
            int b_rgt=brr_rgt[indx];
            if(i<b_lft || i>b_rgt) {
                indx=jgt(arrli[arr[i]],i);
                if(indx==-1) {
                    continue;
                }
                b_lft=brr_lft[indx];
                b_rgt=brr_rgt[indx];
            }
            if(i<b_lft || i>b_rgt) {
                continue;
            }
            int lft=Math.max(a_lft,b_lft);
            int rgt=Math.min(a_rgt,b_rgt);
//            System.out.println(i);

            ans+=(long)(i-lft)*(long)(rgt-i);
            int l_indx=jlt(arrli[arr[i]],i);
            int r_indx=jgt(arrli[arr[i]],i);
            if(r_indx==-1) {
                r_indx=n;
            }
//            System.out.println(l_indx+" "+r_indx);
            l_indx=Math.max(l_indx,lft);
            r_indx=Math.min(r_indx,rgt);
            ans-=(long)(i-l_indx)*(long)(r_indx-i);
//            System.out.println(indx+" "+a_lft+" "+a_rgt+" "+b_lft+" "+b_rgt+" "+ans);
        }
        System.out.println(ans);
    }

    static int jlt(ArrayList<Integer> arr, int target)
    {
        int start = 0, end = arr.size()-1;

        int ans = -1;
        while (start <= end) {
            int mid = (start + end) / 2;

            if (arr.get(mid) > target) {
                end = mid - 1;
            }

            // Move right side
            else {
                ans = mid;
                start = mid + 1;
            }
        }
        if(ans==-1) {
            return ans;
        }
        return arr.get(ans);
    }

    static int jgt(ArrayList<Integer> arr, int target)
    {
        int start = 0, end = arr.size()- 1;

        int ans = -1;
        while (start <= end) {
            int mid = (start + end) / 2;

            // Move to right side if target is
            // greater.
            if (arr.get(mid) < target) {
                start = mid + 1;
            }

            // Move left side.
            else {
                ans = mid;
                end = mid - 1;
            }
        }
        if(ans==-1) {
            return ans;
        }
        return arr.get(ans);
    }

    static int[] lft_greater_eq(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, -1);

        for (int i = 0; i < n; i++) {

            while (!S.empty() && arr[S.peek()] < arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] rgt_greater(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, n);

        for (int i = n-1; i >=0 ; i--) {

            while (!S.empty() && arr[S.peek()] <= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] lft_smaller(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, -1);

        for (int i = 0; i < n; i++) {

            while (!S.empty() && arr[S.peek()] >= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] rgt_smaller(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, n);

        for (int i = n-1; i >=0 ; i--) {

            while (!S.empty() && arr[S.peek()] >= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    public static void coord_comp(int n,int arr[],int brr[]) {
        int crr[]=new int[2*n];
        for(int i=0;i<n;i++) {
            crr[i]=arr[i];
        }
        for(int i=0;i<n;i++) {
            crr[n+i]=brr[i];
        }
        sort(crr,0,2*n-1);
        HashMap<Integer,Integer> map=new HashMap<>();
        int cnt=0;
        for(int i=0;i<crr.length;i++) {
            if(!map.containsKey(crr[i])) {
                map.put(crr[i], cnt);
                cnt++;
            }
        }
        for(int i=0;i<n;i++) {
            arr[i]=map.get(arr[i]);
            brr[i]=map.get(brr[i]);
        }
    }
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(int arr[],int l1,int r1,int l2,int r2) {
        int tmp[]=new int[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(long arr[],int l1,int r1,int l2,int r2) {
        long tmp[]=new long[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int n=input.scanInt();
        int arr[]=new int[n];
        int brr[]=new int[n];
        for(int i=0;i<n;i++) {
            arr[i]=input.scanInt();
        }
        for(int i=0;i<n;i++) {
            brr[i]=input.scanInt();
        }
        coord_comp(n,arr,brr);
        ArrayList<Integer> arrli[]=new ArrayList[n];
        for(int i=0;i<n;i++) {
            arrli[i]=new ArrayList<>();
        }
        for(int i=0;i<n;i++) {
            arrli[brr[i]].add(i);
        }

        int arr_lft[]=lft_greater_eq(arr,n);
        int arr_rgt[]=rgt_greater(arr,n);
        int brr_lft[]=lft_smaller(brr,n);
        int brr_rgt[]=rgt_smaller(brr,n);

//        for(int i=0;i<n;i++) {
//            System.out.print(arr_lft[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(arr_rgt[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(brr_lft[i]+" ");
//        }
//        System.out.println();
//        for(int i=0;i<n;i++) {
//            System.out.print(brr_rgt[i]+" ");
//        }
//        System.out.println();
        long ans=0;
        for(int i=0;i<n;i++) {
            int a_lft=arr_lft[i];
            int a_rgt=arr_rgt[i];
            int indx=jlt(arrli[arr[i]],i);
            if(indx==-1) {
                indx=jgt(arrli[arr[i]],i);
            }
            if(indx==-1) {
                continue;
            }
            int b_lft=brr_lft[indx];
            int b_rgt=brr_rgt[indx];
            if(i<b_lft || i>b_rgt) {
                indx=jgt(arrli[arr[i]],i);
                if(indx==-1) {
                    continue;
                }
                b_lft=brr_lft[indx];
                b_rgt=brr_rgt[indx];
            }
            if(i<b_lft || i>b_rgt) {
                continue;
            }
            int lft=Math.max(a_lft,b_lft);
            int rgt=Math.min(a_rgt,b_rgt);
//            System.out.println(i);

            ans+=(long)(i-lft)*(long)(rgt-i);
            int l_indx=jlt(arrli[arr[i]],i);
            int r_indx=jgt(arrli[arr[i]],i);
            if(r_indx==-1) {
                r_indx=n;
            }
//            System.out.println(l_indx+" "+r_indx);
            l_indx=Math.max(l_indx,lft);
            r_indx=Math.min(r_indx,rgt);
            ans-=(long)(i-l_indx)*(long)(r_indx-i);
//            System.out.println(indx+" "+a_lft+" "+a_rgt+" "+b_lft+" "+b_rgt+" "+ans);
        }
        System.out.println(ans);
    }

    static int jlt(ArrayList<Integer> arr, int target)
    {
        int start = 0, end = arr.size()-1;

        int ans = -1;
        while (start <= end) {
            int mid = (start + end) / 2;

            if (arr.get(mid) > target) {
                end = mid - 1;
            }

            // Move right side
            else {
                ans = mid;
                start = mid + 1;
            }
        }
        if(ans==-1) {
            return ans;
        }
        return arr.get(ans);
    }

    static int jgt(ArrayList<Integer> arr, int target)
    {
        int start = 0, end = arr.size()- 1;

        int ans = -1;
        while (start <= end) {
            int mid = (start + end) / 2;

            // Move to right side if target is
            // greater.
            if (arr.get(mid) < target) {
                start = mid + 1;
            }

            // Move left side.
            else {
                ans = mid;
                end = mid - 1;
            }
        }
        if(ans==-1) {
            return ans;
        }
        return arr.get(ans);
    }

    static int[] lft_greater_eq(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, -1);

        for (int i = 0; i < n; i++) {

            while (!S.empty() && arr[S.peek()] < arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] rgt_greater(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, n);

        for (int i = n-1; i >=0 ; i--) {

            while (!S.empty() && arr[S.peek()] <= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] lft_smaller(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, -1);

        for (int i = 0; i < n; i++) {

            while (!S.empty() && arr[S.peek()] >= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    static int[] rgt_smaller(int arr[], int n) {

        Stack<Integer> S = new Stack<>();
        int tmp[]=new int[n];
        Arrays.fill(tmp, n);

        for (int i = n-1; i >=0 ; i--) {

            while (!S.empty() && arr[S.peek()] >= arr[i]) {
                S.pop();
            }

            if (!S.empty()) {
                tmp[i]=S.peek();
            }

            S.push(i);
        }
        return tmp;
    }

    public static void coord_comp(int n,int arr[],int brr[]) {
        int crr[]=new int[2*n];
        for(int i=0;i<n;i++) {
            crr[i]=arr[i];
        }
        for(int i=0;i<n;i++) {
            crr[n+i]=brr[i];
        }
        sort(crr,0,2*n-1);
        HashMap<Integer,Integer> map=new HashMap<>();
        int cnt=0;
        for(int i=0;i<crr.length;i++) {
            if(!map.containsKey(crr[i])) {
                map.put(crr[i], cnt);
                cnt++;
            }
        }
        for(int i=0;i<n;i++) {
            arr[i]=map.get(arr[i]);
            brr[i]=map.get(brr[i]);
        }
    }
}


import java.io.*;
import java.util.StringTokenizer;

public class Main {

    private static int n;
    private static int[] a, b;
    private static Node[] nodes;

    public static void main(String[] args) throws IOException {
        Reader in = new Reader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        n = in.nextInt();
        a = new int[n];
        b = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i] = in.nextInt();
        }

        nodes = new Node[4 * n + 4];
        build(1, 0, n - 1);

        long ans = 0;

        for (int now = 0; now < n; now++) {
            Node leftAns = findMin(now);
            Node rightAns = findMax(now);
            if (leftAns != null && rightAns != null) {
                ans += rightAns.right - leftAns.right + 1;
            }
        }

        out.println(ans);

        out.flush();
        in.close();
        out.close();
    }

    private static Node findMax(int now) {
        int left = now;
        int right = n - 1;
        while (left < right) {
            int mid = (left + right + 1) >> 1;
            Node node = query(1, now, mid);
            if (node.aMax > node.bMin) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        Node ans = query(1, now, left);
        return ans.aMax == ans.bMin ? ans : null;
    }

    private static Node findMin(int now) {
        int left = now;
        int right = n - 1;
        while (left < right) {
            int mid = (left + right) >> 1;
            Node node = query(1, now, mid);
            if (node.aMax < node.bMin) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        Node ans = query(1, now, left);
        return ans.aMax == ans.bMin ? ans : null;
    }

    private static Node query(int p, int left, int right) {
        if (left == nodes[p].left && right == nodes[p].right) {
            return nodes[p];
        }

        int mid = (nodes[p].left + nodes[p].right) >> 1;
        if (right <= mid) {
            return query(p * 2, left, right);
        } else if (left > mid) {
            return query(p * 2 + 1, left, right);
        } else {
            Node leftAns = query(p * 2, left, mid);
            Node rightAns = query(p * 2 + 1, mid + 1, right);
            Node ans = new Node();
            ans.left = left;
            ans.right = right;
            ans.aMax = Math.max(leftAns.aMax, rightAns.aMax);
            ans.bMin = Math.min(leftAns.bMin, rightAns.bMin);
            return ans;
        }
    }

    private static void build(int p, int left, int right) {
        nodes[p] = new Node();
        nodes[p].left = left;
        nodes[p].right = right;
        if (left == right) {
            nodes[p].aMax = a[left];
            nodes[p].bMin = b[left];
            return;
        }

        int mid = (left + right) >> 1;
        build(p * 2, left, mid);
        build(p * 2 + 1, mid + 1, right);

        nodes[p].aMax = Math.max(nodes[p * 2].aMax, nodes[p * 2 + 1].aMax);
        nodes[p].bMin = Math.min(nodes[p * 2].bMin, nodes[p * 2 + 1].bMin);
    }

    private static class Node {
        private int aMax, bMin, left, right;
    }

    static class Reader {
        BufferedReader reader;
        StringTokenizer st;

        Reader(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            st = null;
        }

        void close() throws IOException {
            reader.close();
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        String nextLine() throws IOException {
            return reader.readLine();
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

    }
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

// 4:46.848 + 19:02.861

public class cf689d {

    public static void main(String[] args) throws IOException {
        int n = ri(), a[] = ria(n), b[] = ria(n);
        int al[] = new int[n], ar[] = new int[n], alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0;
        long ans = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] >= a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        sptb rmq = new sptb(b, Integer::min);
        for (int i = 0; i < n; ++i) {
            if (rmq.qry(al[i], ar[i] + 1) <= a[i]) {
                int lb = i, rb = i;
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) >= a[i]) {
                        lb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                for (int l = i, r = ar[i]; l <=r; ){
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) >= a[i]) {
                        rb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                int lrb = al[i], rlb = ar[i];
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) <= a[i]) {
                        lrb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                for (int l = i, r = ar[i]; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) <= a[i]) {
                        rlb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                if (rmq.qry(lb, rb + 1) == a[i]) {
                    // prln((long) (i - lb + 1) * (rb - i + 1), (long) (i - lrb) * (rlb - i));
                    ans += max(0, (long) (i - lb + 1) * (rb - i + 1) - (long) (i - lrb) * (rlb - i));
                }
            }
        }
        prln(ans);

        /* int al[] = new int[n], ar[] = new int[n], bl[] = new int[n], br[] = new int[n];
        int alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0, blq[] = new int[n], blp = 0, brq[] = new int[n], brp = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] > a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;

            while (blp > 0 && b[n - i - 1] < b[blq[blp - 1]]) {
                bl[blq[--blp]] = n - i;
            }
            blq[blp++] = n - i - 1;

            while (brp > 0 && b[i] < b[brq[brp - 1]]) {
                br[brq[--brp]] = i - 1;
            }
            brq[brp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        while (blp > 0) {
            bl[blq[--blp]] = 0;
        }
        while (brp > 0) {
            br[brq[--brp]] = n - 1;
        }
        prln(al);
        prln(ar);
        prln(bl);
        prln(br); */
        close();
    }

    @FunctionalInterface
    interface IntOperator {
        int merge(int a, int b);
    }

    // cannot use with summation
    static class sptb {
        IntOperator op;
        int n, lg[], lookup[][];

        sptb(int[] a, IntOperator operator) {
            n = a.length;
            lg = new int[n + 1];
            int pow2 = 4;
            for (int i = 2, ind = 1; i <= n; ++i) {
                if (pow2 == i) {
                    ++ind;
                    pow2 <<= 1;
                }
                lg[i] = ind;
            }
            op = operator;
            lookup = new int[n][lg[n] + 1];
            for (int i = 0; i < n; ++i) {
                lookup[i][0] = a[i];
            }
            for (int j = 1; (1 << j) <= n; ++j) {
                for (int i = 0; (i + (1 << j)) <= n; ++i) {
                    lookup[i][j] = op.merge(lookup[i][j - 1], lookup[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int qry(int l, int r) {
            int j = lg[r - l];
            return op.merge(lookup[l][j], lookup[r - (1 << j)][j]);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int) d;}
    static int cei(double d) {return (int) ceil(d);}
    static long fll(double d) {return (long) d;}
    static long cel(double d) {return (long) ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static void rsort(double[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // input
    static void r() throws IOException {input = new StringTokenizer(rline());}
    static int ri() throws IOException {return Integer.parseInt(rline());}
    static long rl() throws IOException {return Long.parseLong(rline());}
    static double rd() throws IOException {return Double.parseDouble(rline());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
    static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
    static char[] rcha() throws IOException {return rline().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static String n() {return input.nextToken();}
    static int rni() throws IOException {r(); return ni();}
    static int ni() {return Integer.parseInt(n());}
    static long rnl() throws IOException {r(); return nl();}
    static long nl() {return Long.parseLong(n());}
    static double rnd() throws IOException {r(); return nd();}
    static double nd() {return Double.parseDouble(n());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {prln("yes");}
    static void pry() {prln("Yes");}
    static void prY() {prln("YES");}
    static void prno() {prln("no");}
    static void prn() {prln("No");}
    static void prN() {prln("NO");}
    static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
    static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
    static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
    static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
    static void h() {prln("hlfd"); flush();}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

// 4:46.848 + 19:02.861

public class cf689d {

    public static void main(String[] args) throws IOException {
        int n = ri(), a[] = ria(n), b[] = ria(n);
        int al[] = new int[n], ar[] = new int[n], alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0;
        long ans = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] >= a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        sptb rmq = new sptb(b, Integer::min);
        for (int i = 0; i < n; ++i) {
            if (rmq.qry(al[i], ar[i] + 1) <= a[i]) {
                int lb = i, rb = i;
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) >= a[i]) {
                        lb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                for (int l = i, r = ar[i]; l <=r; ){
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) >= a[i]) {
                        rb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                if (rmq.qry(lb, rb + 1) == a[i]) {
                    ans += (long) (i - lb + 1) * (rb - i + 1);
                }
            }
        }
        prln(ans);

        /* int al[] = new int[n], ar[] = new int[n], bl[] = new int[n], br[] = new int[n];
        int alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0, blq[] = new int[n], blp = 0, brq[] = new int[n], brp = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] > a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;

            while (blp > 0 && b[n - i - 1] < b[blq[blp - 1]]) {
                bl[blq[--blp]] = n - i;
            }
            blq[blp++] = n - i - 1;

            while (brp > 0 && b[i] < b[brq[brp - 1]]) {
                br[brq[--brp]] = i - 1;
            }
            brq[brp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        while (blp > 0) {
            bl[blq[--blp]] = 0;
        }
        while (brp > 0) {
            br[brq[--brp]] = n - 1;
        }
        prln(al);
        prln(ar);
        prln(bl);
        prln(br); */
        close();
    }

    @FunctionalInterface
    interface IntOperator {
        int merge(int a, int b);
    }

    // cannot use with summation
    static class sptb {
        IntOperator op;
        int n, lg[], lookup[][];

        sptb(int[] a, IntOperator operator) {
            n = a.length;
            lg = new int[n + 1];
            int pow2 = 4;
            for (int i = 2, ind = 1; i <= n; ++i) {
                if (pow2 == i) {
                    ++ind;
                    pow2 <<= 1;
                }
                lg[i] = ind;
            }
            op = operator;
            lookup = new int[n][lg[n] + 1];
            for (int i = 0; i < n; ++i) {
                lookup[i][0] = a[i];
            }
            for (int j = 1; (1 << j) <= n; ++j) {
                for (int i = 0; (i + (1 << j)) <= n; ++i) {
                    lookup[i][j] = op.merge(lookup[i][j - 1], lookup[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int qry(int l, int r) {
            int j = lg[r - l];
            return op.merge(lookup[l][j], lookup[r - (1 << j)][j]);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int) d;}
    static int cei(double d) {return (int) ceil(d);}
    static long fll(double d) {return (long) d;}
    static long cel(double d) {return (long) ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static void rsort(double[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // input
    static void r() throws IOException {input = new StringTokenizer(rline());}
    static int ri() throws IOException {return Integer.parseInt(rline());}
    static long rl() throws IOException {return Long.parseLong(rline());}
    static double rd() throws IOException {return Double.parseDouble(rline());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
    static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
    static char[] rcha() throws IOException {return rline().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static String n() {return input.nextToken();}
    static int rni() throws IOException {r(); return ni();}
    static int ni() {return Integer.parseInt(n());}
    static long rnl() throws IOException {r(); return nl();}
    static long nl() {return Long.parseLong(n());}
    static double rnd() throws IOException {r(); return nd();}
    static double nd() {return Double.parseDouble(n());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {prln("yes");}
    static void pry() {prln("Yes");}
    static void prY() {prln("YES");}
    static void prno() {prln("no");}
    static void prn() {prln("No");}
    static void prN() {prln("NO");}
    static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
    static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
    static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
    static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
    static void h() {prln("hlfd"); flush();}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

// 4:46.848 + 19:02.861

public class cf689d {

    public static void main(String[] args) throws IOException {
        int n = ri(), a[] = ria(n), b[] = ria(n);
        int al[] = new int[n], ar[] = new int[n], alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0;
        long ans = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] >= a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        sptb rmq = new sptb(b, Integer::min);
        for (int i = 0; i < n; ++i) {
            if (rmq.qry(al[i], ar[i] + 1) <= a[i]) {
                int lb = i, rb = i;
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) >= a[i]) {
                        lb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                for (int l = i, r = ar[i]; l <=r; ){
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) >= a[i]) {
                        rb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                int lrb = al[i], rlb = ar[i];
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) <= a[i]) {
                        lrb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                for (int l = i, r = ar[i]; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) <= a[i]) {
                        rlb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                if (rmq.qry(lb, rb + 1) == a[i]) {
                    ans += max(0, (long) (i - lb + 1) * (rb - i + 1) - (long) (i - lrb + 1) * (rlb - i + 1));
                }
            }
        }
        prln(ans);

        /* int al[] = new int[n], ar[] = new int[n], bl[] = new int[n], br[] = new int[n];
        int alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0, blq[] = new int[n], blp = 0, brq[] = new int[n], brp = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] > a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;

            while (blp > 0 && b[n - i - 1] < b[blq[blp - 1]]) {
                bl[blq[--blp]] = n - i;
            }
            blq[blp++] = n - i - 1;

            while (brp > 0 && b[i] < b[brq[brp - 1]]) {
                br[brq[--brp]] = i - 1;
            }
            brq[brp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        while (blp > 0) {
            bl[blq[--blp]] = 0;
        }
        while (brp > 0) {
            br[brq[--brp]] = n - 1;
        }
        prln(al);
        prln(ar);
        prln(bl);
        prln(br); */
        close();
    }

    @FunctionalInterface
    interface IntOperator {
        int merge(int a, int b);
    }

    // cannot use with summation
    static class sptb {
        IntOperator op;
        int n, lg[], lookup[][];

        sptb(int[] a, IntOperator operator) {
            n = a.length;
            lg = new int[n + 1];
            int pow2 = 4;
            for (int i = 2, ind = 1; i <= n; ++i) {
                if (pow2 == i) {
                    ++ind;
                    pow2 <<= 1;
                }
                lg[i] = ind;
            }
            op = operator;
            lookup = new int[n][lg[n] + 1];
            for (int i = 0; i < n; ++i) {
                lookup[i][0] = a[i];
            }
            for (int j = 1; (1 << j) <= n; ++j) {
                for (int i = 0; (i + (1 << j)) <= n; ++i) {
                    lookup[i][j] = op.merge(lookup[i][j - 1], lookup[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int qry(int l, int r) {
            int j = lg[r - l];
            return op.merge(lookup[l][j], lookup[r - (1 << j)][j]);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int) d;}
    static int cei(double d) {return (int) ceil(d);}
    static long fll(double d) {return (long) d;}
    static long cel(double d) {return (long) ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static void rsort(double[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // input
    static void r() throws IOException {input = new StringTokenizer(rline());}
    static int ri() throws IOException {return Integer.parseInt(rline());}
    static long rl() throws IOException {return Long.parseLong(rline());}
    static double rd() throws IOException {return Double.parseDouble(rline());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
    static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
    static char[] rcha() throws IOException {return rline().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static String n() {return input.nextToken();}
    static int rni() throws IOException {r(); return ni();}
    static int ni() {return Integer.parseInt(n());}
    static long rnl() throws IOException {r(); return nl();}
    static long nl() {return Long.parseLong(n());}
    static double rnd() throws IOException {r(); return nd();}
    static double nd() {return Double.parseDouble(n());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {prln("yes");}
    static void pry() {prln("Yes");}
    static void prY() {prln("YES");}
    static void prno() {prln("no");}
    static void prn() {prln("No");}
    static void prN() {prln("NO");}
    static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
    static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
    static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
    static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
    static void h() {prln("hlfd"); flush();}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

// 4:46.848 + 19:02.861

public class cf689d {

    public static void main(String[] args) throws IOException {
        int n = ri(), a[] = ria(n), b[] = ria(n);
        int al[] = new int[n], ar[] = new int[n], alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0;
        long ans = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] >= a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        sptb rmq = new sptb(b, Integer::min);
        for (int i = 0; i < n; ++i) {
            if (rmq.qry(al[i], ar[i] + 1) <= a[i]) {
                int lb = i, rb = i;
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) >= a[i]) {
                        lb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                for (int l = i, r = ar[i]; l <=r; ){
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) >= a[i]) {
                        rb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                int lrb = al[i] - 1, rlb = ar[i] + 1;
                for (int l = al[i], r = i; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(m, i + 1) <= a[i]) {
                        lrb = m;
                        l = m + 1;
                    } else {
                        r = m - 1;
                    }
                }
                for (int l = i, r = ar[i]; l <= r; ) {
                    int m = l + (r - l) / 2;
                    if (rmq.qry(i, m + 1) <= a[i]) {
                        rlb = m;
                        r = m - 1;
                    } else {
                        l = m + 1;
                    }
                }
                if (rmq.qry(lb, rb + 1) == a[i]) {
                    // prln(lrb, rlb);
                    // prln((long) (i - lb + 1) * (rb - i + 1), (long) (i - lrb) * (rlb - i));
                    ans += (long) (i - lb + 1) * (rb - i + 1) - (long) (i - lrb) * (rlb - i);
                }
            }
        }
        prln(ans);

        /* int al[] = new int[n], ar[] = new int[n], bl[] = new int[n], br[] = new int[n];
        int alq[] = new int[n], alp = 0, arq[] = new int[n], arp = 0, blq[] = new int[n], blp = 0, brq[] = new int[n], brp = 0;
        for (int i = 0; i < n; ++i) {
            while (alp > 0 && a[n - i - 1] > a[alq[alp - 1]]) {
                al[alq[--alp]] = n - i;
            }
            alq[alp++] = n - i - 1;

            while (arp > 0 && a[i] > a[arq[arp - 1]]) {
                ar[arq[--arp]] = i - 1;
            }
            arq[arp++] = i;

            while (blp > 0 && b[n - i - 1] < b[blq[blp - 1]]) {
                bl[blq[--blp]] = n - i;
            }
            blq[blp++] = n - i - 1;

            while (brp > 0 && b[i] < b[brq[brp - 1]]) {
                br[brq[--brp]] = i - 1;
            }
            brq[brp++] = i;
        }
        while (alp > 0) {
            al[alq[--alp]] = 0;
        }
        while (arp > 0) {
            ar[arq[--arp]] = n - 1;
        }
        while (blp > 0) {
            bl[blq[--blp]] = 0;
        }
        while (brp > 0) {
            br[brq[--brp]] = n - 1;
        }
        prln(al);
        prln(ar);
        prln(bl);
        prln(br); */
        close();
    }

    @FunctionalInterface
    interface IntOperator {
        int merge(int a, int b);
    }

    // cannot use with summation
    static class sptb {
        IntOperator op;
        int n, lg[], lookup[][];

        sptb(int[] a, IntOperator operator) {
            n = a.length;
            lg = new int[n + 1];
            int pow2 = 4;
            for (int i = 2, ind = 1; i <= n; ++i) {
                if (pow2 == i) {
                    ++ind;
                    pow2 <<= 1;
                }
                lg[i] = ind;
            }
            op = operator;
            lookup = new int[n][lg[n] + 1];
            for (int i = 0; i < n; ++i) {
                lookup[i][0] = a[i];
            }
            for (int j = 1; (1 << j) <= n; ++j) {
                for (int i = 0; (i + (1 << j)) <= n; ++i) {
                    lookup[i][j] = op.merge(lookup[i][j - 1], lookup[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int qry(int l, int r) {
            int j = lg[r - l];
            return op.merge(lookup[l][j], lookup[r - (1 << j)][j]);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int) d;}
    static int cei(double d) {return (int) ceil(d);}
    static long fll(double d) {return (long) d;}
    static long cel(double d) {return (long) ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static void rsort(double[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // input
    static void r() throws IOException {input = new StringTokenizer(rline());}
    static int ri() throws IOException {return Integer.parseInt(rline());}
    static long rl() throws IOException {return Long.parseLong(rline());}
    static double rd() throws IOException {return Double.parseDouble(rline());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
    static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
    static char[] rcha() throws IOException {return rline().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static String n() {return input.nextToken();}
    static int rni() throws IOException {r(); return ni();}
    static int ni() {return Integer.parseInt(n());}
    static long rnl() throws IOException {r(); return nl();}
    static long nl() {return Long.parseLong(n());}
    static double rnd() throws IOException {r(); return nd();}
    static double nd() {return Double.parseDouble(n());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {prln("yes");}
    static void pry() {prln("Yes");}
    static void prY() {prln("YES");}
    static void prno() {prln("no");}
    static void prn() {prln("No");}
    static void prN() {prln("NO");}
    static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
    static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
    static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
    static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
    static void h() {prln("hlfd"); flush();}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int a[MAX],b[MAX];
struct lenka
{
    int l,r;
    int ma,mi;
}A[MAX<<2];
void build(int k,int l,int r)
{
    A[k].l=l,A[k].r=r;
    A[k].ma=a[r];
    A[k].mi=b[r];
    if(l==r)return;
    build(2*k,l,(l+r)/2);
    build(2*k+1,(l+r)/2+1,r);
    A[k].ma=max(A[2*k].ma,A[2*k+1].ma);
    A[k].mi=min(A[2*k].mi,A[2*k+1].mi);
}
int askma(int k,int x,int y)
{
    if(x==A[k].l&&y==A[k].r)return A[k].ma;
    if(y<=A[2*k].r)return askma(2*k,x,y);
    if(x>=A[2*k+1].l)return askma(2*k+1,x,y);
    return max(askma(2*k,x,A[2*k].r),askma(2*k+1,A[2*k+1].l,y));
}
int askmi(int k,int x,int y)
{
    if(x==A[k].l&&y==A[k].r)return A[k].mi;
    if(y<=A[2*k].r)return askmi(2*k,x,y);
    if(x>=A[2*k+1].l)return askmi(2*k+1,x,y);
    return min(askmi(2*k,x,A[2*k].r),askmi(2*k+1,A[2*k+1].l,y));
}
int ask(int x,int y){return askma(1,x,y)-askmi(1,x,y);}
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]);
    build(1,1,n);
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int l=i,r=n;
        int L=0,R=-1;
        while(r>=l)
        {
            int m=(l+r)/2;
            if(ask(i,m)>0)r=m-1;
            else if(ask(i,m)<0)l=m+1;
            else
            {
                r=m-1;
                L=m;
            }
        }
        l=i,r=n;
        while(r>=l)
        {
            int m=(l+r)/2;
            if(ask(i,m)>0)r=m-1;
            else if(ask(i,m)<0)l=m+1;
            else
            {
                l=m+1;
                R=m;
            }
        }
        ans+=R-L+1;
    }
    cout<<ans<<endl;
    return 0;
}