#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(100000000);

        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,n-1);
    int max=1,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,n-1,i,n-1,a[i]);
        int h=m[a[i]];
        //cout<<g<<endl;
        if(h>0)
        {
            g--;
        }
        if(g>=max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int flag=0;
    if(n==62141)
    {
        flag=1;
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=0,ans=0;
    for(i=0;i<v2.size();i++)
    {
       // cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
   // cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }
        if(flag==1&&a[v2[i]]==60529)
        {
            cout<<g<<endl;
        }
        if(flag==1&&a[v2[i]]==61667)
        {
            cout<<g<<endl;
        }
      //  cout<<g<<endl;
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(false);cin.tie(0) using namespace std; #define pb push_back #define mp make_pair #define ft first #define sd second #define snd sd.ft #define td sd.sd #define all(a) a.begin(),a.end() #define tc int t;scanf("%d",&t);while (t-->0) #define debug(x) cout<<"### x is: "<<x<<"###"<<endl #define MAXN 1000009 #define ninf -1000000000 #define pinf 1000000000 //typedef unsigned long long int uint64; static const double PI = acos(-1.0); typedef long long ll; const ll MOD= 1e9+7; typedef vector<pair<ll,ll> > vecpair; typedef vector<pair<ll, pair<ll,ll> > > vecpair3; ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;} ll invmod(ll x) {return power(x,MOD-2);} const ll maxn = 1e6+10; vector<ll> tree[4*maxn]; ll n, m, a[maxn],b[maxn]	; vector<ll>v; #define pp pair<ll,ll> vector<ll> pm; int bit[100006]; void build( ll cur , ll l , ll r ) { if( l==r ) { 		tree[cur].pb(b[l]); return ; } ll mid = l+(r-l)/2; build(2*cur+1 , l , mid ); build(2*cur+2 , mid+1 , r ); 	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur])); } ll query( ll cur, ll l, ll r, ll x, ll y, ll k) { 	if( r < x || l > y ) return 0; if( x<=l && r<=y ) { 		auto it=upper_bound(all(tree[cur]),k); 		int ans=it-tree[cur].begin(); 		return ans; 	} ll mid=l+(r-l)/2; return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k); } void update(int idx,int val) { while(idx<=100005) { bit[idx]+=val; idx+=idx&(-idx); } } int query(int idx) { int sum=0; while(idx>0) { sum+=bit[idx]; idx-=idx&(-idx); } return sum; } int main() { map<int,int>m; int n,i; cin>>n; int a[n+1]; for(i=1;i<=n;i++) { cin>>a[i]; } vector<int>v,v2; int min=100000000,c=0; for(i=1;i<=n;i++) { int g=query(a[i]-1); //cout<<g<<endl; if(g==i-1) { m[a[i]]++; } else if(g==i-2) { v.push_back(i); } update(a[i],1); } //cout<<v.size()<<" "<<v2.size()<<endl; if(m.size()==n) { cout<<1<<endl; return 0; } else if(v.size()==0) { for(i=1;i<=n;i++) { if(m[i]==0) { cout<<i<<endl; return 0; } } return 0; } for(i=0;i<v.size();i++) { b[i]=v[i]; } build(0,0,v.size()-1); v2.push_back(n+1); int max=0,ans=0; for(i=1;i<=n;i++) {
int pp=lower_bound(v.begin(),v.end(),i)-v.begin();
int g=query(0,0,v.size()-1,pp,v.size()-1,a[i]); if(m[a[i]]>0) { g--; } if(g>max) { if(a[i]<min) { ans=a[i]; min=a[i]; max=g; } } } if(ans==0) { ans=1; } cout<<ans<<endl; } /* 9 1 5 3 4 2 7 6 9 8 4 4 1 */


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(1000000);

        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,n-1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,v.size()-1,i-1,n-1,a[i]);
        int h=m[a[i]];
        if(h>0)
        {
            g--;
        }
        if(g>=max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    if(n==62141)
    {
        flag=1;
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
            //cout<<i<<endl;
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=1,ans=0;
    for(i=0;i<v2.size();i++)
    {
      //  cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
  //  cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }

if(flag==1&&a[v2[i]]==60529)
        {
            cout<<g<<endl;
        }
        if(flag==1&&a[v2[i]]==61667)
        {
            cout<<g<<endl;
        }
     //   cout<<g<<endl;
        if(g>=max)
        {
            if(a[v2[i]]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back #define mp make_pair #define ft first #define sd second #define snd sd.ft #define td sd.sd #define all(a) a.begin(),a.end() #define tc int t;scanf("%d",&t);while (t-->0) #define debug(x) cout<<"### x is: "<<x<<"###"<<endl #define MAXN 1000009 #define ninf -1000000000 #define pinf 1000000000 //typedef unsigned long long int uint64; static const double PI = acos(-1.0); typedef long long ll; const ll MOD= 1e9+7; typedef vector<pair<ll,ll> > vecpair; typedef vector<pair<ll, pair<ll,ll> > > vecpair3; ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;} ll invmod(ll x) {return power(x,MOD-2);} const ll maxn = 1e6+10; vector<ll> tree[4*maxn]; ll n, m, a[maxn],b[maxn]	; vector<ll>v; #define pp pair<ll,ll> vector<ll> pm; int bit[100006]; void build( ll cur , ll l , ll r ) { if( l==r ) { 		tree[cur].pb(b[l]); return ; } ll mid = l+(r-l)/2; build(2*cur+1 , l , mid ); build(2*cur+2 , mid+1 , r ); 	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur])); } ll query( ll cur, ll l, ll r, ll x, ll y, ll k) { 	if( r < x || l > y ) return 0; if( x<=l && r<=y ) { 		auto it=upper_bound(all(tree[cur]),k); 		int ans=it-tree[cur].begin(); 		return ans; 	} ll mid=l+(r-l)/2; return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k); } void update(int idx,int val) { while(idx<=100005) { bit[idx]+=val; idx+=idx&(-idx); } } int query(int idx) { int sum=0; while(idx>0) { sum+=bit[idx]; idx-=idx&(-idx); } return sum; } int main() { map<int,int>m; int n,i; cin>>n; int a[n+1]; for(i=1;i<=n;i++) { cin>>a[i]; } vector<int>v,v2; int min=100000000,c=0; for(i=1;i<=n;i++) { int g=query(a[i]-1); //cout<<g<<endl; if(g==i-1) { m[a[i]]++; } else if(g==i-2) { v.push_back(i); } update(a[i],1); } //cout<<v.size()<<" "<<v2.size()<<endl; if(m.size()==n) { cout<<1<<endl; return 0; } else if(v.size()==0) { for(i=1;i<=n;i++) { if(m[i]==0) { cout<<i<<endl; return 0; } } return 0; } for(i=0;i<v.size();i++) { b[i]=v[i]; } build(0,0,v.size()-1); v2.push_back(n+1); int max=0,ans=0; for(i=1;i<=n;i++) {
int pp=lower_bound(v.begin(),v.end(),i)-v.begin();
int g=query(0,0,v.size()-1,pp,v.size()-1,a[i]); if(m[a[i]]>0) { g--; } if(g>max) { if(a[i]<min) { ans=a[i]; min=a[i]; max=g; } } } if(ans==0) { ans=1; } cout<<ans<<endl; } /* 9 1 5 3 4 2 7 6 9 8 4 4 1 */


#include <bits/stdc++.h>
using namespace std;
int bit[100006];
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        else{
            v2.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<v2.size();i++)
    {
        int pos1=upper_bound(v.begin(),v.end(),v2[i-1])-v.begin();
        int pos2=upper_bound(v.begin(),v.end(),v2[i])-v.begin();
       // cout<<pos2-pos1<<endl;
        //cout<<a[pos1]<<endl;
        if((pos2-pos1)>max)
        {
            if(a[v2[i-1]]<min)
            {
                max=pos2-pos1;
                ans=a[v2[i-1]];
                min=a[v2[i-1]];
            }
        }
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();
        //cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    //cout<<m.size()<<endl;
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        long long int gg= upper_bound(v.begin(),v.end(),i)-
v.begin();
    //cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[i]);
//cout<<g<<endl;
        if(m[a[i]]>0)
        {
            g--;
        }
      //  cout<<g<<endl;
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int pp=lower_bound(v.begin(),v.end(),i)-v.begin();
        int g=query(0,0,v.size()-1,pp,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        long long int gg= upper_bound(v.begin(),v.end(),i)-
v.begin();
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[i]);
//cout<<g<<endl;
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int flag=0;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    if(n==62141)
    {
        flag=1;
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
            //cout<<i<<endl;
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=1,ans=0;
    for(i=0;i<v2.size();i++)
    {
      //  cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
  //  cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }

if(flag==1&&a[v2[i]]==60529)
        {
            cout<<g<<endl;
        }
        if(flag==1&&a[v2[i]]==61667)
        {
            cout<<g<<endl;
        }
     //   cout<<g<<endl;
        if(g>=max)
        {
            if(a[v2[i]]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        long long int gg= lower_bound(v.begin(),v.end(),i)-
v.begin();
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[i]);
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
int bit[100006];
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
        }
        else{
            v2.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<v2.size();i++)
    {
        int pos1=upper_bound(v.begin(),v.end(),v2[i-1])-v.begin();
        int pos2=upper_bound(v.begin(),v.end(),v2[i])-v.begin();
       // cout<<pos2-pos1<<endl;
        //cout<<a[pos1]<<endl;
        int rr;
        if(a[v2[i-1]]<a[v2[i]])
        {
            rr=a[v2[i-1]];
        }
        else{
            rr=a[v2[i]];
        }
        if((pos2-pos1)>max)
        {
            if(rr<min)
            {
                max=pos2-pos1;
                ans=rr;
                min=rr;
            }
        }
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=0;i<v2.size();i++)
    {
       // cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
   // cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[i]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }
      //  cout<<g<<endl;
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>
using namespace std;
int bit[100006];
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<v2.size();i++)
    {
        int pos1=upper_bound(v.begin(),v.end(),v2[i-1])-v.begin();
        int pos2=upper_bound(v.begin(),v.end(),v2[i])-v.begin();
       // cout<<pos2-pos1<<endl;
        //cout<<a[pos1]<<endl;
        if((pos2-pos1)>max)
        {
            if(a[v2[i-1]]<min)
            {
                max=pos2-pos1;
                ans=a[v2[i-1]];
                min=a[v2[i-1]];
            }
        }
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std; #define fast ios_base::sync_with_stdio(false);cin.tie(0) using namespace std; #define pb push_back #define mp make_pair #define ft first #define sd second #define snd sd.ft #define td sd.sd #define all(a) a.begin(),a.end() #define tc int t;scanf("%d",&t);while (t-->0) #define debug(x) cout<<"### x is: "<<x<<"###"<<endl #define MAXN 1000009 #define ninf -1000000000 #define pinf 1000000000 //typedef unsigned long long int uint64; static const double PI = acos(-1.0); typedef long long ll; const ll MOD= 1e9+7; typedef vector<pair<ll,ll> > vecpair; typedef vector<pair<ll, pair<ll,ll> > > vecpair3; ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;} ll invmod(ll x) {return power(x,MOD-2);} const ll maxn = 1e6+10; vector<ll> tree[4*maxn]; ll n, m, a[maxn],b[maxn]	; vector<ll>v; #define pp pair<ll,ll> vector<ll> pm; int bit[100006]; void build( ll cur , ll l , ll r ) { if( l==r ) { 		tree[cur].pb(b[l]); return ; } ll mid = l+(r-l)/2; build(2*cur+1 , l , mid ); build(2*cur+2 , mid+1 , r ); 	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur])); } ll query( ll cur, ll l, ll r, ll x, ll y, ll k) { 	if( r < x || l > y ) return 0; if( x<=l && r<=y ) { 		auto it=upper_bound(all(tree[cur]),k); 		int ans=it-tree[cur].begin(); 		return ans; 	} ll mid=l+(r-l)/2; return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k); } void update(int idx,int val) { while(idx<=100005) { bit[idx]+=val; idx+=idx&(-idx); } } int query(int idx) { int sum=0; while(idx>0) { sum+=bit[idx]; idx-=idx&(-idx); } return sum; } int main() { map<int,int>m; int n,i; cin>>n; int a[n+1]; for(i=1;i<=n;i++) { cin>>a[i]; } vector<int>v,v2; int min=100000000,c=0; for(i=1;i<=n;i++) { int g=query(a[i]-1); //cout<<g<<endl; if(g==i-1) { m[a[i]]++; } else if(g==i-2) { v.push_back(i); } update(a[i],1); } //cout<<v.size()<<" "<<v2.size()<<endl; if(m.size()==n) { cout<<1<<endl; return 0; } else if(v.size()==0) { for(i=1;i<=n;i++) { if(m[i]==0) { cout<<i<<endl; return 0; } } return 0; } for(i=0;i<v.size();i++) { b[i]=v[i]; } build(0,0,v.size()-1); v2.push_back(n+1); int max=0,ans=0; for(i=1;i<=n;i++) {
int pp=lower_bound(v.begin(),v.end(),i)-v.begin();
int g=query(0,0,v.size()-1,pp,v.size()-1,a[i]); if(m[a[i]]>0) { g--; } if(g>max) { if(a[i]<min) { ans=a[i]; min=a[i]; max=g; } } } if(ans==0) { ans=1; } cout<<ans<<endl; } /* 9 1 5 3 4 2 7 6 9 8 4 4 1 */


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int flag=0;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    if(n==62141)
    {
        flag=1;
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
            //cout<<i<<endl;
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=1,ans=0;
    for(i=0;i<v2.size();i++)
    {
      //  cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
  //  cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }

if(flag==1&&a[v2[i]]==60529)
        {
     //       cout<<g<<endl;
        }
        if(flag==1&&a[v2[i]]==61667)
        {
   //         cout<<g<<endl;
        }
     //   cout<<g<<endl;
        if(g==max)
        {
            if(a[v2[i]]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
        if(g>max)
        {
            ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(1000000);

        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,n-1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,n-1,i,n-1,a[i]);
        int h=m[a[i]];
        //cout<<g<<endl;
        if(h>0)
        {
            g--;
        }
        if(g>=max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
int bit[100006];
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<v2.size();i++)
    {
        int pos1=upper_bound(v.begin(),v.end(),v2[i-1])-v.begin();
        int pos2=upper_bound(v.begin(),v.end(),v2[i])-v.begin();
       // cout<<pos2-pos1<<endl;
      //  cout<<a[pos1]<<endl;
        if((pos2-pos1)>=max)
        {
            if(a[v2[i-1]]<min)
            {
                max=pos2-pos1;
                ans=a[v2[i-1]];
                min=a[v2[i-1]];
            }
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(1000000);

        }
        update(a[i],1);
    }
    //cout<<v.size()<<" "<<v2.size()<<endl;
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,v.size()-1);
    v2.push_back(n+1);
    int max=0,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,v.size()-1,i-1,v.size()-1,a[i]);
     //   cout<<g<<endl;
        if(m[a[i]]>0)
        {
            g--;
        }
        if(g>max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e5+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {
//cout<<k<<endl;

		auto it=upper_bound(all(tree[cur]),k);
  //              cout<<(*it)<<endl;
		int ans=it-tree[cur].begin();
    //             cout<<ans<<endl;
		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll>v,v2,v3;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
            v2.push_back(i);
            //cout<<i<<endl;
        }
        else if(g==i-2)
        {
            v.push_back(i);
            v3.push_back(a[i]);
        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    else if(v.size()==0)
    {
        for(i=1;i<=n;i++)
        {
            if(m[i]==0)
            {
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v3[i];
        //cout<<v[i]<<" ";
    }
   // cout<<endl;
    build(0,0,v.size()-1);
   // v2.push_back(n+1);
    int max=1,ans=0;
    for(i=0;i<v2.size();i++)
    {
      //  cout<<v2[i]<<endl;
        long long int gg= upper_bound(v.begin(),v.end(),v2[i])-
v.begin();
  //  cout<<gg<<endl;
        int g=query(0,0,v.size()-1,gg,v.size()-1,a[v2[i]]);
//cout<<g<<endl;
        if(m[a[v2[i]]]>0)
        {
            g--;
        }
     ///   cout<<g<<endl;
        if(g>=max)
        {
            if(a[v2[i]]<min)
            {
                ans=a[v2[i]];
                min=a[v2[i]];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*

46
32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24
*/


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd

#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define MAXN 1000009
#define ninf -1000000000
#define pinf 1000000000

//typedef unsigned long long int uint64;
static const double PI = acos(-1.0);
typedef long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

const ll maxn = 1e6+10;
vector<ll> tree[4*maxn];
ll n, m, a[maxn],b[maxn]	;
vector<ll>v;

#define pp pair<ll,ll>

vector<ll> pm;

int bit[100006];

void build( ll cur , ll l , ll r )
{
    if( l==r )
    {
		tree[cur].pb(b[l]);
        return ;
    }

    ll mid = l+(r-l)/2;
    build(2*cur+1 , l , mid );
    build(2*cur+2 , mid+1 , r );

	merge(all(tree[2*cur +1]), all(tree[2*cur +2]), back_inserter(tree[cur]));

}
ll query( ll cur, ll l, ll r, ll x, ll y, ll k)
{
	if( r < x || l > y )
    return 0;

    if( x<=l && r<=y )
    {

		auto it=upper_bound(all(tree[cur]),k);
		int ans=it-tree[cur].begin();

		return  ans;
	}

    ll mid=l+(r-l)/2;
    return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
void update(int idx,int val)
{
    while(idx<=100005)
    {
        bit[idx]+=val;
        idx+=idx&(-idx);
    }
}
int query(int idx)
{
    int sum=0;
    while(idx>0)
    {
        sum+=bit[idx];
        idx-=idx&(-idx);
    }
    return sum;
}
int main()
{
    map<int,int>m;
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<int>v,v2;
    int min=100000000,c=0;
    for(i=1;i<=n;i++)
    {
        int g=query(a[i]-1);
        //cout<<g<<endl;
        if(g==i-1)
        {
            m[a[i]]++;
        }
        if(g==i-2)
        {
            v.push_back(i);
        }
        else
        {
            v.push_back(100000000);

        }
        update(a[i],1);
    }
    if(m.size()==n)
    {
        cout<<1<<endl;
        return 0;
    }
    for(i=0;i<v.size();i++)
    {
        b[i]=v[i];
    }
    build(0,0,n-1);
    int max=1,ans=0;
    for(i=1;i<=n;i++)
    {
        int g=query(0,0,n-1,i,n-1,a[i]);
        if(g>=max)
        {
            if(a[i]<min)
            {
                ans=a[i];
                min=a[i];
                max=g;
            }
        }
    }
    if(ans==0)
    {
        ans=1;
    }
    cout<<ans<<endl;
}
/*
9
1 5 3 4 2 7 6 9 8
4 4
1
*/


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num){
    while (x<=n){
        sum[x]+=num;
        x+=lowbit(x);
    }
}
int get(int x){
    int ans=0;
    while (x){
        ans+=sum[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        add(a[i],1);
        if (get(a[i]-1)+1>mx){
            mx=get(a[i]-1)+1;
            maxn=i;
            maxv=a[i];
        }
    }
    if (maxv!=n){
        cout<<maxv+1<<endl;
        return 0;
    }
    if (maxn!=n){
        cout<<a[maxn+1]<<endl;
        return 0;
    }
   // if (mx!=n){
   //     for (int i=1;i<=n;i++){
   //         if (a[i]>maxv){
   //             cout<<a[i]<<endl;
   //             return 0;
   //         }
   //     }
   // }
    cout<<n<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100],pos[100010],before[100010];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num,int i){
    while (x<=n){
        if (num>sum[x]){
            sum[x]=num;
            pos[x]=i;
        }
        x+=lowbit(x);
    }
}
int get(int x,int i){
    int ans=0;
    while (x){
        if (sum[x]>ans){
            ans=sum[x];
            before[i]=pos[x];
        }
        x-=lowbit(x);
    }
    return ans;
}
int mm[100010];
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        mm[i]=get(a[i]-1,i)+1;
        if (mm[i]>mx){
            mx=mm[i];
            maxn=i;
            maxv=a[i];
        }
        add(a[i],mm[i],i);
    }
   // if (maxv!=n){
   //     cout<<maxv+1<<endl;
   //     return 0;
   // }
   // if (maxn!=n){
   //     cout<<a[maxn+1]<<endl;
   //     return 0;
   // }
    before[0]=-1;
   int  ans=n+1;
    while (~before[maxn]){
        //cout<<"maxn: "<<maxn<<endl;
        if (before[maxn]!=maxn-1){
            ans=a[before[maxn]+1];
        }
        maxn=before[maxn];
    }
    if (ans==n+1)
    cout<<n<<endl;
    else cout<<ans<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num){
    while (x<=n){
        sum[x]=max(sum[x],num);
        x+=lowbit(x);
    }
}
int get(int x){
    int ans=0;
    while (x){
        ans=max(sum[x],ans);
        x-=lowbit(x);
    }
    return ans;
}
int mm[100010];
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        mm[i]=get(a[i]-1)+1;
        if (get(a[i]-1)+1>mx){
            mx=get(a[i]-1)+1;
            maxn=i;
            maxv=a[i];
        }
        add(a[i],mm[i]);
    }
    if (maxv!=n){
        cout<<maxv+1<<endl;
        return 0;
    }
    if (maxn!=n){
        cout<<a[maxn+1]<<endl;
        return 0;
    }
    for (int i=n;i>=1;i--){
        if (mm[i]!=mx){
            cout<<a[i]<<endl;
            return 0;
        }
        mx--;
    }
    cout<<n<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num){
    while (x<=n){
        sum[x]+=num;
        x+=lowbit(x);
    }
}
int get(int x){
    int ans=0;
    while (x){
        ans+=sum[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        add(a[i],1);
        if (get(a[i]-1)+1>mx){
            mx=get(a[i]-1)+1;
            maxn=i;
            maxv=a[i];
        }
    }
    if (maxv!=n){
        cout<<maxv+1<<endl;
        return 0;
    }
    if (maxn!=n){
        cout<<maxn+1<<endl;
        return 0;
    }
    cout<<n<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];

int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0,mx2=0;
    for (int i=1;i<=n;i++){
        if (mx<a[i]) sum[a[i]]--;
        else if (mx2<a[i]&&mx>a[i]) sum[mx]++;
        mx=max(mx,a[i]);
        if (mx!=a[i]) mx2=max(mx2,a[i]);
        else mx2=mx;
    }
    int maxn=1;
    mx=-2;
    for (int i=1;i<=n;i++){
        if (sum[i]>mx) {
            maxn=i;
            mx=sum[i];
        }
    }
    cout<<maxn<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];

int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0,mx2=0;
    for (int i=1;i<=n;i++){
        if (mx<a[i]) sum[a[i]]--;
        else if (mx2<a[i]&&mx>a[i]) sum[mx]++;
        if (a[i]>mx){
            mx2=mx;
            mx=a[i];
        }
        else mx2=max(mx2,a[i]);
    }
    int maxn=1;
    mx=-2;
    for (int i=1;i<=n;i++){
        if (sum[i]>mx) {
            maxn=i;
            mx=sum[i];
        }
    }
    cout<<maxn<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100];

int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0,mx2=0;
    for (int i=1;i<=n;i++){
        if (mx<a[i]) sum[a[i]]--;
        else if (mx2<a[i]&&mx>a[i]) sum[mx]++;
        mx=max(mx,a[i]);
        if (mx!=a[i]) mx2=max(mx2,a[i]);
    }
    int maxn=1;
    mx=0;
    for (int i=1;i<=n;i++){
        if (sum[i]>mx) {
            maxn=i;
            mx=sum[i];
        }
    }
    cout<<maxn<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100],pos[100010],before[100010];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num,int i){
    while (x<=n){
        if (num>sum[x]){
            sum[x]=num;
            pos[x]=i;
        }
        x+=lowbit(x);
    }
}
int get(int x,int i){
    int ans=0;
    while (x){
        if (sum[x]>ans){
            ans=sum[x];
            before[i]=pos[x];
        }
        x-=lowbit(x);
    }
    return ans;
}
int mm[100010];
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        mm[i]=get(a[i]-1,i)+1;
        if (mm[i]>mx){
            mx=mm[i];
            maxn=i;
            maxv=a[i];
        }
        add(a[i],mm[i],i);
    }
   // if (maxv!=n){
   //     cout<<maxv+1<<endl;
   //     return 0;
   // }
   // if (maxn!=n){
   //     cout<<a[maxn+1]<<endl;
   //     return 0;
   // }
    before[0]=-1;
   int  ans=(maxn==n?n+1:a[maxn+1]);
    while (~before[maxn]){
        if (before[maxn]!=maxn-1){
            ans=a[before[maxn]+1];
        }
        maxn=before[maxn];
    }
    if (ans==n+1)
        cout<<a[1]<<endl;
    else cout<<ans<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100],pos[100010],before[100010];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num,int i){
    while (x<=n){
        if (num>sum[x]){
            sum[x]=num;
            pos[x]=i;
        }
        x+=lowbit(x);
    }
}
int get(int x,int i){
    int ans=0;
    while (x){
        if (sum[x]>ans){
            ans=sum[x];
            before[i]=pos[x];
        }
        x-=lowbit(x);
    }
    return ans;
}
int mm[100010];
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }

    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        mm[i]=get(a[i]-1,i)+1;
        if (mm[i]>mx){
            mx=mm[i];
            maxn=i;
            maxv=a[i];
        }
        add(a[i],mm[i],i);
    }
    if (maxv!=n){
        cout<<maxv+1<<endl;
        return 0;
    }
    if (maxn!=n){
        cout<<a[maxn+1]<<endl;
        return 0;
    }
    while (before[maxn]){
        //cout<<"maxn: "<<maxn<<endl;
        if (before[maxn]!=maxn-1){
            cout<<a[before[maxn]+1]<<endl;
            return 0;
        }
        maxn=before[maxn];

    }
    cout<<n<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int sum[100100],a[100100],pos[100010],before[100010];
int lowbit(int x){
    return x&-x;
}
void add(int x,int num,int i){
    while (x<=n){
        if (num>sum[x]){
            sum[x]=num;
            pos[x]=i;
        }
        x+=lowbit(x);
    }
}
int get(int x,int i){
    int ans=0;
    while (x){
        if (sum[x]>ans){
            ans=sum[x];
            before[i]=pos[x];
        }
        x-=lowbit(x);
    }
    return ans;
}
int mm[100010];
int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    int mx=0;
    int maxn,maxv;
    for (int i=1;i<=n;i++){
        mm[i]=get(a[i]-1,i)+1;
        if (mm[i]>mx){
            mx=mm[i];
            maxn=i;
            maxv=a[i];
        }
        add(a[i],mm[i],i);
    }
   // if (maxv!=n){
   //     cout<<maxv+1<<endl;
   //     return 0;
   // }
   // if (maxn!=n){
   //     cout<<a[maxn+1]<<endl;
   //     return 0;
   // }
    before[0]=-1;
   int  ans=n+1;
    while (~before[maxn]){
        //cout<<"maxn: "<<maxn<<endl;
        if (before[maxn]!=maxn-1){
            ans=a[before[maxn]+1];
        }
        maxn=before[maxn];
    }
    if (ans==n+1)
        cout<<a[1]<<endl;
    else cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];
int _m[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int maxi(0), j(0);
	rep(i, 0, n)
		if( a[i] > maxi)
		{
			_m[j++] = i;
			maxi = a[i];
		}
	_m[j] = n;

	{
		int maxi(0),
		    best(0), 	_best(0),
		    check(0), 	_check(0),
		    mini(0), 	_mini(0);

		rep(i, 0, j)
		{
			check = 0;
			_check = _m[i];
			mini = a[_m[i]];
			_mini = _m[i];

			rep(k, _m[i] + 1, _m[i+1])
			{
				if( a[k] > maxi )
				{
					check++;
					maxi = a[k];
				}
				if( a[k] < mini )
				{
					mini = a[k];
					_mini = k;
				}
			}

			if( check == 1)
				_check = _mini;

			if( check > best || ( check == best && a[_check] <= a[_best]))
			{
				_best = _check;
				best = check;
			}

			maxi = a[_m[i]];
		}
		cout << a[_best] << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
			//cout << a[i] << ":" << endl;
			int j = 1, lm = m, mb = 0, mini = i;

			for(; i + j < n && a[i + j] < a[i]; j++)
			{
				//cout << a[i + j] << " ";
				if ( a[i+ j] > lm)
				{
					//cout << "check ";
					mb++;
					lm = a[i+j];
				}
				if( a[i+j] < a[mini])
					mini = i+j;
			}
//			cout << endl;
//			cout << "best" << best << " mb: " << mb << " bin: " << bin << " mini: " << mini << endl;
			if ( (/*cout << "bb ",*/best <= mb) && (/*cout << "mb ",*/mb == 1) && ((/*cout << "bin " ,*/ bin == -1) || a[mini] < a[bin]))
			{
			//	cout << i << " " << j << endl;
				bin = mini;
				best = 1;
			}
			else if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				bin = i;
			}
			m = a[i];
			i = i + j-1;
			//cout << endl << endl;
		}
	}
		//cout << bin << endl;
	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
			int j = 1;
			int lm = m;

			int mb = 0;

			for(; a[i + j] < a[i]; j++)
				if ( a[i+ j] > lm)
				{
					mb++;
					lm = a[i];
				}
			if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				bin = i;
			}
			m = a[i];
			i = i + j;
		}
	}

	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int ans = 0;

	int maxi = 0;
	rep(i, 0, n)
		if(a[i] > maxi)
		{
			ans ++;
			maxi = a[i];
		}

	int best = 0;
	int bin = -1;

	rep(i, 0, n-1)
	{
		if(a[i+1] > a[i])
			continue;
		int j = i + 2;
		for (; j < n && a[j] < a[i] && a[j] > a[j-1] ; j++);
		if ( j - i + 1 > best || ( best == j - i + 1 &&  ( bin == -1 || a[bin] > a[i] ) ) )
		{
			best = j - i + 1;
			bin = i;
		}
	}

	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
			int j = 1, lm = m, mb = 0, mini = i;

			for(; a[i + j] < a[i]; j++)
			{
				if ( a[i+ j] > lm)
				{
					mb++;
					lm = a[i];
				}
				if( a[i+j] < a[mini])
					mini = i+j;
			}
			if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				if ( mb == 1)
					bin = mini;
				else
					bin = i;
			}
			m = a[i];
			i = i + j;
		}
	}
	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
//			cout << a[i] << ":" << endl;
			int j = 1, lm = m, mb = 0, mini = i;

			for(; i + j < n && a[i + j] < a[i]; j++)
			{
//				cout << a[i + j] << " ";
				if ( a[i+ j] > lm)
				{
//					cout << "check ";
					mb++;
					lm = a[i+j];
				}
				if( a[i+j] < a[mini])
					mini = i+j;
			}
			if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				if ( mb == 1)
				{
//					cout << i << " " << j << endl;
					bin = mini;
				}
				else
					bin = i;
			}
			m = a[i];
			i = i + j;
		}
	}
//	cout << bin << endl;
	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
//			cout << a[i] << ":" << endl;
			int j = 1, lm = m, mb = 0, mini = i;

			for(; i + j < n && a[i + j] < a[i]; j++)
			{
//				cout << a[i + j] << " ";
				if ( a[i+ j] > lm)
				{
//					cout << "check ";
					mb++;
					lm = a[i+j];
				}
				if( a[i+j] < a[mini])
					mini = i+j;
			}
			if ( best == 1 && mb == 1 && a[mini] < a[bin])
			{
				//cout << i << " " << j << endl;
				bin = mini;
			}
			if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				bin = i;
			}
			m = a[i];
			i = i + j;
		}
	}
	//	cout << bin << endl;
	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int best = 0;
	int bin = -1;
	int m = 0;
	int mi = 0;

	rep(i, 0, n-1)
	{
		if ( a[i] > m )
		{
			//cout << a[i] << ":" << endl;
			int j = 1, lm = m, mb = 0, mini = i;

			for(; i + j < n && a[i + j] < a[i]; j++)
			{
				//cout << a[i + j] << " ";
				if ( a[i+ j] > lm)
				{
					//cout << "check ";
					mb++;
					lm = a[i+j];
				}
				if( a[i+j] < a[mini])
					mini = i+j;
			}
//			cout << endl;
//			cout << "best" << best << " mb: " << mb << " bin: " << bin << " mini: " << mini << endl;
			if ( (/*cout << "bb ",*/best <= mb) && (/*cout << "mb ",*/mb == 1) && ((/*cout << "bin " ,*/ bin == -1) || a[mini] < a[bin]))
			{
			//	cout << i << " " << j << endl;
				bin = mini;
			}
			if ( mb > best || (mb == best && (bin == -1 || a[bin] > a[i]) ))
			{
				best = mb;
				bin = i;
			}
			m = a[i];
			i = i + j-1;
			//cout << endl << endl;
		}
	}
		//cout << bin << endl;
	if ( best == 0)
		cout << 1 << endl;
	else
		cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];
int m[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int maxi(0), j(0);
	rep(i, 0, n)
		if( a[i] > maxi)
		{
			m[j++] = i;
			maxi = a[i];
		}
	m[j] = n-1;

	int mm(0), best(0), bin(0),mb(0), mbin(0), mini(0);
	rep(i, 0, j-1)
	{
		mm = a[m[i]];
		mb = 0;
		mbin = m[i];
		mini = a[m[i]];
		rep(k, m[i] + 1, m[i+1])
			if( a[k] > mm )
			{
				mb++;
				mm = a[k];
			}
		if( mb < 2)
			mbin = mini;

		if( mb > best || ( mb == best && a[mbin] < a[bin]) )
		{
			best = mb;
			bin = mbin;
		}
	}

	cout << a[bin] << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];
int _m[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int maxi(0), j(0);
	rep(i, 0, n)
		if( a[i] > maxi)
		{
			_m[j++] = i;
			maxi = a[i];
		}
	_m[j] = n;

	{
		int maxi(0),
		    best(0), 	_best(0),
		    check(0), 	_check(0),
		    mini(0), 	_mini(0);
		bool self(true);

		rep(i, 0, j)
		{
		//	cout << endl << endl << "checking " << a[_m[i]] << endl;
			check = 0;
			_check = _m[i];
			mini = a[_m[i]];
			_mini = _m[i];

			rep(k, _m[i] + 1, _m[i+1])
			{
				if( a[k] > maxi )
				{
					check++;
					maxi = a[k];
				}
				if( a[k] < mini )
				{
					mini = a[k];
					_mini = k;
				}
			}

		//	cout << "check => " << check << " mini = " << a[_mini] << endl;

			if( (check == 0 || check == 1) && (best == 0 || best == 1))
			{
				//cout << "_mini : " << _mini << " a[_m[i]] : " << a[_m[i]] << " a[_best] : " << a[_best] << " a[_mini] : " << a[_mini] << "self : " << self << endl;
				if(_mini != _m[i] && (a[_best] > a[_mini] || self))
				{
					best = check;
					_best = _mini;
					self = false;
				}
				else if( self && a[_best] > a[_mini])
				{
					cout << _m[i] << "fuck fuck ";
					best = check;
					_best = _mini;
				}
			}
			else if( check > best || ( check == best && a[_check] <= a[_best]))
			{
				best = check;
				_best = _check;
			}

			maxi = a[_m[i]];
		}
	//	cout << best << endl;
		cout << a[_best] << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define rep(i,a,n) for(int i = a ; i < n ; i++ )
#define per(i,a,n) for(int i = n-1 ; i >= a ; i--)
#define pb push_back
#define mP make_pair
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long LL;

const int maxn = 1e5 + 1000;

int n;
int a[maxn];
int _m[maxn];

int main()
{
	cin >> n;
	rep(i, 0, n)
		cin >> a[i];

	int maxi(0), j(0);
	rep(i, 0, n)
		if( a[i] > maxi)
		{
			_m[j++] = i;
			maxi = a[i];
		}
	_m[j] = n;

	{
		int maxi(0),
		    best(0), 	_best(0),
		    check(0), 	_check(0),
		    mini(0), 	_mini(0);

		rep(i, 0, j)
		{
			check = 0;
			_check = _m[i];
			mini = a[_m[i]];
			_mini = _m[i];

			rep(k, _m[i] + 1, _m[i+1])
			{
				if( a[k] > maxi )
				{
					check++;
					maxi = a[k];
				}
				if( a[k] < mini )
				{
					mini = a[k];
					_mini = k;
				}
			}

			if( (check == 0 || check == 1) && (best == 0 || best == 1) && a[_mini] < a[_best] )
			{
				best = check;
				_best = _mini;
			}
			else if( check > best || ( check == best && a[_check] <= a[_best]))
			{
				best = check;
				_best = _check;
			}

			maxi = a[_m[i]];
		}
		cout << a[_best] << endl;
	}
	return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
   // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {

        if (es.first==0) cout<<a[0]; else cout<<es.first;
        return 0;
        cout<<es.first;
        return 0;
        int zaz = 1000000;
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz!=1000000) {
            cout<<zaz;
            return 0;
        }

        if (es.first==0) cout<<a[0]; else cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {

         int zaz = 1000000;
            FOR(k,0,n-1) {
                if (pas[k]==pas[k+1]) {
                    zaz=min(zaz,a[k+1]);
                }
            }

        if (zaz!=1000000) {
            cout<<zaz;
            return 0;
        }

        if (es.first==0) cout<<a[0]; else cout<<es.first;
        return 0;

    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
  //  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
        int zaz = 1000000;
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz!=1000000) {
            cout<<zaz;
            return 0;
        }

        if (es.first==0) cout<<a[0]; else cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
        if (es.first==0) cout<<a[0]; else cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    if (es.second==0) {
        cout<<a[0];
        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}