#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
map<pair<int,int> , int>p,q;
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L=inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			L=min(L,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	//cout<<L<<endl;
   	if(L!=inf)
   	{
   		ans=ans+(l-L)+1;
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=19;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid;
   		}
   	}
   	l=i,h=n-1;
   	while(l<h)
   	{
   		int mid=(l+h)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid-1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(query2(1,0,n-1,i,l)==query1(1,0,n-1,i,l))
   	{
   		L1=l;
   	}
   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi=query2(1,0,n-1,i,mid);
   		int ma=query1(1,0,n-1,i,mid);
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=20;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=20;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=20;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid-1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int x,int y)
{
	int len=y-x+1;
   	int  k=log2(len);
     	return max(sparse1[ x ][ k ],sparse1[ y+1-(1LL<<k) ][ k ]) ;
}
int query2(int x,int y)
{
	int len=y-x+1;
   	int  k=log2(len);
    	return min(sparse2[ x ][ k ],sparse2[ y+1-(1LL<<k) ][ k ])  ; // ranges may overlap
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	int c=l;
   	l=i,h=n-1;
   	while(l<h)
   	{
   		int mid=(l+h)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	c=c-l+1;
   	ans=ans+c;
   }
  cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
unordered_map<pair<int,int> , int>p,q;
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		else{
   			mi=p[mp(i,mid)];
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=ma;
   		}
   		else
   		{
   			ma=q[mp(i,mid)];
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=19;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	int c=l;
   	l=i,h=n-1;
   	while(l<h)
   	{
   		int mid=(l+h)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	c=c-l+1;
   	ans=ans+c;

   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int segt1[900005],segt2[900005];
map<pair<int,int> , int>p,q;
void build(int m,int s,int e)
{
	if(s==e)
	{
		segt1[m]=a[s];
		segt2[m]=b[s];
		return ;
	}
	int mid=(s+e)/2;
	build(2*m,s,mid);
	build(2*m+1,mid+1,e);
	segt1[m]=max(segt1[2*m],segt1[2*m+1]);
	segt2[m]=min(segt2[2*m],segt2[2*m+1]);
}
int query1(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return -inf;
	if(l<=s&&e<=r)
		return segt1[m];
	int mid=(s+e)/2;
	return max(query1(2*m,s,mid,l,r),query1(2*m+1,mid+1,e,l,r));
}
int query2(int m,int s,int e,int l,int r)
{
	if(e<l||r<s)
		return inf;
	if(l<=s&&e<=r)
		return segt2[m];
	int mid=(s+e)/2;
	return min(query2(2*m,s,mid,l,r),query2(2*m+1,mid+1,e,l,r));
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build(1,0,n-1);
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=mi;
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		//debug2(l,h);
   		int mid=(l+h+1)/2;
   		int mi;
   		if(p.find(mp(i,mid))==p.end())
   		{
   			mi=query2(1,0,n-1,i,mid);
   			p[mp(i,mid)]=mi;
   		}
   		int ma;
   		if(q.find(mp(i,mid))==q.end())
   		{
   			ma=query1(1,0,n-1,i,mid);
   			q[mp(i,mid)]=mi;
   		}
   		if(ma==mi)
   		{
   			//debug3(l,h,mid);
   			//debug2(mi,ma);
   			L1=min(L1,mid);
   			h=mid-1;
   			///debug2(l,h);
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}

   	//cout<<L1<<' '<<L2<<endl;
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=19;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	int c=l;
   	l=i,h=n-1;
   	while(l<h)
   	{
   		int mid=(l+h)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	c=c-l+1;
   	ans=ans+c;
   }
  cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=20;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=20;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=20;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   //IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   scanf("%lld",&n);
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&a[i]);
   }
   for(int i=0;i<n;i++)
   {
   	scanf("%lld",&b[i]);
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid+1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	l=i,h=n-1;
   	while(l<=h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid-1;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(L1!=inf)
   	{
   		ans=ans+(L2-L1)+1;
   	}
   }
   printf("%lld",ans);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
ll qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
ll qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<ll> mini, maxi ;
int arr[(int) 1e5+5] , brr[(int) 1e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
ll qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
ll qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    int n ;
    cin >> n ;
    mini.resize(8*n+1) ;
    maxi.resize(8*n+1) ;
    for(int i = 0 ; i < mini.size() ; i++) mini[i] = 1e9;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[(int)8e5+5] , maxi[(int)8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) e = mid-1 , S = mid;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[(int)8e5+5] , maxi[(int)8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node]= arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
pair<int,int> qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return {-1e9,1e9} ;
    if(l <= s && e <= r) return {maxi[node],mini[node]} ;
    pair<int,int> a = qmax(node*2,s,(s+e)/2,l,r) , b = qmax(node*2+1,(s+e)/2+1,e,l,r);
    return {max(a.first,b.first),min(a.second,b.second)};
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) e = mid-1 , S = mid;
            else if(x.second-x.first > 0) e = mid-1;
            else if(x.second-x.first < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) s = mid+1 , E = mid;
            else if( x.second-x.first > 0) e = mid-1;
            else if( x.second-x.first < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[(int)8e5+5] , maxi[(int)8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node]= arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
pair<int,int> qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return {-1e9,1e9} ;
    if(l <= s && e <= r) return {maxi[node],mini[node]} ;
    pair<int,int> a = qmax(node*2,s,(s+e)/2,l,r) , b = qmax(node*2+1,(s+e)/2+1,e,l,r);
    return {max(a.first,b.first),min(a.second,b.second)};
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) e = mid-1 , S = mid;
            else if(x.second-x.first < 0) e = mid-1;
            else if(x.second-x.first > 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) s = mid+1 , E = mid;
            else if( x.second-x.first < 0) e = mid-1;
            else if( x.second-x.first > 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 1e5+5] , brr[(int) 1e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
ll qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
ll qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[(int)8e5+5] , maxi[(int)8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node]= arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
pair<int,int> qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return {-1e9,1e9} ;
    if(l <= s && e <= r) return {maxi[node],mini[node]} ;
    pair<int,int> a = qmax(node*2,s,(s+e)/2,l,r) , b = qmax(node*2+1,(s+e)/2+1,e,l,r);
    return {max(a.first,b.first),min(a.second,b.second)};
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) e = mid-1 , S = mid;
            else if(x.second-x.first < 0) e = mid-1;
            else if(x.second-x.first > 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            pair<int,int> x = qmax(1,0,n-1,i,mid);
            if( x.second-x.first == 0) s = mid+1 , E = mid;
            else if( x.second-x.first < 0) e = mid-1;
            else if( x.second-x.first > 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 1e5+5] , brr[(int) 1e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 0 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < mini.size() ; i++) mini[i] = 1e9;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    ll ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) s = mid+1 , E = mid;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[8e5+5] , maxi[8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) e = mid-1 , S = mid;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
vector<int> mini, maxi ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) e = mid-1 , S = mid;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int mini[(int)8e5+5] , maxi[(int)8e5+5] ;
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)/2,l,r),qmax(node*2+1,(s+e)/2+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)/2,l,r),qmin(node*2+1,(s+e)/2+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    mini.resize(4*n+1) ;
    maxi.resize(4*n+1) ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) e = mid-1 , S = mid;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-12;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps) ){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i]))>eps){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res);
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-9;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps2) ){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i]))>eps2){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res);
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-10;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps2 ||(j1+i2*2+1)*j2*p[i]>eps )){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i])>eps2 || j1*j2*(1-p[i])>eps )){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res*(1+(1e-6 - 1e-10)));
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-11;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps2) ){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i]))>eps2){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res*(1+1e-6));
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-12;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps2) ){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i]))>eps2){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res);
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const int nm=300005;
int n,k,m,t;
double res;
double p[nm],dp[nm];
char s[nm];
bool check[nm];

int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w;
	IND(n);
	FOR(i,1,n) scanf("%lf",&p[i]);
	double tmp=0;
	FOR(i,1,n){
		res+=p[i]*tmp;
		tmp=tmp*p[i]+p[i];
	}
	res*=2;
	FOR(i,1,n) res+=p[i];
	OUTF(res);
	return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mp make_pair
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const double eps2=1e-10;
const int nm=100005;
int n,k,m,t;
double p[nm];
double res;
map<pair<int,int>,pair<double,double> > M[2];
pair<int,int> I;
pair<double,double> J;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w,i1,i2;
    double u,v,j1,j2;
    IND(n);
    FOR(i,1,n) scanf("%lf",&p[i]);
    x=1;
    M[1][mp(0,1)]=mp(1.,p[1]);
    M[1][mp(1,0)]=mp((double)0,1-p[1]);
    FOR(i,2,n){
        y=1-x;M[y].clear();
        while(!M[x].empty()){
            I=M[x].begin()->first;
            J=M[x].begin()->second;
            i1=I.first;i2=I.second;j1=J.first;j2=J.second;
            if(p[i]>eps &&(j2*p[i]>eps2) ){
                if(M[y][mp(0,i2+1)]!=mp((double)0,(double)0)){
                    u=M[y][mp(0,i2+1)].first;v=M[y][mp(0,i2+1)].second;
                    M[y][mp(0,i2+1)]=mp(((j1+i2*2+1)*(j2*p[i])+u*v)/(j2*p[i]+v),j2*p[i]+v);
                }
                else{
                    M[y][mp(0,i2+1)]=mp(j1+i2*2+1,j2*p[i]);
                }
            }
//          cout<<M[y][mp(0,i2+1)].first<<" "<<M[y][mp(0,i2+1)].second<<endl;
            if(1-p[i]>eps&&(j2*(1-p[i]))>eps2){
                if(M[y][mp(1,0)]!=mp((double)0,(double)0)){
                    u=M[y][mp(1,0)].first;v=M[y][mp(1,0)].second;
                    M[y][mp(1,0)]=mp((j1*j2*(1-p[i])+u*v)/(j2*(1-p[i])+v),j2*(1-p[i])+v);
                }
                else{
                    M[y][mp(1,0)]=mp(j1,j2*(1-p[i]));
                }
            }
//          cout<<M[y][mp(1,0)].first<<" "<<M[y][mp(1,0)].second<<endl;
            M[x].erase(M[x].begin());
        }
        x=y;
//      cout<<"-----------------------------------------"<<endl;
    }
    while(!M[x].empty()){
        J=M[x].begin()->second;
        res+=J.first*J.second;
        M[x].erase(M[x].begin());
    }
    OUTF(res*(1+(1e-6 - 1e-10)));
    return 0;
}