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

typedef long long int Long;
typedef long double Double;
const int MAXN = 1e5 + 4;
const int MAXLG = 21;

int n, p[MAXN], B[MAXN], T[MAXLG][MAXN];
set<int> S[MAXN];

void update(int i, int val) {
	while(i <= n) {
		B[i] += val;
		i += (i & -i);
	}
}
int get(int i) {
	int sum = 0;
	while(i) {
		sum += B[i];
		i -= (i & -i);
	}
	return sum;
}
int get(int i, int j) {
	if(i > j) return 0;
	return get(j) - get(i - 1);
}
void build() {
	for(int i = 0; i < n; i++)
        T[i][0] = p[i + 1];
    for(int j = 1; j <= MAXLG; j++) {
        for(int i = 0; i <= n - (1 << j); i++)
            T[i][j] = max(T[i][j - 1] , T[i + (1 << (j - 1))][j - 1]);
    }
    /*
	for(int i = 1; i <= n; ++i)
		T[0][i] = p[i];
	for(int i = 1; (1 << i) <= n; ++i)
		for(int j = 1; j + (1 << (i - 1)) <= n; ++j)
			T[i][j] = max(T[i - 1][j], T[i - 1][j + (1 << (i - 1))]);
			*/
}
int query(int L, int R) {
	if(L > R) return 0;
	/*if(i > j) return 0;
	int diff = j - i + 1, k = MAXLG;
	while((1 << k) > diff) --k;
	return max(T[k][i], T[k][j - (1 << k) + 1]);*/

	int answer = 0;
    for(int j = MAXLG; j >= 0; j--) {
        if(L + (1 << j) - 1 <= R) {
            answer = max(answer, T[L][j]);
            L += 1 << j;
        }
    }
    return answer;
}

int main() {
	memset(B, 0, sizeof(B));
	memset(T, 0, sizeof(T));

	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", p + i);
	build();

	for(int i = 1; i <= n; ++i) {
		if(get(p[i] + 1, n) == 1) {
			int badMatch = query(0, i - 2);
			S[badMatch].insert(p[i]);
		}
		update(p[i], 1);
	}

	int best = -1, ans = 0;
	for(int i = 1; i <= n; ++i) {
		// printf("%d) %d\n", i, (int) S[i].size());
		if(best < (int) S[i].size()) {
			best = (int) S[i].size();
			ans = i;
		}
	}

	printf("%d\n", ans);
	return 0;
}


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

typedef long long int Long;
typedef long double Double;
const int MAXN = 1e5 + 4;
const int MAXLG = 21;

int n, p[MAXN], B[MAXN], T[MAXLG][MAXN];
vector<int> V[MAXN];

void update(int i, int val) {
	while(i <= n) {
		B[i] += val;
		i += (i & -i);
	}
}
int get(int i) {
	int sum = 0;
	while(i) {
		sum += B[i];
		i -= (i & -i);
	}
	return sum;
}
void build() {
	for(int i = 1; i <= n; ++i)
		T[0][i] = p[i];
	for(int i = 1; (1 << i) <= n; ++i)
		for(int j = 1; j + (1 << (i - 1)) <= n; ++j)
			T[i][j] = max(T[i - 1][j], T[i - 1][j + (1 << (i - 1))]);
}
int query(int i, int j) {
	int diff = j - i + 1, k = MAXLG;
	while((1 << k) > diff) --k;
	return max(T[k][i], T[k][j - (1 << k) + 1]);
}

int main() {
	memset(B, 0, sizeof(B));
	memset(T, 0, sizeof(T));

	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", p + i);
	build();

	for(int i = 1; i <= n; ++i) {
		// printf("%d %d %d %d %d\n", p[i], get(p[i] - 1), get(p[i] - 1) == p[i] - 1, p[i] + 1 == i, i+1);
		if(get(p[i] - 1) == p[i] - 1 && p[i] + 1 == i) {
			int badMatch = query(1, i - 1);
			V[badMatch].push_back(p[i]);
		}
		update(p[i], 1);
	}

	int best = -1, ans = 0;
	for(int i = 1; i <= n; ++i) {
		// printf("%d) %d\n", i, (int) V[i].size());
		if(best < (int) V[i].size()) {
			best = (int) V[i].size();
			ans = i;
		}
	}

	printf("%d\n", ans);
	return 0;
}


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

typedef long long int Long;
typedef long double Double;
const int MAXN = 1e5 + 4;
const int MAXLG = 21;

int n, p[MAXN], B[MAXN], T[MAXLG][MAXN];
vector<int> V[MAXN];

void update(int i, int val) {
	while(i <= n) {
		B[i] += val;
		i += (i & -i);
	}
}
int get(int i) {
	int sum = 0;
	while(i) {
		sum += B[i];
		i -= (i & -i);
	}
	return sum;
}
int get(int i, int j) {
	if(i > j) return 0;
	return get(j) - get(i - 1);
}
void build() {
	for(int i = 1; i <= n; ++i)
		T[0][i] = p[i];
	for(int i = 1; (1 << i) <= n; ++i)
		for(int j = 1; j + (1 << (i - 1)) <= n; ++j)
			T[i][j] = max(T[i - 1][j], T[i - 1][j + (1 << (i - 1))]);
}
int query(int i, int j) {
	if(i > j) return 0;
	int diff = j - i + 1, k = MAXLG;
	while((1 << k) > diff) --k;
	return max(T[k][i], T[k][j - (1 << k) + 1]);
}

int main() {
	memset(B, 0, sizeof(B));
	memset(T, 0, sizeof(T));

	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", p + i);
	build();

	for(int i = 1; i <= n; ++i) {
		if(get(p[i] + 1, n) == 1 && get(p[i] - 1) >= p[i] - 1) {
			int badMatch = query(1, i - 1);
			V[badMatch].push_back(p[i]);
		}
		update(p[i], 1);
	}

	int best = -1, ans = 0;
	for(int i = 1; i <= n; ++i) {
		printf("%d) %d\n", i, (int) V[i].size());
		if(best < (int) V[i].size()) {
			best = (int) V[i].size();
			ans = i;
		}
	}

	assert(ans != 0);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    int oldRc=rc;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=oldRc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        for(i=1;i<=n;i++)
        {
            if(!record[i])
            {
                ans=min(ans,i);
            }
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define st first
#define nd second
int main()
{
    int n;
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<pair<int,int> >vt;
    int p1,p2;
    p1=p2=0;
    for(i=1;i<n;i++)
    {
        if(a[i]>a[i-1])
        {
            p2++;
        }
        else
        {
            vt.pb({p1,p2});
            p1=p2=i;
        }
    }
    vt.pb({p1,p2});
    int ans=0;
    int maxLen=0;
    if(vt.size()==1)
    {
        ans=a[vt[0].st];
    }
    for(i=1;i<vt.size();i++)
    {
        int u=vt[i-1].nd;
        int v=vt[i].st;
        if(vt[i-1].st!=vt[i-1].nd && a[u-1]<a[v])
        {
            if(maxLen<=vt[i].nd-vt[i-1].st)
            {
                if(maxLen==vt[i].nd-vt[i-1].st)
                {
                    ans=min(ans,a[u]);
                }
                else
                {
                    ans=a[u];
                }
                maxLen=vt[i].nd-vt[i-1].st;
            }
        }
        if(vt[i].st!=vt[i].nd && a[u]<a[v+1])
        {
            if(maxLen<=vt[i].nd-vt[i-1].st)
            {
                if(maxLen==vt[i].nd-vt[i-1].st)
                {
                    ans=min(ans,a[u]);
                }
                else
                {
                    ans=a[v];
                }
                maxLen=vt[i].nd-vt[i-1].st;
            }
        }
        if(vt[i-1].st==vt[i-1].nd)
        {
            if(maxLen<=vt[i].nd-vt[i].st+1)
            {
                if(maxLen==vt[i].nd-vt[i].st+1)
                {
                    ans=min(ans,a[u]);
                }
                else
                {
                    ans=a[u];
                }
                maxLen=vt[i].nd-vt[i].st+1;
            }
        }
        if(vt[i].st==vt[i].nd)
        {
            if(maxLen<=vt[i-1].nd-vt[i-1].st+1)
            {
                if(maxLen==vt[i-1].nd-vt[i-1].st+1)
                {
                    ans=min(ans,a[v]);
                }
                else
                {
                    ans=a[v];
                }
                maxLen=vt[i-1].nd-vt[i-1].st+1;
            }
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
//    for(i=1;i<=n;i++)
//    {
//        if(record[i])
//        {
//            cout<<i<<" ";
//        }
//    }
//    cout<<endl;
//    for(auto it:vt)
//    {
//        cout<<it<<" ";
//    }
    int ans=1e9;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=i;
        int total=rc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=i;
        int total=rc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=rc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    int oldRc=rc;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=oldRc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    int oldRc=rc;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=oldRc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        for(i=1;i<=n;i++)
        {
            if(!record[i])
            {
                ans=min(ans,i);
            }
        }
        if(ans==1e9)
        {
            ans=1;
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=0;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    int oldRc=rc;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=oldRc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ll long long
#define ld long double
const int N=1e5;
int bit[N+5];
int mx[N+5];
int n;
int record[N+5];
void update(int idx)
{
    while(idx<=n)
    {
        bit[idx]++;
        idx+=(idx&-idx);
    }
}
int getSum(int idx)
{
    int ans=0;
    while(idx>0)
    {
        ans+=bit[idx];
        idx-=(idx&-idx);
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    int i,t;
    vector<int> a;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        a.pb(t);
    }
    vector<int> vt;
    int rc=1;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            mx[i]=a[i];
        }
        else
        {
            mx[i]=max(a[i],mx[i-1]);
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            record[a[i]]++;
            update(a[i]);
            continue;
        }
        int bg=getSum(n)-getSum(a[i]);
        if(bg==1)
        {
            vt.pb(i);
        }
        if(bg==0)
        {
            record[a[i]]++;
            rc++;
        }
        update(a[i]);
    }
    int ans=1e9;
    int oldRc=rc;
    for(i=0;i<vt.size();i++)
    {
        int u=vt[i];
        int newRc=0;
        while(i+1<vt.size() && mx[u]==mx[vt[i+1]])
        {
            i++;
            newRc++;
        }
        u=vt[i];
        int total=oldRc+newRc;
        if(record[mx[u]])
        {
            total--;
        }
        if(total>=rc)
        {
            if(total==rc)
            {
                ans=min(ans,mx[u]);
            }
            else
            {
                ans=mx[u];
            }
            rc=total;
        }
    }
    if(ans==1e9)
    {
        ans=1;
    }
    printf("%d",ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e4;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p,p2;
        int vis[maxn]={},cnt=0;
        memset(vis,0,sizeof(vis));
        int x;
        scanf("%d",&x);p.push_back(x);vis[x]=1;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            scanf("%d",&x);
            int pos=lower_bound(p.begin(),p.end(),x)-p.begin();
            if(pos==p.size()) vis[x]=1,cnt++;
            else if(pos==p.size()-1) p2.push_back(p[pos]),kk[p[pos]].push_back(x);
            p.push_back(x);sort(p.begin(),p.end());
        }
        int ans=p[0],tem=cnt;
        for(int i=0;i<p2.size();i++){
            int x2=p2[i];
            int tem2=-vis[x2];
            tem2+=kk[x2].size();
            if(tem2>tem) ans=x2,tem2=tem;
            else if(tem2==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e4;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn];
        int cnt=0,p;
        scanf("%d",&p);
        kk[cnt].push_back(p);
        for(int i=2;i<=n;i++){
            scanf("%d",&p);
            if(p<kk[cnt].back()) cnt++;
            kk[cnt].push_back(p);
        }
        int ans=kk[0][0],maxx=kk[0].size();
        for(int i=1;i<=cnt;i++){
            int tem1=kk[i-1].size(),tem2=kk[i].size();
            if(tem1+tem2-1<maxx) continue;
            if(kk[i-1][tem1-2]<kk[i][0]) {
                if(maxx==tem1+tem2-1) ans=min(ans,kk[i-1][tem1-1]);
                else ans=kk[i-1][tem1-1];
                maxx=max(maxx,tem1+tem2-1);
            }
            else if(kk[i-1][tem1-1]<kk[i][1]){
                if(maxx==tem1+tem2-1) ans=min(ans,kk[i][0]);
                else ans=kk[i][0];
                maxx=max(maxx,tem1+tem2-1);
            }
            else{
                if(tem1>=maxx){
                    if(maxx==tem1) ans=min(ans,kk[i-1][0]);
                    else ans=kk[i-1][0];
                    maxx=max(maxx,tem1);
                }
                if(tem2>=maxx){
                    if(maxx==tem2) ans=min(ans,kk[i][0]);
                    else ans=kk[i][0];
                    maxx=max(maxx,tem2);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e4;
int n;
template <class T>
inline void read(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p2;
        int vis[maxn]={},flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem1=0,tem2=-1,tem=0,xx;
        read(x);
        xx=x;tem1=xx;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            read(x);
            if(x>tem1) vis[x]=1,swap(tem1,x),tem2=max(tem2,x);
            else if(x>tem2 && i!=2) p2.push_back(tem1),kk[tem1].push_back(x),tem2=x;
            else ans=min(ans,x);
            if(i==2) tem2=x;
            //cout<<tem1<<" "<<tem2<<endl;
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        //cout<<ans<<endl;
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            //cout<<x2<<endl;
            int tem3=-vis[x2];
            tem3=tem3+kk[x2].size();
            //cout<<tem3<<" "<<tem<<endl;
            if(tem3>tem) ans=x2,tem=tem3;
            else if(tem3==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn];
        int cnt=0,p;
        scanf("%d",&p);
        kk[cnt].push_back(p);
        for(int i=2;i<=n;i++){
            scanf("%d",&p);
            if(p<kk[cnt].back()) cnt++;
            kk[cnt].push_back(p);
        }
        int ans=kk[0][0],maxx=kk[0].size();
        for(int i=2;i<cnt;i++){
            int tem1=kk[i-1].size(),tem2=kk[i].size();
            if(tem1+tem2-1<=maxx) continue;
            if(kk[i-1][tem1-2]<kk[i][0]) maxx=max(maxx,tem1+tem2-1),ans=kk[i-1][tem1-1];
            else if(kk[i-1][tem1-1]<kk[i][1]) maxx=max(maxx,tem1+tem2-1),ans=ans=kk[i][0];
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e4;
int n;
template <class T>
inline void read(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p2;
        int vis[maxn]={},flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem1=0,tem2=-1,tem=0,xx;
        read(x);
        xx=x;tem1=xx;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            read(x);
            if(x>tem1) vis[x]=1,swap(tem1,x),tem2=max(tem2,x);
            else if(x>tem2 && i!=2) p2.push_back(tem1),kk[tem1].push_back(x),tem2=x;
            else ans=min(ans,x);
            if(i==2) tem2=x;
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem3=-vis[x2];
            tem3=tem3+kk[x2].size();
            if(tem3>tem) ans=x2,tem=tem3;
            else if(tem3==tem && ans>x2) ans=x2;
        }
        if(ans==maxn) ans=xx;
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p,p2;
        int vis[maxn]={},index[maxn]={},cnt=1;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem=0,xx;
        scanf("%d",&x);p.push_back(x);
        xx=x;index[x]=1;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            scanf("%d",&x);index[x]=i;
            int pos=lower_bound(p.begin(),p.end(),x)-p.begin();
            if(pos==p.size()) vis[x]=1,cnt++;
            else if(pos==p.size()-1) p2.push_back(p[pos]),kk[p[pos]].push_back(x);
            else ans=min(ans,x);
            p.push_back(x);sort(p.begin(),p.end());
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem2=-vis[x2];
            tem2=tem2+kk[x2].size();
            if(kk[x2].size()==1 && x2==xx && tem<=tem2){
                ans=min(ans,xx);
                for(int i=0;i<kk[x2].size();i++)if(index[kk[x2][i]]==2){
                    tem2--;break;
                }
            }
            if(tem2>tem) ans=x2,tem=tem2;
            else if(tem2==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p,p2;
        int vis[maxn]={},index[maxn]={},cnt=1,flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem=0,xx;
        scanf("%d",&x);p.push_back(x);
        xx=x;index[x]=1;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            scanf("%d",&x);index[x]=i;
            int pos=lower_bound(p.begin(),p.end(),x)-p.begin();
            if(pos==p.size()) vis[x]=1,cnt++;
            else if(pos==p.size()-1 && i!=2) p2.push_back(p[pos]),kk[p[pos]].push_back(x);
            else ans=min(ans,x);
            p.push_back(x);sort(p.begin(),p.end());
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem2=-vis[x2];
            tem2=tem2+kk[x2].size();
            if(tem2>tem) ans=x2,tem=tem2;
            else if(tem2==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
template <class T>
inline void read(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p2;
        int vis[maxn]={},flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem1=0,tem2=-1,tem=0,xx;
        read(x);
        xx=x;tem1=xx;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            read(x);
            if(x>tem1) vis[x]=1,swap(tem1,x),tem2=max(tem2,x);
            else if(x>tem2 && i!=2) p2.push_back(tem1),kk[tem1].push_back(x),tem2=x;
            else ans=min(ans,x);
            if(i==2) tem2=x;
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem3=-vis[x2];
            tem3=tem3+kk[x2].size();
            if(tem3>tem) ans=x2,tem=tem3;
            else if(tem3==tem && ans>x2) ans=x2;
        }
        if(ans==maxn) ans=xx;
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
template <class T>
inline void read(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p,p2;
        int vis[maxn]={},index[maxn]={},cnt=1,flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem=0,xx;
        read(x);p.push_back(x);
        xx=x;index[x]=1;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            read(x);index[x]=i;
            int pos=lower_bound(p.begin(),p.end(),x)-p.begin();
            if(pos==p.size()) vis[x]=1,cnt++;
            else if(pos==p.size()-1 && i!=2) p2.push_back(p[pos]),kk[p[pos]].push_back(x);
            else ans=min(ans,x);
            p.push_back(x);sort(p.begin(),p.end());
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem2=-vis[x2];
            tem2=tem2+kk[x2].size();
            if(tem2>tem) ans=x2,tem=tem2;
            else if(tem2==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p,p2;
        int vis[maxn]={},cnt=1;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem=0;;
        scanf("%d",&x);p.push_back(x);vis[x]=1;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            scanf("%d",&x);
            int pos=lower_bound(p.begin(),p.end(),x)-p.begin();
            if(pos==p.size()) vis[x]=1,cnt++;
            else if(pos==p.size()-1) p2.push_back(p[pos]),kk[p[pos]].push_back(x);
            else ans=min(ans,x);
            p.push_back(x);sort(p.begin(),p.end());
        }
        for(int i=0;i<p2.size();i++){
            int x2=p2[i];
            int tem2=-vis[x2];
            tem2+=kk[x2].size();
            if(tem2>tem) ans=x2,tem2=tem;
            else if(tem2==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e5;
int n;
template <class T>
inline void read(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn],p2;
        int vis[maxn]={},flag;
        memset(vis,0,sizeof(vis));
        int x,ans=maxn,tem1=0,tem2=-1,tem=0,xx;
        read(x);
        xx=x;tem1=xx;
        if(n==1){
            printf("%d\n",x);
            continue;
        }
        for(int i=2;i<=n;i++){
            read(x);
            if(x>tem1) vis[x]=1,swap(tem1,x),tem2=max(tem2,x);
            else if(x>tem2 && i!=2) p2.push_back(tem1),kk[tem1].push_back(x),tem2=x;
            else ans=min(ans,x);
            if(i==2) tem2=x;
        }
        int ksize=unique(p2.begin(),p2.end())-p2.begin();
        for(int i=0;i<ksize;i++){
            int x2=p2[i];
            int tem3=-vis[x2];
            tem3=tem3+kk[x2].size();
            if(tem3>tem) ans=x2,tem=tem3;
            else if(tem3==tem && ans>x2) ans=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=2e4;
int n;
int main()
{
    while(scanf("%d",&n)==1){
        vector<int>kk[maxn];
        int cnt=0,p;
        scanf("%d",&p);
        kk[cnt].push_back(p);
        for(int i=2;i<=n;i++){
            scanf("%d",&p);
            if(p<kk[cnt].back()) cnt++;
            kk[cnt].push_back(p);
        }
        int ans=kk[0][0],maxx=kk[0].size();
        for(int i=1;i<=cnt;i++){
            int tem1=kk[i-1].size(),tem2=kk[i].size();
            if(tem1+tem2-1<maxx) continue;
            if(kk[i-1][tem1-2]<kk[i][0]) {
                if(maxx==tem1+tem2-1) ans=min(ans,kk[i-1][tem1-1]);
                else ans=kk[i-1][tem1-1];
                maxx=max(maxx,tem1+tem2-1);
            }
            if(kk[i-1][tem1-1]<kk[i][1]){
                if(maxx==tem1+tem2-1) ans=min(ans,kk[i][0]);
                else ans=kk[i][0];
                maxx=max(maxx,tem1+tem2-1);
            }
            if(tem1>=maxx){
                if(maxx==tem1) ans=min(ans,kk[i-1][0]);
                else ans=kk[i-1][0];
                maxx=max(maxx,tem1);
            }
            if(tem2>=maxx){
                if(maxx==tem2) ans=min(ans,kk[i][0]);
                else ans=kk[i][0];
                maxx=max(maxx,tem2);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=4e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
int bit[MAX+1],n;
int sum(int i)//[1,i]ǰ׺��
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)//�Ը�����a[i],ִ��a[i]+=x
{
    while(i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}
stack<int>st;
int num,ans;
int ldp[MAX],a[MAX],dp[MAX],cnt;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
    {
        ldp[i]=(i-sum(a[i]));
        add(a[i],1);
    }
//    pre[0]=1;
    for(int i=0;i<n;i++)
    {
        if(!ldp[i])++cnt;
//        if(ldp[i]==0)
//            pre[i]=pre[i-1]+1;
//        else pre[i]=pre[i-1];
    }
    num=1;ans=1;
//    ans=1;num=1;
    for(int i=n-1;i>=0;i--)
    {
//        if(!i)
//        {
//            if(a[1]<a[0])dp[i]=-1;
//            else dp[i]=0;
//        }
//        else if(ldp[i]==0)dp[i]=-1;
//        dp[i]=1;
//        if(ldp[i]==0)
//        {
//            if(i)
//            --dp[i];
//            else if(a[1]<a[0])
//                --dp[i];
//        }
        dp[i]=cnt;
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(ldp[i]==0)
            --dp[i];
//        dp[i]+=pre[i];
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}
/*
6
3 1 5 2 6 4
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=1e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
int bit[MAX+1],n;
int sum(int i)//[1,i]ǰ׺��
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)//�Ը�����a[i],ִ��a[i]+=x
{
    while(i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}
stack<int>st;
int num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        ldp[i]=(i-sum(a[i]));
        add(a[i],1);
//        while(!st.empty()&&st.top()<a[i])
//            st.pop();
//        ldp[i]=st.size();//�����������ĸ���
//        st.push(a[i]);
    }
//    for(int i=0;i<n;i++)
//        printf("%d ",ldp[i]);
//    printf("\n");
//    while(!st.empty())st.pop();
    ans=1;num=1;
    for(int i=n-1;i>=0;i--)
    {
        dp[i]=1;
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}
/*
6
3 1 5 2 6 4
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=4e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
int bit[MAX+1],n;
int sum(int i)//[1,i]ǰ׺��
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)//�Ը�����a[i],ִ��a[i]+=x
{
    while(i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}
stack<int>st;
int num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
    {
        ldp[i]=(i-sum(a[i]));
        add(a[i],1);
    }
    ans=1;num=1;
    for(int i=n-1;i>=0;i--)
    {
//        if(i)
            dp[i]=1;
        if(ldp[i]==0)
            --dp[i];
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}
/*
6
3 1 5 2 6 4
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=1e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
int bit[MAX+1],n;
int sum(int i)//[1,i]ǰ׺��
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)//�Ը�����a[i],ִ��a[i]+=x
{
    while(i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}
stack<int>st;
int num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        ldp[i]=(i-sum(a[i]));
        add(a[i],1);
//        while(!st.empty()&&st.top()<a[i])
//            st.pop();
//        ldp[i]=st.size();//�����������ĸ���
//        st.push(a[i]);
    }
//    for(int i=0;i<n;i++)
//        printf("%d ",ldp[i]);
//    printf("\n");
//    while(!st.empty())st.pop();
    ans=1;num=1;
    for(int i=n-1;i>=0;i--)
    {
        if(i)
            dp[i]=1;
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}
/*
6
3 1 5 2 6 4
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=1e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
stack<int>st;
int n,num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        while(!st.empty()&&st.top()<a[i])
            st.pop();
        ldp[i]=st.size();//�����������ĸ���
        st.push(a[i]);
    }
//    for(int i=1;i<=n;i++)
//        printf("%d ",ldp[i]);
//    printf("\n");
    while(!st.empty())st.pop();
    ans=1;num=1;
    for(int i=n;i>=1;i--)
    {
        dp[i]=1;
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }

        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=4e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
int bit[MAX+1],n;
int sum(int i)//[1,i]ǰ׺��
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)//�Ը�����a[i],ִ��a[i]+=x
{
    while(i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}
stack<int>st;
int num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
    {
        ldp[i]=(i-sum(a[i]));
        add(a[i],1);
    }
    ans=1;num=1;
    for(int i=n-1;i>=0;i--)
    {
            dp[i]=1;
        if(ldp[i]==0)
        {
            if(i)
            --dp[i];
            else if(a[1]<a[0])
                --dp[i];
        }
        while(!st.empty()&&st.top()<a[i])
            {st.pop();++dp[i];}
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        if(ldp[i]==1)//�������������ĸ���Ϊ1
            st.push(a[i]);
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}
/*
6
3 1 5 2 6 4
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <utility>
#include <assert.h>
#include <limits.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a,b) ((b)&1?(a):0)
#define tm tmp

//#define LL ll
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
//const int INF= 0x7fffffff;
//const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF=0x3f3f3f3f3f3f3fll;
//const ll INFF=1e14+5;
const int MAX=1e5+5;
//const ll MAXN=2e8;
//const int MAX_N=MAX;
const ll MOD=100000007;
//const long double pi=acos(-1.0);
//const double eps=0.00000001;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<class T> inline
void read(T& num) {
    bool start=false,neg=false;
    char c;
    num=0;
    while((c=getchar())!=EOF) {
        if(c=='-') start=neg=true;
        else if(c>='0' && c<='9') {
            start=true;
            num=num*10+c-'0';
        } else if(start) break;
    }
    if(neg) num=-num;
}
inline ll powMM(ll a,ll b,ll M){
    ll ret=1;
    a%=M;
//    b%=M;
    while (b){
        if (b&1) ret=ret*a%M;
        b>>=1;
        a=a*a%M;
    }
    return ret;
}
void open()
{
//    freopen("1009.in","r",stdin);
    freopen("out.txt","w",stdout);
}
stack<int>st;
int n,num,ans;
int ldp[MAX],a[MAX],dp[MAX];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        while(!st.empty()&&st.top()<a[i])
            st.pop();
        ldp[i]=st.size();
        st.push(a[i]);
    }
//    for(int i=1;i<=n;i++)
//        printf("%d ",ldp[i]);
//    printf("\n");
    while(!st.empty())st.pop();
    ans=1;num=1;
    for(int i=n;i>=1;i--)
    {
        dp[i]=st.size();
        if(dp[i]>num)
        {
            num=dp[i];
            ans=a[i];
        }
        else if(dp[i]==num)
        {
            if(a[i]<ans)
                ans=a[i];
        }
        while(!st.empty()&&st.top()<a[i])
            st.pop();
        if(ldp[i]==1)
        {
            st.push(a[i]);
        }
    }

//    for(int i=1;i<=n;i++)
//        printf("%d ",dp[i]);
//    printf("\n");
//    if(num>1)
    printf("%d\n",ans);
//    else printf("1\n");
}


//
// Program by ilbonius2000
//
#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

# define in freopen("", "r", stdin)
# define ou freopen("", "w", stdout)
# define IOS ios_base::sync_with_stdio(false)

# define ull unsigned long long
# define ll long long
# define pb push_back
# define ppb pop_back
# define mp make_pair
# define ld long double

using namespace std;

const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = -1e18;
const ll INF = 1e18;

ll n, a[N], dp[N], mx;
priority_queue <ll> pq;

void solve() {
   cin >> n;
   for(int i = 1;i <= n;i++) {
       cin >> a[i];
   }
   if(a[1] > a[2]) {
      dp[a[1]]++;
      dp[a[2]]++;
   }
   pq.push(a[1]);
   pq.push(a[2]);
   for(int i = 3;i <= n;i++) {
       if(pq.top() > a[i]) {
          int v = pq.top();
          pq.pop();
          if(pq.top() < a[i]) dp[v]++;
          else {
              dp[a[i]]++;
          }
          pq.push(v);
       }
       pq.push(a[i]);
   }
   for(int i = 1;i <= n;i++) {
       mx = max(mx, dp[i]);
   }
   for(int i = 1;i <= n;i++) {
       if(mx == dp[i]) {
           cout << i;
           return ;
       }
   }
   cout << 1;
}

int main() {
     solve();
     return 0;
}


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }
    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        if (Y<X){
            ans[pos] = 0 ;
        }
        else{
            int cnt = 0 ;
            for(int i = pos+1 ; i <= n ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt-1 ;
        }
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        if (Y<X){
            int cnt = 0 ;
            for(int i = pos+1 ; i <= m[sufmax[pos+1]] ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt ;
        }
        else{
            int cnt = 0 ;
            for(int i = pos+1 ; i <= m[sufmax[pos+1]] ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt-1 ;
        }
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }
    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        if (Y<X){
            ans[pos] = 0 ;
        }
        else{
            int cnt = 0 ;
            for(int i = pos+1 ; i <= n ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt ;
        }
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        if (Y<X){
            int cnt = 0 ; int maxi = 0 ;
            for(int i = pos+1 ; i <= m[sufmax[pos+1]] ; i++){
                if (a[i]>X && a[i] > maxi){
                    maxi = a[i] ;
                    cnt++ ;
                }
                else if (maxi > a[i]){
                    break ;
                }
                else continue ;
            }
            ans[pos] = cnt ;
        }
        else{
            int cnt = 0 ;
            for(int i = pos+1 ; i <= m[sufmax[pos+1]] ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt-1 ;
        }
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        int cnt = 0 ; int until = 0 ;
        for(int i = pos+1 ; i <= n ; i++){
            if (a[i]>X && a[i]<Y && until < a[i]){
                cnt++ ;
                until = a[i] ;
            }
            if (a[i]>Y) break ;
        }
        if (Y > X) cnt-- ;
        ans[pos] = cnt ;
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        int cnt = 0 ; int until = 0 ;
        for(int i = pos+1 ; i <= n ; i++){
            if (a[i]>X && a[i]<Y && until < a[i]){
                cnt++ ;
                until = a[i] ;
            }
            if (a[i]>Y) break ;
        }
        if (Y > X) cnt-- ;
        ans[pos] = cnt ;
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    for(int i = 1 ; i <= n ; i++){
        cout << ans[i] << sp ;
    }
    cout << el ;
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        if (Y<X){
            ans[pos] = 0 ;
        }
        else{
            int cnt = 0 ;
            for(int i = pos+1 ; i <= m[sufmax[pos+1]] ; i++){
                if (a[i]>X && a[i]<Y) cnt++ ;
                else break ;
            }
            ans[pos] = cnt-1 ;
        }
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


/*
 		Resilience is the greatest of all virtues.
 		Code by rajiv
 */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define CASET  int t;cin>>t;while(t--)
 #define FORN(i,x,y) for(int i=x;i<y;i++)
 #define MAXN 100005
 #define INF 10000000010
 #define MOD 1000000007
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define PI 3.141592653589793

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef vector<VPII > VVPII ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    int n ; cin >> n ;
    int a[n+2] ;
    map<int,int> m ;
    FORN(i,1,n+1){
        cin >> a[i] ;
        m[a[i]] = i ;
    }
    int premax[n+2] , sufmax[n+2] ;
    int ans[n+2] ;
    premax[0] = -1 ; sufmax[n+1] = -1 ;
    for(int i = 1 ; i <= n ; i++){
        premax[i] = max(a[i],premax[i-1]) ;
    }
    for(int i = n ; i >= 1 ; i--){
        sufmax[i] = max(a[i],sufmax[i+1]) ;

    }

    for(int pos = 1 ; pos <= n ; pos++){
        int X = premax[pos-1] ;
        int Z = sufmax[pos+1] ;
        int Y = a[pos] ;
        int cnt = 0 ; int until = 0 ;
        for(int i = pos+1 ; i <= n ; i++){
            if (a[i]>X && a[i]<Y && until < a[i]){
                cnt++ ;
                until = a[i] ;
            }
            else if (until > a[i]) break ;
            else if (a[i] >Y )  break   ;
            else {
                continue ;
            }
        }
        if (Y > X) cnt-- ;
        ans[pos] = cnt ;
    }
    int maxi = a[1] ; int idx = 1 ;
    for(int i = 2 ; i <= n ; i++){
        if (ans[idx] < ans[i]){
            idx = i ;
            maxi = a[idx] ;
        }
        else if (ans[idx]==ans[i]){
            if (maxi > a[i]){
                idx = i ;
                maxi = a[idx] ;
            }
        }
    }
    cout << maxi << el ;
 }


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll a[100005];
ll dp[100005];
ll bit[100005];
ll n;
ll vis[100005];
void upd(ll N)
{
    while(N<=n)
    {
        bit[N]+=1;
        N+=N & (-N);
    }
}

ll go(ll n)
{
    ll re=0;
    while(n)
    {
        re+=bit[n];
        n-=n & (-n);
    }
    return re;
}
int main()
{
   ll i,j,k,m,t;
   cin>>n;
   for(i=1;i<=n;i++)
   {
       cin>>a[i];
   }
   if(n==1)
   {
       cout<<1;
       return 0;
   }
   dp[1]=0;
   ll ma=a[1];
   for(i=2;i<=n;i++)
   {  dp[i]=dp[i-1];
       if(ma<a[i])
       {
           ma=a[i];
           dp[i]+=1;
       }
   }
   ll par[n+1];
   memset(par,-1,sizeof(par));
   for(i=1;i<=n;i++)
   {
       if(i==1)
        {
           upd(a[i]);
        }
       else
       {
           ll cur=go(n)-go(a[i]);
           if(cur>1)
           {
               upd(a[i]);
           }
           else if(cur==1)
           {
               if(a[i]<a[i-1])
               {
                   vis[i-1]+=1;
                   par[i]=i-1;
               }
               else
               {
                  ll id=i-1;
                   while(1)
                   {
                       if(par[id]==-1)
                       {
                           vis[id]+=1;
                           par[i]=id;
                           break;
                       }
                       else if(a[par[id]]>a[i])
                       {
                           vis[par[id]]+=1;
                           par[i]=par[id];
                           break;
                       }
                       else {
                       id=par[id];
                       }
                   }
               }
           upd(a[i]);
           }
       }
   }
   ll re=-1;
   ll ans=-1;
   for(i=1;i<=n;i++)
   {
       ll pk=dp[n]-dp[i]+dp[i-1];
       pk+=vis[i];
       if(re<pk)
       {
           re=pk;
           ans=a[i];
       }
       else if(re==pk)
       {
           ans=min(ans,a[i]);
       }
   }
   cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mo 1000000007
ll a[300005];
ll dp[300005][50];
ll inc[100005];
ll bit[100005];
void upd(ll id)
{
    while(id<=100000)
    {
        bit[id]+=1;
        id+=id & (-id);
    }
}
ll go(ll id)
{
    ll re=0;
    while(id)
    {
        re+=bit[id];
        id-=id & (-id);
    }
    return re;
}
int main()
{
    ll i,j,k,n,m,t,s,l;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];

    for(i=0;i<n;i++)
        dp[i][0]=a[i];
    ll sz=log2(n)+1;
    for(i=1;i<=sz;i++)
    {
        for(j=0;j<=(n-(1<<i));j++)
        {
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }

    ll ma=0;
    upd(a[0]);
    for(i=1;i<n;i++)
    {
        ll cur=a[i];
        ll now=go(cur-1);
        if(now==i || now<(i-1))
        {
            upd(cur);
            continue;
        }
        upd(cur);
        ll lo=0,hi=i-1,lt=i;
        while(lo<=hi)
        {
            ll mi=(lo+hi)/2;
            ll len=log2(i-mi);
            ll cv=max(dp[mi][len],dp[i-(1<<len)][len]);
            if(cv<a[i])
            {
                lt=mi;
                hi=mi-1;
            }
            else lo=mi+1;
        }
       if(lt!=0)
        inc[lt-1]+=1;
    }
    ma=0;
    for(i=0;i<n;i++)
    {
        ma=max(ma,inc[i]);
    }
    if(ma==1)
    {
        if(inc[0]==1)
            inc[0]=0;
    }
    ll re=LLONG_MAX;
    for(i=0;i<n;i++)
    {
        if(inc[i]==ma)
            re=min(re,a[i]);
    }
    if(re==LLONG_MAX)
    {
        for(i=0;i<n;i++)
        {
            if(inc[i]==0)
                re=min(re,a[i]);
        }
    }
    cout<<re<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll a[100005];
ll dp[100005];
ll bit[100005];
ll n;
ll vis[100005];
void upd(ll N)
{
    while(N<=n)
    {
        bit[N]+=1;
        N+=N & (-N);
    }
}

ll go(ll n)
{
    ll re=0;
    while(n)
    {
        re+=bit[n];
        n-=n & (-n);
    }
    return re;
}
int main()
{
   ll i,j,k,m,t;
   cin>>n;
   for(i=1;i<=n;i++)
   {
       cin>>a[i];
   }
   if(n==1)
   {
       cout<<1;
       return 0;
   }
   dp[1]=0;
   ll ma=a[1];
   for(i=2;i<=n;i++)
   {  dp[i]=dp[i-1];
       if(ma<a[i])
       {
           ma=a[i];
           dp[i]+=1;
       }
   }
   ll par[n+1];
   memset(par,-1,sizeof(par));
   for(i=1;i<=n;i++)
   {
       if(i==1)
        {
           upd(a[i]);
        }
       else
       {
           ll cur=go(n)-go(a[i]);
           if(cur>1)
           {
               upd(a[i]);
           }
           else if(cur==1)
           {
               if(a[i]<a[i-1])
               {
                   vis[i-1]+=1;
                   par[i]=i-1;
               }
               else
               {
                  ll id=i-1;
                   while(1)
                   {
                       if(par[id]==-1)
                       {
                           vis[id]+=1;
                           par[i]=id;
                           break;
                       }
                       else if(a[par[id]]>a[i])
                       {
                           vis[par[id]]+=1;
                           par[i]=par[id];
                           break;
                       }
                       else {
                       id=par[id];
                       }
                   }
               }
           upd(a[i]);
           }
       }
   }
   ll re=-1;
   ll ans=LLONG_MAX;
   for(i=1;i<=n;i++)
   {
       ll pk=dp[n]-dp[i]+dp[i-1];
       pk+=vis[i];
       if(i==1 && par[2]==1)
       {
           pk-=1;
       }
       if(re<pk)
       {
           re=pk;
           ans=a[i];
       }
       else if(re==pk)
       {
           ans=min(ans,a[i]);
       }
   }

   cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mo 1000000007
ll a[300005];
ll dp[300005][50];
ll inc[100005];
ll bit[100005];
void upd(ll id)
{
    while(id<=100000)
    {
        bit[id]+=1;
        id+=id & (-id);
    }
}
ll go(ll id)
{
    ll re=0;
    while(id)
    {
        re+=bit[id];
        id-=id & (-id);
    }
    return re;
}
int main()
{
    ll i,j,k,n,m,t,s,l;
    cin>>n;

    for(i=0;i<n;i++)
        cin>>a[i];
    if(n==1)
    {
        cout<<"1\n";
        return 0;
    }
    for(i=0;i<n;i++)
        dp[i][0]=a[i];
    ll sz=log2(n)+1;
    for(i=1;i<=sz;i++)
    {
        for(j=0;j<=(n-(1<<i));j++)
        {
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }

    ll ma=0;
    upd(a[0]);
    for(i=1;i<n;i++)
    {
        ll cur=a[i];
        ll now=go(cur-1);
        if(now==i || now<(i-1))
        {
            upd(cur);
            continue;
        }
        upd(cur);
        ll lo=0,hi=i-1,lt=i;
        while(lo<=hi)
        {
            ll mi=(lo+hi)/2;
            ll len=log2(i-mi);
            ll cv=max(dp[mi][len],dp[i-(1<<len)][len]);
            if(cv<a[i])
            {
                lt=mi;
                hi=mi-1;
            }
            else lo=mi+1;
        }
       if(lt!=0)
        inc[lt-1]+=1;
    }
    ma=0;
    for(i=0;i<n;i++)
    {
        ma=max(ma,inc[i]);
    }
    ll re=LLONG_MAX;
    if(ma==0)
    {
        for(i=1;i<n;i++)
        {
            re=min(re,a[i]);
        }
        cout<<re;
        return 0;
    }
    if(ma==1)
    {
        if(inc[0]==1)
            inc[0]=0;
    }

    for(i=0;i<n;i++)
    {
        if(inc[i]==ma)
            re=min(re,a[i]);
    }
    if(re==LLONG_MAX)
    {
        for(i=0;i<n;i++)
        {
            if(inc[i]==0)
                re=min(re,a[i]);
        }
    }
    cout<<re<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mo 1000000007
ll a[300005];
ll dp[300005][50];
ll inc[100005];
ll bit[100005];
void upd(ll id)
{
    while(id<=100000)
    {
        bit[id]+=1;
        id+=id & (-id);
    }
}
ll go(ll id)
{
    ll re=0;
    while(id)
    {
        re+=bit[id];
        id-=id & (-id);
    }
    return re;
}
int main()
{
    ll i,j,k,n,m,t,s,l;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];

    for(i=0;i<n;i++)
        dp[i][0]=a[i];
    ll sz=log2(n)+1;
    for(i=1;i<=sz;i++)
    {
        for(j=0;j<=(n-(1<<i));j++)
        {
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }

    ll ma=0;
    upd(a[0]);
    for(i=1;i<n;i++)
    {
        ll cur=a[i];
        ll now=go(cur-1);
        if(now==i || now<(i-1))
        {
            upd(cur);
            continue;
        }
        upd(cur);
        ll lo=0,hi=i-1,lt=i;
        while(lo<=hi)
        {
            ll mi=(lo+hi)/2;
            ll len=log2(i-mi);
            ll cv=max(dp[mi][len],dp[i-(1<<len)][len]);
            if(cv<a[i])
            {
                lt=mi;
                hi=mi-1;
            }
            else lo=mi+1;
        }
       if(lt!=0)
        inc[lt-1]+=1;
    }
    ma=0;
    for(i=0;i<n;i++)
    {
        ma=max(ma,inc[i]);
    }
    ll re=LLONG_MAX;
    for(i=0;i<n;i++)
    {
        if(inc[i]==ma)
            re=min(re,a[i]);
    }
    cout<<re<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mo 1000000007
ll a[300005];
ll dp[300005][50];
ll inc[100005];
ll bit[100005];
void upd(ll id)
{
    while(id<=100000)
    {
        bit[id]+=1;
        id+=id & (-id);
    }
}
ll go(ll id)
{
    ll re=0;
    while(id)
    {
        re+=bit[id];
        id-=id & (-id);
    }
    return re;
}
int main()
{
    ll i,j,k,n,m,t,s,l;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];

    for(i=0;i<n;i++)
        dp[i][0]=a[i];
    ll sz=log2(n)+1;
    for(i=1;i<=sz;i++)
    {
        for(j=0;j<=(n-(1<<i));j++)
        {
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }

    ll ma=0;
    upd(a[0]);
    for(i=1;i<n;i++)
    {
        ll cur=a[i];
        ll now=go(cur-1);
        if(now==i || now<(i-1))
        {
            upd(cur);
            continue;
        }
        upd(cur);
        ll lo=0,hi=i-1,lt=i;
        while(lo<=hi)
        {
            ll mi=(lo+hi)/2;
            ll len=log2(i-mi);
            ll cv=max(dp[mi][len],dp[i-(1<<len)][len]);
            if(cv<a[i])
            {
                lt=mi;
                hi=mi-1;
            }
            else lo=mi+1;
        }
       if(lt!=0)
        inc[lt-1]+=1;
    }
    ma=0;
    for(i=0;i<n;i++)
    {
        ma=max(ma,inc[i]);
    }
    ll re=LLONG_MAX;
    if(ma==0)
    {
        for(i=1;i<n;i++)
        {
            re=min(re,a[i]);
        }
        cout<<re;
        return 0;
    }
    if(ma==1)
    {
        if(inc[0]==1)
            inc[0]=0;
    }

    for(i=0;i<n;i++)
    {
        if(inc[i]==ma)
            re=min(re,a[i]);
    }
    if(re==LLONG_MAX)
    {
        for(i=0;i<n;i++)
        {
            if(inc[i]==0)
                re=min(re,a[i]);
        }
    }
    cout<<re<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll a[100005];
ll dp[100005];
ll bit[100005];
ll n;
ll vis[100005];
void upd(ll N)
{
    while(N<=n)
    {
        bit[N]+=1;
        N+=N & (-N);
    }
}

ll go(ll n)
{
    ll re=0;
    while(n)
    {
        re+=bit[n];
        n-=n & (-n);
    }
    return re;
}
int main()
{
   ll i,j,k,m,t;
   cin>>n;
   for(i=1;i<=n;i++)
   {
       cin>>a[i];
   }
   if(n==1)
   {
       cout<<1;
       return 0;
   }
   dp[1]=0;
   ll ma=a[1];
   for(i=2;i<=n;i++)
   {  dp[i]=dp[i-1];
       if(ma<a[i])
       {
           ma=a[i];
           dp[i]+=1;
       }
   }
   ll par[n+1];
   memset(par,-1,sizeof(par));
   for(i=1;i<=n;i++)
   {
       if(i==1)
        {
           upd(a[i]);
        }
       else
       {
           ll gt=go(a[i]);
           ll cur=i-gt-1;
           if(cur>1 || cur==0)
           {
               upd(a[i]);
           }
           else if(cur==1)
           {
               if(a[i]<a[i-1])
               {
                   vis[i-1]+=1;
                   par[i]=i-1;
               }
               else
               {
                  ll id=i-1;
                   while(1)
                   {
                       if(par[id]==-1)
                       {
                           vis[id]+=1;
                           par[i]=id;
                           break;
                       }
                       else if(a[par[id]]>a[i])
                       {
                           vis[par[id]]+=1;
                           par[i]=par[id];
                           break;
                       }
                       else {
                       id=par[id];
                       }
                   }
               }
           upd(a[i]);
           }
       }
   }
   ll re=-1;
   ll ans=LLONG_MAX;
   for(i=1;i<=n;i++)
   {
       ll pk=dp[n]-dp[i]+dp[i-1];
       pk+=vis[i];
       if(i==1 && par[2]==1)
       {
           pk-=1;
       }
       else if(i==1 && a[i+1]>a[i])
       {
           pk-=1;
       }
       if(re<pk)
       {
           re=pk;
           ans=a[i];
       }
       else if(re==pk)
       {
           ans=min(ans,a[i]);
       }
   }

   cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mo 1000000007
ll a[300005];
ll dp[300005][50];
ll inc[100005];
ll bit[100005];
ll vis[100005];
void upd(ll id)
{
    while(id<=100000)
    {
        bit[id]+=1;
        id+=id & (-id);
    }
}
ll go(ll id)
{
    ll re=0;
    while(id)
    {
        re+=bit[id];
        id-=id & (-id);
    }
    return re;
}
int main()
{
    ll i,j,k,n,m,t,s,l;
    cin>>n;

    for(i=0;i<n;i++)
        cin>>a[i];
    if(n==1)
    {
        cout<<"1\n";
        return 0;
    }
    for(i=0;i<n;i++)
        dp[i][0]=a[i];
    ll sz=log2(n)+1;
    for(i=1;i<=sz;i++)
    {
        for(j=0;j<=(n-(1<<i));j++)
        {
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }

    upd(a[0]);
    ll cn=1;
    vis[0]=1;
    for(i=1;i<n;i++)
    {
        ll cur=a[i];
        ll now=go(cur-1);
        if(now==i)
        {
            vis[i]=1;
            cn+=1;
            upd(cur);
            continue;
        }
        if(now<(i-1))
        {
            vis[i]=-1;
            upd(cur);
            continue;
        }
        upd(cur);
        ll lo=0,hi=i-1,lt=i;
        while(lo<=hi)
        {
            ll mi=(lo+hi)/2;
            ll len=log2(i-mi);
            ll cv=max(dp[mi][len],dp[i-(1<<len)][len]);
            if(cv<a[i])
            {
                lt=mi;
                hi=mi-1;
            }
            else lo=mi+1;
        }
       if(lt!=0)
        inc[lt-1]+=1;
    }
   ll ma=-1,re;
   for(i=0;i<n;i++)
   {
       {
           ll now=cn;
           if(vis[i]==1) now-=1;
           now+=inc[i];
           if(ma<now)
           {
               ma=now;
               re=a[i];
           }
           else if(ma==now)
           {
               re=min(re,a[i]);
           }
       }
   }
   cout<<re<<"\n";

}