#include <iostream>

using namespace std;
int n,v[100003],m1,m2,sol,nr,act;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
    }
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    m1=v[1];
    sol=m1;
    nr=0;
    act=0;
    for(int i=2;i<=n;++i)
    {
        if(v[i]<m1&&v[i]<m2) continue;
        if(v[i]<m1&&v[i]>m2)
        {
            m2=v[i];
            ++act;
            continue;
        }
        if(v[i]>m1)
        {
            if(act>nr)
            {
                nr=act;
                sol=m1;
            }
            act=0;
            m2=m1;
            m1=v[i];
        }
    }
    for(int i=1;i<n;++i)
    {
        if(sol==n&&v[i]==sol&&v[i+1]==n-1)
        {
            sol=n-1;
            break;
        }
    }
    int ma=0,ca1=0;
    for(int i=1;i<=n;++i)
    {
        if(v[i]>ma)
        {
            ++ca1;
            ma=v[i];
        }
    }
    ma=0;
    int ca2=0;
    for(int i=1;i<=n;++i)
    {
        if(v[i]==sol) continue;
        if(v[i]>ma)
        {
            ++ca2;
            ma=v[i];
        }
    }
    if(ca1==ca2||ca2==ca1-1)
    {
        cout<<1;
        return 0;
    }
    cout<<sol;
    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
	<ActiveTarget name="Debug" />
	<File name="main.cpp" open="1" top="1" tabpos="1">
		<Cursor position="0" topLine="0" />
	</File>
</CodeBlocks_layout_file>


#include <iostream>

using namespace std;
int n,v[200003],t[200003],mare,k,act,usu,sol;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
        if(v[i]>mare)
        {
            mare=v[i];
            t[++k]=i;
        }
    }
    sol=k;
    t[++k]=n+1;
    mare=0;
    for(int i=1;i<=k;++i)
    {
        act=0;
        for(int j=t[i]+1;j<t[i+1];++j)
        {
            if(v[j]>mare)
            {
                ++act;
                mare=v[j];
            }
        }
        mare=v[t[i]];
        usu=max(usu,act);
    }
    if(sol==n)
    {
        cout<<sol-1;
        return 0;
    }
    cout<<max(sol,sol+usu-1);
    return 0;
}


#include <iostream>

using namespace std;
int n,v[200003],t[200003],mare,k,act,usu,sol;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
        if(v[i]>mare)
        {
            mare=v[i];
            t[++k]=i;
        }
    }
    sol=k;
    t[++k]=n+1;
    mare=0;
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    for(int i=1;i<=k;++i)
    {
        act=0;
        for(int j=t[i]+1;j<t[i+1];++j)
        {
            if(v[j]>mare)
            {
                ++act;
                mare=v[j];
            }
        }
        mare=v[t[i]];
        if(act>usu)
        {
            usu=act;
            sol=v[t[i]];
        }
    }
    if(n-1==usu+k-1-1)
    {
        cout<<1;
        return 0;
    }
    cout<<sol;
    return 0;
}


#include <iostream>
using namespace std;
int n,x,m,sec,ans=1,v[100100];
int main()
{
	cin>>n;
	for(int i=0;i<n;++i)
	{
		cin>>x;
		if(x>m)
		{
		    v[x]=1;
		    sec=m;
		    m=x;
		}
		else if(x>sec)
		{
		    --v[m];
            sec=x;
		}
	}
	for(int i=1;i<=n;++i) if(v[i]<v[ans]) ans=i;
	cout<<ans;
    return 0;
}


#include <iostream>

using namespace std;
int n,v[100003],m1,m2,sol,nr,act;
int main()
{
    cin>>n;
    cin>>v[1];
    int ok=1;
    for(int i=2;i<=n;++i)
    {
        cin>>v[i];
        if(v[i]>v[i-1]) ok=0;
    }
    if(ok)
    {
        cout<<1;
        return 0;
    }
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    m1=v[1];
    sol=m1;
    nr=0;
    act=0;
    for(int i=2;i<=n;++i)
    {
        if(v[i]<m1&&v[i]<m2) continue;
        if(v[i]<m1&&v[i]>m2)
        {
            m2=v[i];
            ++act;
            continue;
        }
        if(v[i]>m1)
        {
            if(act>nr)
            {
                nr=act;
                sol=m1;
            }
            act=0;
            m2=m1;
            m1=v[i];
        }
    }
    for(int i=1;i<n;++i)
    {
        if(sol==n&&v[i]==sol&&v[i+1]==n-1)
        {
            sol=n-1;
            break;
        }
    }
    cout<<sol;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int pos,ans,n,a[100010];
int c[100010];
int lowbit(int x)
{
    return x & (-x);
}
void updata(int p,int x)
{
    for (int i=p;i<=n;i+=lowbit(i))
    {
        c[i] += x;
    }
}
int getsum(int x)
{
    int tmp = 0;
    for (int i=x;i>=1;i-= lowbit(i))
    {
        tmp += c[i];
    }
    return tmp;
}
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    for (int i=n;i>=1;i--)
    {
        updata(a[i],1);
        if (ans < getsum(a[i]))
        {
            ans = getsum(a[i]);
            pos = a[i];
        }
        else
            if (ans == getsum(a[i]) && pos > a[i])
                pos = a[i];
    }
    printf("%d\n", pos);

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int n,ans,m1,m2,x,a[100010];
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d", &x);
        if (x>m1)
        {
            a[x] = 1;m2 = m1;m1 = x;
        }
        else if (x>m2)
        {
            a[m1]--;m2 = x;
        }
    }
    ans = 1;
    for (int i=1;i<=n;i++) if (a[i] < a[ans]) ans = i;
    printf("%d\n", ans);

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int pos,ans,n,x,a[100010];
int c[100010];
int lowbit(int x)
{
    return x & (-x);
}
void updata(int p,int x)
{
    for (int i=p;i<=n;i+=lowbit(i))
    {
        c[i] += x;
    }
}
int getsum(int x)
{
    int tmp = 0;
    for (int i=x;i>=1;i-= lowbit(i))
    {
        tmp += c[i];
    }
    return tmp;
}
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    pos = n+1;ans = 0;
    for (int i=n;i>=1;i--)
    {
        updata(a[i],1);
        x = getsum(a[i]) -1;
        if (ans < x)
        {
            ans = x;
            pos = a[i];
        }
        else
            if (ans == x && pos > a[i])
                pos = a[i];
    }
    printf("%d\n", pos);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int mx,ans,n,x,a[100010];
int c[100010];
int p[100010];
int lowbit(int x)
{
    return x & (-x);
}
void updata(int p,int x)
{
    for (int i=p;i<=n;i+=lowbit(i))
    {
        c[i] += x;
    }
}
int getsum(int x)
{
    int tmp = 0;
    for (int i=x;i>=1;i-= lowbit(i))
    {
        tmp += c[i];
    }
    return tmp;
}
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    mx = 0;
    for (int i=1;i<=n;i++)
    {
        updata(a[i],1);
        x = i - getsum(a[i]);
        if (x == 1) p[mx]++;
        mx = max(mx, a[i]);
    }
    mx = 0;ans = n+1;
    for (int i=1;i<=n;i++)
        if (mx < p[i])
        {
            mx = p[i];
            ans = i;
        }
        else
            if (mx == p[i] && ans > i)
                ans = i;
    printf("%d\n", ans);
}


#include <iostream>
#include <stdio.h>
using namespace std;
int a[100010], max1[100010], max2[100010], c[100010], tg[100010], pos[100010];

int main()
{
    //freopen("input.inp","r",stdin);
    int n;
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
        pos[a[i]]=i;
    }

    for (int i=1;i<=n;i++){
        if (max1[i-1]>a[i]){
            max1[i]=max1[i-1];
            if (a[i]>max2[i-1])
                max2[i]=a[i];
            else
                max2[i]=max2[i-1];
        }
        else{
            tg[i]=1;
            max1[i]=a[i];
            max2[i]=max1[i-1];
        }
    }

    for (int i=1;i<=n;i++){
        if (a[i]<max1[i-1] && a[i]>max2[i-1]){
            c[max1[i-1]]++;
        }
    }

    int maxx=-1,ans=1;
    for (int i=1;i<=n;i++){
        if (c[i]-tg[pos[i]]>maxx){
            maxx=c[i]-tg[pos[i]];
            ans=i;
        }
    }

    cout<<ans;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
int a[100010], max1, max2;

int main()
{
    //freopen("input.inp","r",stdin);
    int n,k;
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>k;
        if (k>max1){
            max2=max1;
            max1=k;
            a[k]--;
        }
        else{
            if (k>max2){
                a[max1]++;
                max2=k;
            }
        }
    }
    int ans=1;
    for (int i=1;i<=n;i++){
        if (a[i]>a[ans]){
            ans=i;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
int a[100010], max1[100010], max2[100010], c[100010], tg[100010], pos[100010];

int main()
{
    //freopen("input.inp","r",stdin);
    int n;
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
        pos[a[i]]=i;
    }

    for (int i=1;i<=n;i++){
        if (max1[i-1]>a[i]){
            max1[i]=max1[i-1];
            if (a[i]>max2[i-1])
                max2[i]=a[i];
            else
                max2[i]=max2[i-1];
        }
        else{
            tg[i]=1;
            max1[i]=a[i];
            max2[i]=max1[i-1];
        }
    }

    for (int i=1;i<=n;i++){
        if (a[i]<max1[i-1] && a[i]>max2[i-1]){
            c[max1[i-1]]++;
        }
    }

    int maxx=-2,ans=1;
    for (int i=1;i<=n;i++){
        if (c[i]-tg[pos[i]]>maxx){
            maxx=c[i]-tg[pos[i]];
            ans=i;
        }
    }

    cout<<ans;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

int cmp(const void * x, const void * y) {
#define datatype lint
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}
const int N = 1e6 + 10;
struct tree {
    int l, r;//左右儿子节点编号
    int num;//当前节点的数字
    int s;//以当前节点为根的子树的节点数
    int sum;//当前节点的数字的数量
    int rnd;//随机优先级
} tr[N]; //下标为节点编号
int n, rt, cnt, t1, t2;
void updata(int &k) {
    int &l = tr[k].l, &r = tr[k].r;
    tr[k].s = tr[l].s + tr[r].s + tr[k].sum;
}
void lturn(int &k) {
    int t = tr[k].r;
    tr[k].r = tr[t].l;
    tr[t].l = k;
    tr[t].s = tr[k].s;
    updata(k);
    k = t;
}
void rturn(int &k) {
    int t = tr[k].l;
    tr[k].l = tr[t].r;
    tr[t].r = k;
    tr[t].s = tr[k].s;
    updata(k);
    k = t;
}
void insert(int &k, int x) {
    if(!k) {
        k = ++cnt;
        tr[k].num = x;
        tr[k].s = 1;
        tr[k].sum++;
        tr[k].rnd = rand();
        return;
    }

    tr[k].s++;
    int &l = tr[k].l, &r = tr[k].r;

    if(x < tr[k].num) {
        insert(l, x);

        if(tr[l].rnd < tr[k].rnd) rturn(k);
    } else if(x > tr[k].num) {
        insert(r, x);

        if(tr[r].rnd < tr[k].rnd) lturn(k);
    } else {
        tr[k].sum++;
        return;
    }
}
void del(int &k, int x) {
    if(!k) return;

    int &l = tr[k].l, &r = tr[k].r;

    if(x == tr[k].num) {
        if(tr[k].sum > 1) {
            tr[k].sum--;
            tr[k].s--;
            return;
        }

        if(l * r == 0) k = l + r;
        else {
            if(tr[l].rnd < tr[r].rnd) rturn(k);
            else lturn(k);

            del(k, x);
        }
    } else {
        tr[k].s--;

        if(x > tr[k].num) del(r, x);
        else del(l, x);
    }
}
int find1(int &k, int x) {
    if(!k) return 0;

    int &l = tr[k].l, &r = tr[k].r;

    if(tr[k].num == x) return tr[l].s + 1;

    if(tr[k].num > x) return find1(l, x);

    if(tr[k].num < x) return tr[l].s + tr[k].sum + find1(r, x);
}
int find2(int &k, int x) {
    if(!k) return 0;

    int &l = tr[k].l, &r = tr[k].r;

    if(tr[l].s + 1 <= x && tr[l].s + tr[k].sum >= x) return tr[k].num;

    if(tr[l].s >= x) return find2(l, x);

    if(tr[l].s + tr[k].sum < x) return find2(r, x - tr[l].s - tr[k].sum);
}
void pred(int &k, int x) {
    if(!k) return;

    int &l = tr[k].l, &r = tr[k].r;

    if(tr[k].num < x) {
        t1 = tr[k].num;
        pred(r, x);
    } else pred(l, x);
}
void succ(int &k, int x) {
    if(!k) return;

    int &l = tr[k].l, &r = tr[k].r;

    if(tr[k].num > x) {
        t2 = tr[k].num;
        succ(l, x);
    } else succ(r, x);
}

int p[100005], a[100005], f[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> a[i];
        p[a[i]] = i;
    }

    for(int i = n - 1; i >= 0; i--) {
        f[a[i]] = find1(rt, a[i]);
        insert(rt, a[i]);
    }

    int ans = 1;

    for(int i = 2; i <= n; i++) {
        if(f[i] > f[ans]) ans = i;
    }

    cout << ans << endl;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

int cmp(const void * x, const void * y) {
#define datatype lint
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

int a[100005], f[100005];
bool p[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 0; i < n; i++) cin >> a[i];

    memset(f, 0, sizeof(f));
    int max1 = a[0], max2 = a[0], record = 0;

    for(int i = 1; i < n; i++) {
        if(max1 < a[i]) {
            max2 = max1;
            max1 = a[i];
            continue;
        }

        if(max2 < a[i]) {
            f[max1]++;
            max2 = a[i];
        }

    }

    int ans = 1;

    for(int i = 2; i <= n; i++) {
        if(f[i] > f[ans]) ans = i;
    }

    cout << ans << endl;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

int cmp(const void * x, const void * y) {
#define datatype lint
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

int a[100005], f[100005];
bool p[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    if (n <= 2) {
        cout << 1 << endl;
        return 0;
    }
    memset(f, 0, sizeof(f));
    memset(p, false, sizeof(p));
    int max1 = max(a[0], a[1]), max2 = min(a[0], a[1]), record = 0;
    p[a[0]] = true;
    if (a[0] < a[1]) p[a[1]] = true;
    else f[a[0]]++;
    for (int i = 2; i < n; i++) {
        if (max1 < a[i]) {
            max2 = max1;
            max1 = a[i];
            p[a[i]] = true;
            continue;
        }
        if (max2 < a[i]) {
            f[max1]++;
            max2 = a[i];
        }
    }
    for (int i = 1; i <= n; i++) if (p[i]) f[i]--;
    int ans = 1;
    for (int i = 2; i <= n; i++) {
        if (f[i] > f[ans]) ans = i;
    }
    cout << ans << endl;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

int cmp(const void * x, const void * y) {
#define datatype lint
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

int a[100005], f[100005];
bool p[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 0; i < n; i++) cin >> a[i];

    if(n <= 2) {
        cout << 1 << endl;
        return 0;
    }

    memset(f, 0, sizeof(f));
    memset(p, false, sizeof(p));
    int max1 = max(a[0], a[1]), max2 = min(a[0], a[1]), record = 0;
    p[a[0]] = true;

    if(a[0] < a[1]) p[a[1]] = true;

    for(int i = 2; i < n; i++) {
        if(max1 < a[i]) {
            max2 = max1;
            max1 = a[i];
            p[a[i]] = true;
            continue;
        }

        if(max2 < a[i]) {
            f[max1]++;
            max2 = a[i];
        }
    }

    for(int i = 1; i <= n; i++) if(p[i]) f[i]--;

    int ans = 1;

    for(int i = 2; i <= n; i++) {
        if(f[i] > f[ans]) ans = i;
    }

    cout << ans << endl;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

int cmp(const void * x, const void * y) {
#define datatype lint
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

int a[100005], f[100005];
bool p[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 0; i < n; i++) cin >> a[i];

    memset(f, 0, sizeof(f));
    int max1 = a[0], max2 = 0, record = 0;

    for(int i = 1; i < n; i++) {
        if(max1 < a[i]) {
            max2 = max1;
            max1 = a[i];
            continue;
        }

        if(max2 < a[i]) {
            f[max1]++;
            max2 = a[i];
        }

    }

    int ans = 1;

    for(int i = 2; i <= n; i++) {
        if(f[i] > f[ans]) ans = i;
    }

    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax,maxx=0,self[Tmax];
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=-2;
	for(i=n;i>=2;i--)
	{
		if((sum(cr,data[i])-self[i])>ans)
		{
			ans=sum(cr,data[i])-self[i];
			num=data[i];
		}
		else if((sum(cr,data[i])-self[i])==ans) num=min(num,data[i]);
		//printf("i=%d tmpans=%d ans=%d num=%d\n",i,sum(cr,data[i])-self[i],ans,num);
		if(v[i]==true) add(cr,data[i],1);
	}
	if(data[1]>data[2])
	{
		if((sum(cr,data[1])-1)>ans)
		{
			ans=sum(cr,data[1])-1;
			num=data[1];
		}
		else if((sum(cr,data[1])-1)==ans) num=min(num,data[1]);
	}
	else
	{
		if(-1>ans)
		{
			ans=1;
			num=data[1];
		}
		else if(-1==ans) num=min(num,data[1]);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
		maxx=max(maxx,data[i]);
		if(maxx==data[i]) self[i]=1;
	}
	init();
	work();/*
	for(i=1;i<=n;i++) printf("%2d ",data[i]);
	printf("\n");
	for(i=1;i<=n;i++) printf("%2d ",v[i]);
	printf("\n");
	for(i=1;i<=n;i++) printf("%2d ",self[i]);
	printf("\n");*/
	if(num==0) printf("%d",minn);
	else if(num==data[1]&&ans==1&&data[2]<data[1]) printf("%d",minn);
	else printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans;
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=0;
	for(i=n;i>=1;i--)
	{
		if(sum(cr,data[i])>ans)
		{
			ans=sum(cr,data[i]);
			num=data[i];
		}
		else if(sum(cr,data[i])==ans) num=min(num,data[i]);
		if(v[i]==true) add(cr,data[i],1);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
	}
	if(n==1)
	{
		printf("%d",data[1]);
		return 0;
	}
	init();
	work();
	printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax;
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=0;
	for(i=n;i>=1;i--)
	{
		if(sum(cr,data[i])>ans)
		{
			ans=sum(cr,data[i]);
			num=data[i];
		}
		else if(sum(cr,data[i])==ans) num=min(num,data[i]);
		if(v[i]==true) add(cr,data[i],1);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
	}
	init();
	work();
	if(num==0) printf("%d",minn);
	else if(num==data[1]&&ans==1) printf("%d",minn);
	else printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax,maxx=0,self[Tmax];
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=-2;
	for(i=n;i>=2;i--)
	{
		if((sum(cr,data[i])-self[i])>ans)
		{
			ans=sum(cr,data[i])-self[i];
			num=data[i];
		}
		else if((sum(cr,data[i])-self[i])==ans) num=min(num,data[i]);
		//printf("i=%d tmpans=%d ans=%d num=%d\n",i,sum(cr,data[i])-self[i],ans,num);
		if(v[i]==true) add(cr,data[i],1);
	}
	if(data[1]>data[2])
	{
		if((sum(cr,data[1])-1)>ans)
		{
			ans=sum(cr,data[1])-1;
			num=data[1];
		}
		else if((sum(cr,data[1])-1)==ans) num=min(num,data[1]);
	}
	else
	{
		if(-1>ans)
		{
			ans=1;
			num=data[1];
		}
		else if(-1==ans) num=min(num,data[1]);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
		maxx=max(maxx,data[i]);
		if(maxx==data[i]) self[i]=1;
	}
	init();
	work();/*
	for(i=1;i<=n;i++) printf("%2d ",data[i]);
	printf("\n");
	for(i=1;i<=n;i++) printf("%2d ",v[i]);
	printf("\n");
	for(i=1;i<=n;i++) printf("%2d ",self[i]);
	printf("\n");*/
	printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax;
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=0;
	for(i=n;i>=1;i--)
	{
		if(sum(cr,data[i])>ans)
		{
			ans=sum(cr,data[i]);
			num=data[i];
		}
		else if(sum(cr,data[i])==ans) num=min(num,data[i]);
		if(v[i]==true) add(cr,data[i],1);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
	}
	init();
	work();
	if(num==0) printf("%d",minn);
	else printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax,maxx=0,self[Tmax];
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=0;
	for(i=n;i>=2;i--)
	{
		if((sum(cr,data[i])-self[i])>ans)
		{
			ans=sum(cr,data[i])-self[i];
			num=data[i];
		}
		else if((sum(cr,data[i])-self[i])==ans) num=min(num,data[i]);
		if(v[i]==true) add(cr,data[i],1);
	}
	if(data[1]>data[2])
	{
		if(sum(cr,data[1])>ans)
		{
			ans=sum(cr,data[1]);
			num=data[1];
		}
		else if(sum(cr,data[1])==ans) num=min(num,data[1]);
	}
	else
	{
		if((sum(cr,data[1])-1)>ans)
		{
			ans=sum(cr,data[1])-1;
			num=data[1];
		}
		else if((sum(cr,data[1])-1)==ans) num=min(num,data[1]);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
		maxx=max(maxx,data[i]);
		if(maxx==data[i]) self[i]=1;
	}
	init();
	work();
	if(num==0) printf("%d",minn);
	else if(num==data[1]&&ans==1&&data[2]<data[1]) printf("%d",minn);
	else printf("%d",num);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int Tmax=1e5+5;
int n,data[Tmax],cl[Tmax],cr[Tmax],num,ans,minn=Tmax,maxx=0,self[Tmax];
bool v[Tmax];
int lowbit(int x)
{
	return x&-x;
}
int sum(int *c,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int *c,int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
    return;
}
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if((sum(cl,n)-sum(cl,data[i]))==1) v[i]=true;
		add(cl,data[i],1);
	}
	return;
}
void work()
{
	int i;
	ans=num=-1;
	for(i=n;i>=2;i--)
	{
		if((sum(cr,data[i])-self[i])>ans)
		{
			ans=sum(cr,data[i])-self[i];
			num=data[i];
		}
		else if((sum(cr,data[i])-self[i])==ans) num=min(num,data[i]);
		if(v[i]==true) add(cr,data[i],1);
	}
	if(data[1]>data[2])
	{
		if(sum(cr,data[1])>ans)
		{
			ans=sum(cr,data[1]);
			num=data[1];
		}
		else if(sum(cr,data[1])==ans) num=min(num,data[1]);
	}
	else
	{
		if((sum(cr,data[1])-1)>ans)
		{
			ans=sum(cr,data[1])-1;
			num=data[1];
		}
		else if((sum(cr,data[1])-1)==ans) num=min(num,data[1]);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&data[i]);
		minn=min(minn,data[i]);
		maxx=max(maxx,data[i]);
		if(maxx==data[i]) self[i]=1;
	}
	init();
	work();
	//for(i=1;i<=n;i++) printf("%d ",v[i]);
	if(num==0) printf("%d",minn);
	else if(num==data[1]&&ans==1&&data[2]<data[1]) printf("%d",minn);
	else printf("%d",num);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int N=100009;
struct T
{
    bool r[N];
    int x[N];
    int mx[N];
}m,t;
int n,a[N];
int main()
{
    //����
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    //��ʼ��
    m.r[1]=0;m.x[1]=0;m.mx[1]=a[1];
    for(int i=2;i<=n;i++)
    {
       if(a[i]>m.mx[i-1])
       {
           m.r[i]=1;
           m.mx[i]=a[i];
           m.x[i]=m.x[i-1]+1;
        }
        else
        {
            m.r[i]=0;
            m.mx[i]=m.mx[i-1];
            m.x[i]=m.x[i-1];
         }
     }

     int mn=a[1];
     int mx=0;
     //��ʼ����
     for(int i=1;i<=n-1;i++)
     {
           T t=m;
           //ȥ��a[i]
           t.r[i]=0;if(t.mx[i]==a[i])t.mx[i]=t.mx[i-1];t.x[i]=t.x[i-1];
           if(i==1)t.r[2]=0,t.mx[2]=a[2],t.x[2]=0;
           for(int j=i+1;j<=n;j++)
           if(t.r[j])
           {
                t.x[j]=t.x[j-1]+1;
            }
            else
            if(j!=2)
            {
                 t.mx[j]=t.mx[j-1];
                 if(t.mx[j-1]<a[j])t.r[j]=1,t.mx[j]=a[j];
                 t.x[j]=t.x[j-1]+t.r[j];
             }
            if(t.x[n]>mx||t.x[n]==mx&&mn>a[i])mn=a[i],mx=t.x[n];
            //cout<<i<<"\t"<<t.x[n]<<endl;
     }
     cout<<mn<<endl;
     return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int N=100009;
struct T
{
    bool r[N];
    int x[N];
    int mx[N];
}m,t;
int n,a[N];
int main()
{
    //����
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    //��ʼ��
    m.r[1]=0;m.x[1]=0;m.mx[1]=a[1];
    for(int i=2;i<=n;i++)
    {
       if(a[i]>m.mx[i-1])
       {
           m.r[i]=1;
           m.mx[i]=a[i];
           m.x[i]=m.x[i-1]+1;
        }
        else
        {
            m.r[i]=0;
            m.mx[i]=m.mx[i-1];
            m.x[i]=m.x[i-1];
         }
     }

     int mn=a[1];
     int mx=0;
     //��ʼ����
     for(int i=1;i<=n;i++)
     {
           T t=m;
           //ȥ��a[i]
           t.r[i]=0;if(t.mx[i]==a[i])t.mx[i]=t.mx[i-1];t.x[i]=t.x[i-1];
           if(i==1)t.r[2]=0,t.mx[2]=a[2],t.x[2]=0;
           for(int j=i+1;j<=n;j++)
           if(t.r[j])
           {
                t.x[j]=t.x[j-1]+1;
            }
            else
            if(j!=2)
            {
                 t.mx[j]=t.mx[j-1];
                 if(t.mx[j-1]<a[j])t.r[j]=1,t.mx[j]=a[j];
                 t.x[j]=t.x[j-1]+t.r[j];
             }
            if(t.x[n]>mx||t.x[n]==mx&&mn>a[i])mn=a[i],mx=t.x[n];
            //cout<<i<<"\t"<<t.x[n]<<endl;
     }
     cout<<mn<<endl;
     return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int n,a[100009];
	int x[100009];
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	memset(x,0,sizeof(x));
	int mx1=0,mx2=0;
	for(int i=1;i<=n;i++)
	if(a[i]>mx1)mx2=mx1,mx1=a[i],x[a[i]]--;
	else if(a[i]>mx2)x[mx1]++,mx2=a[i];
	int ans=1;
	for(int i=2;i<=n;i++)if(x[i]>x[ans])ans=i;
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,s[N],q[N],top,ans,cnt[N],c[N];
class bit
{
  public:
  void modify(int x)
  {
    for(;x<=n;x+=x&-x)c[x]++;
  }
  int qry(int x)
  {
    int res=0;
    for(;x;x-=x&-x)res+=c[x];
    return res;
  }
}T;
int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d",&s[i]);
  for(int i=1;i<=n;i++)
  {
    while(q[top]<s[i]&&top)top--;
    if(T.qry(n-s[i]+1)==1)cnt[q[top]]++;
    q[++top]=s[i];T.modify(n-s[i]+1);
  }
  ans=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]>cnt[ans])ans=i;
  printf("%d\n",ans);
  return 0;
}


#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,s[N],q[N],top,ans,cnt[N];
int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d",&s[i]);
  for(int i=1;i<=n;i++)
  {
    while(q[top]<s[i]&&top)top--;
    if(top==1)cnt[q[top]]++;
    q[++top]=s[i];
  }
  ans=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]>cnt[ans])ans=i;
  printf("%d\n",ans);
  return 0;
}


#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,s[N],q[N],top,ans,cnt[N],c[N];
class bit
{
  public:
  void modify(int x)
  {
    for(;x<=n;x+=x&-x)c[x]++;
  }
  int qry(int x)
  {
    int res=0;
    for(;x;x-=x&-x)res+=c[x];
    return res;
  }
}T;
int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d",&s[i]);
  for(int i=1;i<=n;i++)
  {
    while(q[top]<s[i]&&top)top--;
    if(T.qry(n-s[i]+1)==1)cnt[q[top]]++;
    if(!top)cnt[s[i]]--;
    q[++top]=s[i];T.modify(n-s[i]+1);
  }
  ans=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]>cnt[ans])ans=i;
  printf("%d\n",ans);
  return 0;
}


#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,s[N],q[N],top,ans,cnt[N];
int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d",&s[i]);
  for(int i=1;i<=n;i++)
  {
    while(s[q[top]]<s[i]&&top)top--;
    if(top==1)cnt[q[top]]++;
    q[++top]=i;
  }
  ans=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]>cnt[ans])ans=i;
  printf("%d\n",s[ans]);
  return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<int> que;
    int n;
    cin >> n;
    int *countValue = new int[n + 1];
    int *status = new int[n + 1];
    fill(countValue,countValue + n + 1,0);
    que.push(0);
    {
        int tmpValue;
        cin >> tmpValue;
        status[tmpValue] = -1;
        que.push(tmpValue);
    }
    for(int i = 0;i < n - 1;i++) {
        int tmpValue;
        cin >> tmpValue;
        if(tmpValue > que.top()) {
            status[tmpValue] = -1;
            que.push(tmpValue);
            continue;
        }else {
            int tmpTop = que.top();
            que.pop();
            if(tmpValue > que.top()) {
                countValue[tmpTop]++;
                status[tmpValue] = tmpTop;
            }else {
                status[tmpValue] = -2;
            }
            que.push(tmpTop);
            que.push(tmpValue);
        }
    }
    int maxPos = 1;
    for(int i = 2;i <= n;i++) {
        if(countValue[i] > countValue[maxPos]) {
            maxPos = i;
        }
    }
    if(status[maxPos] == -1 && countValue[maxPos] == 1) {
        int minValue = 1;
        for(int i = 2;i <= n;i++) {
            if(status[i] == -2 || status[i] != maxPos) {
                minValue = i;
                break;
            }
        }
        cout << minValue << endl;
    }else {
        cout << maxPos << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<int> que;
    int n;
    cin >> n;
    int *countValue = new int[n + 1];
    fill(countValue,countValue + n + 1,0);
    que.push(0);
    {
        int tmpValue;
        cin >> tmpValue;
        que.push(tmpValue);
    }
    for(int i = 0;i < n - 1;i++) {
        int tmpValue;
        cin >> tmpValue;
        if(tmpValue > que.top()) {
            continue;
        }else {
            int tmpTop = que.top();
            que.pop();
            if(tmpValue > que.top()) {
                countValue[tmpTop]++;
            }
            que.push(tmpTop);
        }
    }
    int maxPos = 1;
    for(int i = 2;i <= n;i++) {
        if(countValue[i] > countValue[maxPos]) {
            maxPos = i;
        }
    }
    cout << maxPos << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<int> que;
    int n;
    cin >> n;
    int *countValue = new int[n + 1];
    int *status = new int[n + 1];
    fill(countValue,countValue + n + 1,0);
    que.push(0);
    {
        int tmpValue;
        cin >> tmpValue;
        status[tmpValue] = -1;
        que.push(tmpValue);
    }
    for(int i = 0;i < n - 1;i++) {
        int tmpValue;
        cin >> tmpValue;
        if(tmpValue > que.top()) {
            status[tmpValue] = -1;
            que.push(tmpValue);
            continue;
        }else {
            int tmpTop = que.top();
            que.pop();
            if(tmpValue > que.top()) {
                countValue[tmpTop]++;
                status[tmpValue] = tmpTop;
            }else {
                status[tmpValue] = -2;
            }
            que.push(tmpTop);
            que.push(tmpValue);
        }
    }
    int maxPos = 1;
    for(int i = 2;i <= n;i++) {
        if(countValue[i] > countValue[maxPos]) {
            maxPos = i;
        }
    }
    if(status[maxPos] == -1 && countValue[maxPos] == 1) {
        int minValue = -1;
        for(int i = 1;i <= n;i++) {
            if(i < maxPos) {
                if(status[i] != -1) {
                    minValue = i;
                    break;
                }
            }else {
                if(status[i] == -2) {
                    minValue = i;
                    break;
                }
            }
        }
        cout << (minValue == -1 ? maxPos : minValue) << endl;
    }else {
        cout << maxPos << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<int> que;
    int n;
    cin >> n;
    int *countValue = new int[n + 1];
    int *status = new int[n + 1];
    fill(countValue,countValue + n + 1,0);
    que.push(0);
    {
        int tmpValue;
        cin >> tmpValue;
        status[tmpValue] = -1;
        que.push(tmpValue);
    }
    for(int i = 0;i < n - 1;i++) {
        int tmpValue;
        cin >> tmpValue;
        if(tmpValue > que.top()) {
            status[tmpValue] = -1;
            que.push(tmpValue);
            continue;
        }else {
            int tmpTop = que.top();
            que.pop();
            if(tmpValue > que.top()) {
                countValue[tmpTop]++;
                status[tmpValue] = tmpTop;
            }else {
                status[tmpValue] = -2;
            }
            que.push(tmpTop);
            que.push(tmpValue);
        }
    }
    int maxPos = 1;
    for(int i = 2;i <= n;i++) {
        if(countValue[i] > countValue[maxPos]) {
            maxPos = i;
        }
    }
    if(countValue[maxPos] == 0 || (status[maxPos] == -1 && countValue[maxPos] == 1)) {
        int minValue = -1;
        for(int i = 1;i <= n;i++) {
            if(i < maxPos) {
                if(status[i] != -1) {
                    minValue = i;
                    break;
                }
            }else {
                if(status[i] == -2) {
                    minValue = i;
                    break;
                }
            }
        }
        cout << (minValue == -1 ? maxPos : minValue) << endl;
    }else {
        cout << maxPos << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<int> que;
    int n;
    cin >> n;
    int *countValue = new int[n + 1];
    int *status = new int[n + 1];
    fill(countValue,countValue + n + 1,0);
    que.push(0);
    {
        int tmpValue;
        cin >> tmpValue;
        status[tmpValue] = -1;
        que.push(tmpValue);
    }
    for(int i = 0;i < n - 1;i++) {
        int tmpValue;
        cin >> tmpValue;
        if(tmpValue > que.top()) {
            status[tmpValue] = -1;
            que.push(tmpValue);
            continue;
        }else {
            int tmpTop = que.top();
            que.pop();
            if(tmpValue > que.top()) {
                countValue[tmpTop]++;
                status[tmpValue] = tmpTop;
            }else {
                status[tmpValue] = -2;
            }
            que.push(tmpTop);
            que.push(tmpValue);
        }
    }
    int maxPos = 1;
    for(int i = 2;i <= n;i++) {
        if(countValue[i] > countValue[maxPos]) {
            maxPos = i;
        }
    }
    if(status[maxPos] == -1 && countValue[maxPos] <= 1) {
        int minValue = -1;
        for(int i = 1;i <= n;i++) {
            if(i < maxPos) {
                if(status[i] != -1) {
                    minValue = i;
                    break;
                }
            }else {
                if(status[i] == -2) {
                    minValue = i;
                    break;
                }
            }
        }
        cout << (minValue == -1 ? maxPos : minValue) << endl;
    }else {
        cout << maxPos << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has,mark;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=0,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 {
  	 st.insert(ar[i]);
  	 mark[ar[i]]=1;
  }
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		mark[ar[i]]=1;
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(jt=has.begin();jt!=has.end();++jt)
  {
  	 a=jt->ff;
  	 b=jt->ss-mark[a];

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  /*if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {*/
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
 // }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has,mark;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=INT_MIN,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 {
  	 st.insert(ar[i]);
  	 mark[ar[i]]=1;
  }
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		mark[ar[i]]=1;
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(i=1;i<=n;i++)
  {
  	 a=ar[i];
  	 b=has[a]-mark[a];

  	 vp.pb(mp(b,a));

  	 //cout<<a<<" "<<b<<"\n";

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  /*if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {*/
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
 // }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has,mark;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=INT_MIN,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 {
  	 st.insert(ar[i]);
  	 mark[ar[i]]=1;
  }
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		mark[ar[i]]=1;
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(i=i;i<=n;i++)
  {
  	 a=ar[i];
  	 b=has[a]-mark[a];

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  /*if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {*/
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
 // }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=0,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 st.insert(ar[i]);
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(jt=has.begin();jt!=has.end();++jt)
  {
  	 a=jt->ff;
  	 b=jt->ss;

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
  }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has,mark;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=0,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 st.insert(ar[i]);
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		mark[ar[i]]=1;
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(jt=has.begin();jt!=has.end();++jt)
  {
  	 a=jt->ff;
  	 b=jt->ss-mark[a];

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
  }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=0,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 st.insert(ar[i]);
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		continue;
		   }
		   else
		   {
		   	flag=1;
		   	st.insert(ar[i]);
		   	has[*it]++;
		   }
	   }
  }

  for(jt=has.begin();jt!=has.end();++jt)
  {
  	 a=jt->ff;
  	 b=jt->ss;

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  if(flag==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
  }

	//str= to_string(div);

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define debug 0

 using namespace std;

 const ll MOD = 1000000007LL;

ll ar[500056],br[500056];

vector<ll> vec;
map<ll,ll> has,mark;
set<ll> st;

vector<pair<ll,ll> > vp;
ll maxi;

int main()
{

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);

    ll n,p,t,j,x,y=0,k,l,m,z,c,e,f,q,s,flag=0,sum=0,res=0,num,g,h,santax,santay,bagsize,len,xx,yy,lg,kk,remov,a,b;
	ll i,u,v,w,ans=0,maxm=0,r,minm=999999999999,dist;
	bool fl=0;
	ll d,pos,neg;
	string str;

	//double f = setprecision()

	set<ll>::iterator it,kt;
	map<ll,ll>::iterator jt;

  cin>>n;

  flag=0;

  for(i=1;i<=n;i++)
  {
  	cin>>ar[i];
  }

  for(i=1;i<=n;i++)
  {
  	 if(i==1)
  	 {
  	 st.insert(ar[i]);
  	 mark[ar[i]]=1;
  }
  	 else
  	 {
  	 	it=st.lower_bound(ar[i]);
  	 	if(it==st.end())
  	 	{
  	 		st.insert(ar[i]);
  	 		mark[ar[i]]=1;
  	 		continue;
		   }
		   else
		   {
		   	kt=st.upper_bound(*it);

		   //	cout<<"kt="<<*kt<<"\n";

		   	if(kt==st.end())
		   	has[*it]++;

		   	st.insert(ar[i]);
		   }
	   }
  }

  for(i=i;i<=n;i++)
  {
  	 a=ar[i];
  	 b=has[a]-mark[a];

  	 vp.pb(mp(b,a));

  	 if(b>maxm)
  	 {
  	 	maxm=b;
	   }
  }

  /*if(has.size()==0)
  {
  	ans=*(st.begin());
  	cout<<ans;
  }
  else
  {*/
  	sort(vp.begin(),vp.end());
  	l = vp.size();

  	for(i=0;i<l;i++)
  	{
  		if(vp[i].ff==maxm)
  		{
  			ans=vp[i].ss;
  			break;
		  }
	  }

	  cout<<ans;
 // }

	//str= to_string(div);

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int INF = (1 << 28);

int cuenta(vector<int>& A, int l, int r, int val_min, int val_max){
	int ret = 0;
	for(int i = l; i <= r; i++){
		if(val_min < A[i] && A[i] < val_max) ret++;
	}
	return ret;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	vector<int> arr(N + 2);
	vector<bool> es_record(N + 2, false);
	vector<int> record;

	record.push_back(0);

	int maximo = 0;
	for(int i = 1; i <= N; i++){
		cin >> arr[i];
		if(arr[i] > maximo){
			maximo = arr[i];
			record.push_back(i);
			es_record[i] = true;
		}
	}
	arr[N + 1] = INF;
	record.push_back(N + 1);

	int M = record.size() - 2;
	maximo = -1;
	int quien = -1;
	for(int i = 1; i <= M; i++){
		int l = record[i], r = record[i + 1];
		int val_min = arr[record[i - 1]];
		int val_max = arr[record[i]];

		int aux = cuenta(arr, l, r, val_min, val_max);
		if(aux > maximo){
			maximo = aux;
			quien = arr[record[i]];
		}else if(aux == maximo){
			if(quien > arr[record[i]]){
				quien = arr[record[i]];
			}
		}
	}
	if(maximo <= 1){
		for(int i = 1; i <= N; i++){
			if(!es_record[i] && arr[i] < quien){
				quien = arr[i];
			}
		}
	}

	cout << quien << "\n";

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int INF = (1 << 28);

int cuenta(vector<int>& A, int l, int r, int val_min, int val_max){
	int ret = 0;
	for(int i = l; i <= r; i++){
		if(val_min < A[i] && A[i] < val_max){
			ret++;
			val_min = A[i];
		}
	}
	return ret;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	vector<int> arr(N + 2);
	vector<bool> es_record(N + 2, false);
	vector<int> record;

	record.push_back(0);

	int maximo = 0;
	for(int i = 1; i <= N; i++){
		cin >> arr[i];
		if(arr[i] > maximo){
			maximo = arr[i];
			record.push_back(i);
			es_record[i] = true;
		}
	}
	arr[N + 1] = INF;
	record.push_back(N + 1);

	int M = record.size() - 2;
	maximo = -1;
	int quien = -1;
	for(int i = 1; i <= M; i++){
		int l = record[i], r = record[i + 1];
		int val_min = arr[record[i - 1]];
		int val_max = arr[record[i]];

		int aux = cuenta(arr, l, r, val_min, val_max);
		if(aux > maximo){
			maximo = aux;
			quien = arr[record[i]];
		}else if(aux == maximo){
			if(quien > arr[record[i]]){
				quien = arr[record[i]];
			}
		}
	}
	if(maximo == 1){
		for(int i = 1; i <= N; i++){
			if(!es_record[i] && arr[i] < quien){
				quien = arr[i];
			}
		}
	}else if(maximo == 0){
		bool ya_tengo = false;
		for(int i = 1; i <= N; i++){
			if(!es_record[i]){
				if(!ya_tengo){
					quien = arr[i];
					ya_tengo = true;
				}else{
					if(quien > arr[i]){
						quien = arr[i];
					}
				}
			}
		}
	}

	cout << quien << "\n";

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int INF = (1 << 28);

int cuenta(vector<int>& A, int l, int r, int val_min, int val_max){
	int ret = 0;
	for(int i = l; i <= r; i++){
		if(val_min < A[i] && A[i] < val_max) ret++;
	}
	return ret;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	vector<int> arr(N + 2);
	vector<int> record;

	record.push_back(0);

	int maximo = 0;
	for(int i = 1; i <= N; i++){
		cin >> arr[i];
		if(arr[i] > maximo){
			maximo = arr[i];
			record.push_back(i);
		}
	}
	arr[N + 1] = INF;
	record.push_back(N + 1);

	int M = record.size() - 2;
	maximo = -1;
	int quien = -1;
	for(int i = 1; i <= M; i++){
		int l = record[i], r = record[i + 1];
		int val_min = arr[record[i - 1]];
		int val_max = arr[record[i]];

		int aux = cuenta(arr, l, r, val_min, val_max);
		if(aux > maximo){
			maximo = aux;
			quien = record[i];
		}
	}
	cout << arr[quien] << "\n";

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int INF = (1 << 28);

int cuenta(vector<int>& A, int l, int r, int val_min, int val_max){
	int ret = 0;
	for(int i = l; i <= r; i++){
		if(val_min < A[i] && A[i] < val_max){
			ret++;
			val_min = A[i];
		}
	}
	return ret;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	vector<int> arr(N + 2);
	vector<bool> es_record(N + 2, false);
	vector<int> record;

	record.push_back(0);

	int maximo = 0;
	for(int i = 1; i <= N; i++){
		cin >> arr[i];
		if(arr[i] > maximo){
			maximo = arr[i];
			record.push_back(i);
			es_record[i] = true;
		}
	}
	arr[N + 1] = INF;
	record.push_back(N + 1);

	int M = record.size() - 2;
	maximo = -1;
	int quien = -1;
	for(int i = 1; i <= M; i++){
		int l = record[i], r = record[i + 1];
		int val_min = arr[record[i - 1]];
		int val_max = arr[record[i]];

		int aux = cuenta(arr, l, r, val_min, val_max);
		if(aux > maximo){
			maximo = aux;
			quien = arr[record[i]];
		}else if(aux == maximo){
			if(quien > arr[record[i]]){
				quien = arr[record[i]];
			}
		}
	}
	if(maximo <= 1){
		for(int i = 1; i <= N; i++){
			if(!es_record[i] && arr[i] < quien){
				quien = arr[i];
			}
		}
	}

	cout << quien << "\n";

	return 0;
}


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

int n,a[100100],sum[100100];

int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
    int t = 0, flag = 0;
    for(int i = 1; i <= n; i ++){
        if(a[i] <= a[i-1]) sum[i] = ++ t;
    }
   // for(int i = 1; i <= n; i ++) printf("%3d",sum[i]); printf("\n*********\n");
    int ans = 0, p = 0, q = 0;
    for(int i = 1; i <= n; i ++){
        if(sum[i] == 1){
            ans = i-1; p = i, q = p;
        }else if(sum[i] > 1){
            if(a[p] > a[p-2] || a[p-1] < a[p+1]){
                if(ans < i - q) ans = i - q, flag = i;
            }else if(ans < i - p) ans = i - p, flag = i;
            p = i, q = p;
        }else if(i == n && sum[i] == 0){
            if(a[p] > a[p-2] || a[p-1] < a[p+1]){
                if(ans < i - q) ans = i - q, flag = i;
            }else if(ans < i - p) ans = i - p, flag = i;
        }
    }
    printf("%d\n",flag);
    return 0;
}


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

const int N = 1e5 + 10;
int a[N],n,x,p,q;

int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        scanf("%d",&x);
        if(x > p) q = p, p = x, a[x] = 1;
        else if(x > q) q = x, a[p] --;
    }
    int ans = 1;
    for(int i = 1; i <= n; i ++) if(a[i] < a[ans]) ans = i;
    printf("%d\n",ans);
    return 0;
}


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

int n,x,p,q,a[100100];

int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        scanf("%d",&x);
        if(x > p) a[p] = 1, q = p, p = x;
        else if(x > q) a[p] --, q = x;
    }
    int ans = 0;
    for(int i = 0; i <= 100000; i ++) if(a[i] < a[ans]) ans = i;
    printf("%d\n",ans);
    return 0;
}


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

int n,x,p,q,a[100100];

int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        scanf("%d",&x);
        if(x > p) a[x] = 1, q = p, p = x;
        else if(x > q) a[p] --, q = x;
    }
    int ans = 1;
    for(int i = 1; i <= n; i ++) if(a[i] < a[ans]) ans = i;
    printf("%d\n",ans);
    return 0;
}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <set>
#include <map>
//#include <unordered_map>

using namespace std;

class TournamentTree {

  long long inf;

  int size;

  struct data {
    long long min, max;
    int cnt;
    data() : min(2e18), max(-2e18), cnt(0) {}
  };
  vector<data> a;

  long long from, to;
  int count(int pos, int low, int high) {
    if (a[pos].max < from || a[pos].min >= to) return 0;
    if (a[pos].max < to && a[pos].min >= from) return a[pos].cnt;
    return count(2 * pos, low, (low + high) / 2) + count(2 * pos + 1, (low + high) / 2, high);
  }

  long long sum(int pos, int low, int high) {
    if (high <= from || low >= to) return 0;
    if (high <= to && low >= from) return a[pos].cnt;
    return sum(2 * pos, low, (low + high) / 2) + sum(2 * pos + 1, (low + high) / 2, high);
  }
public:

  TournamentTree(int N) {
    inf = 2e18;
    for (size = 1; size < N; size *= 2);
    a.resize(2 * size);
  }

  void set(int pos, long long val) {
    int i = size + pos;
    if (val != -inf) {
      a[i].min = a[i].max = val;
      a[i].cnt = 1;
    }
    else {
      a[i].min = inf;
      a[i].max = -inf;
      a[i].cnt = 0;
    }
    for (i /= 2; i > 0; i /= 2) {
      a[i].max = a[i * 2].max > a[2 * i + 1].max ? a[i * 2].max : a[i * 2 + 1].max;
      a[i].min = a[i * 2].min < a[2 * i + 1].min ? a[i * 2].min : a[i * 2 + 1].min;
      a[i].cnt = a[i * 2].cnt + a[2 * i + 1].cnt;
    }
  }
  /*
  void operator=(const TournamentTree& t) {
  this->size = t.size;
  this->a = t.a;
  }
  */
  int count(long long from_, long long to_) {
    from = from_;
    to = to_;
    return count(1, 0, size);
  }

  int sum(long long from_, long long to_) {
    from = from_;
    to = to_;
    return sum(1, 0, size);
  }

};

vector<int> x;
map<int, int> cnt;

int main() {

  const long long inf = 2e18;
  int n;
  scanf("%d", &n);

  x.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
    --x[i];
  }

  int maxRecord = -1;
  int sol = -1;

  set<int> S0;
  set<int> S1;
  TournamentTree t0(n);
  TournamentTree t1(n);

  TournamentTree t(n);
  for (int i = 0; i < n; ++i) {
    int smaller = t.sum(0, x[i]);
    if (smaller == i) {
      S0.insert(x[i]);
      t0.set(x[i], 1);
    }
    else if (smaller == i - 1) {
      S1.insert(x[i]);
      t1.set(x[i], 1);
    }
    t.set(x[i], 1);
  }

  int cntOK = 0;
  for (int i = 0; i < n; ++i) {
    bool b0 = (S0.count(x[i]) > 0);
    bool b1 = (S1.count(x[i]) > 0);

    int tmpSol = cntOK;
    tmpSol += t0.sum(x[i] + 1, n);
    tmpSol += t1.sum(0, x[i]);
    if (tmpSol > maxRecord) {
      sol = x[i];
      maxRecord = tmpSol;
    }
    else if (tmpSol == maxRecord && x[i] < sol)
      sol = x[i];

    if (b0) ++cntOK;
    if (b0) {
      S0.erase(x[i]);
      t0.set(x[i], -inf);
    }
    else if (b1) {
      S1.erase(x[i]);
      t1.set(x[i], -inf);
    }
  }

  printf("%d", sol + 1);
  return 0;
}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <set>
#include <map>
//#include <unordered_map>

using namespace std;

class TournamentTree {

  const long long inf = 2e18;

  int size;

  struct data {
    const long long inf = 2e18;
    long long min, max;
    int cnt;
    data() : min(inf), max(-inf), cnt(0) {}
  };
  vector<data> a;

  long long from, to;
  int count(int pos, int low, int high) {
    if (a[pos].max < from || a[pos].min >= to) return 0;
    if (a[pos].max < to && a[pos].min >= from) return a[pos].cnt;
    return count(2 * pos, low, (low + high) / 2) + count(2 * pos + 1, (low + high) / 2, high);
  }

  long long sum(int pos, int low, int high) {
    if (high <= from || low >= to) return 0;
    if (high <= to && low >= from) return a[pos].cnt;
    return sum(2 * pos, low, (low + high) / 2) + sum(2 * pos + 1, (low + high) / 2, high);
  }
public:

  TournamentTree(int N) {
    for (size = 1; size < N; size *= 2);
    a.resize(2 * size);
  }

  void set(int pos, long long val) {
    int i = size + pos;
    if (val != -inf) {
      a[i].min = a[i].max = val;
      a[i].cnt = 1;
    }
    else {
      a[i].min = inf;
      a[i].max = -inf;
      a[i].cnt = 0;
    }
    for (i /= 2; i > 0; i /= 2) {
      a[i].max = a[i * 2].max > a[2 * i + 1].max ? a[i * 2].max : a[i * 2 + 1].max;
      a[i].min = a[i * 2].min < a[2 * i + 1].min ? a[i * 2].min : a[i * 2 + 1].min;
      a[i].cnt = a[i * 2].cnt + a[2 * i + 1].cnt;
    }
  }
  /*
  void operator=(const TournamentTree& t) {
  this->size = t.size;
  this->a = t.a;
  }
  */
  int count(long long from_, long long to_) {
    from = from_;
    to = to_;
    return count(1, 0, size);
  }

  int sum(long long from_, long long to_) {
    from = from_;
    to = to_;
    return sum(1, 0, size);
  }

};

vector<int> x;
map<int, int> cnt;

int main() {

  const long long inf = 2e18;
  int n;
  scanf("%d", &n);

  x.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
    --x[i];
  }

  int maxRecord = -1;
  int sol = -1;

  set<int> S0;
  set<int> S1;
  TournamentTree t0(n);
  TournamentTree t1(n);

  TournamentTree t(n);
  for (int i = 0; i < n; ++i) {
    int smaller = t.sum(0, x[i]);
    if (smaller == i) {
      S0.insert(x[i]);
      t0.set(x[i], 1);
    }
    else if (smaller == i - 1) {
      S1.insert(x[i]);
      t1.set(x[i], 1);
    }
    t.set(x[i], 1);
  }

  int cntOK = 0;
  for (int i = 0; i < n; ++i) {
    bool b0 = (S0.count(x[i]) > 0);
    bool b1 = (S1.count(x[i]) > 0);

    int tmpSol = cntOK;
    tmpSol += t0.sum(x[i] + 1, n);
    tmpSol += t1.sum(0, x[i]);
    if (tmpSol > maxRecord) {
      sol = x[i];
      maxRecord = tmpSol;
    }
    else if (tmpSol == maxRecord && x[i] < sol)
      sol = x[i];

    if (b0) ++cntOK;
    if (b0) {
      S0.erase(x[i]);
      t0.set(x[i], -inf);
    }
    else if (b1) {
      S1.erase(x[i]);
      t1.set(x[i], -inf);
    }
  }

  printf("%d", sol + 1);
  return 0;
}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <set>
#include <map>
//#include <unordered_map>

using namespace std;

class TournamentTree {

  long long inf;

  int size;

  struct data {
    const long long inf = 2e18;
    long long min, max;
    int cnt;
    data() : min(inf), max(-inf), cnt(0) {}
  };
  vector<data> a;

  long long from, to;
  int count(int pos, int low, int high) {
    if (a[pos].max < from || a[pos].min >= to) return 0;
    if (a[pos].max < to && a[pos].min >= from) return a[pos].cnt;
    return count(2 * pos, low, (low + high) / 2) + count(2 * pos + 1, (low + high) / 2, high);
  }

  long long sum(int pos, int low, int high) {
    if (high <= from || low >= to) return 0;
    if (high <= to && low >= from) return a[pos].cnt;
    return sum(2 * pos, low, (low + high) / 2) + sum(2 * pos + 1, (low + high) / 2, high);
  }
public:

  TournamentTree(int N) {
    inf = 2e18;
    for (size = 1; size < N; size *= 2);
    a.resize(2 * size);
  }

  void set(int pos, long long val) {
    int i = size + pos;
    if (val != -inf) {
      a[i].min = a[i].max = val;
      a[i].cnt = 1;
    }
    else {
      a[i].min = inf;
      a[i].max = -inf;
      a[i].cnt = 0;
    }
    for (i /= 2; i > 0; i /= 2) {
      a[i].max = a[i * 2].max > a[2 * i + 1].max ? a[i * 2].max : a[i * 2 + 1].max;
      a[i].min = a[i * 2].min < a[2 * i + 1].min ? a[i * 2].min : a[i * 2 + 1].min;
      a[i].cnt = a[i * 2].cnt + a[2 * i + 1].cnt;
    }
  }
  /*
  void operator=(const TournamentTree& t) {
  this->size = t.size;
  this->a = t.a;
  }
  */
  int count(long long from_, long long to_) {
    from = from_;
    to = to_;
    return count(1, 0, size);
  }

  int sum(long long from_, long long to_) {
    from = from_;
    to = to_;
    return sum(1, 0, size);
  }

};

vector<int> x;
map<int, int> cnt;

int main() {

  const long long inf = 2e18;
  int n;
  scanf("%d", &n);

  x.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
    --x[i];
  }

  int maxRecord = -1;
  int sol = -1;

  set<int> S0;
  set<int> S1;
  TournamentTree t0(n);
  TournamentTree t1(n);

  TournamentTree t(n);
  for (int i = 0; i < n; ++i) {
    int smaller = t.sum(0, x[i]);
    if (smaller == i) {
      S0.insert(x[i]);
      t0.set(x[i], 1);
    }
    else if (smaller == i - 1) {
      S1.insert(x[i]);
      t1.set(x[i], 1);
    }
    t.set(x[i], 1);
  }

  int cntOK = 0;
  for (int i = 0; i < n; ++i) {
    bool b0 = (S0.count(x[i]) > 0);
    bool b1 = (S1.count(x[i]) > 0);

    int tmpSol = cntOK;
    tmpSol += t0.sum(x[i] + 1, n);
    tmpSol += t1.sum(0, x[i]);
    if (tmpSol > maxRecord) {
      sol = x[i];
      maxRecord = tmpSol;
    }
    else if (tmpSol == maxRecord && x[i] < sol)
      sol = x[i];

    if (b0) ++cntOK;
    if (b0) {
      S0.erase(x[i]);
      t0.set(x[i], -inf);
    }
    else if (b1) {
      S1.erase(x[i]);
      t1.set(x[i], -inf);
    }
  }

  printf("%d", sol + 1);
  return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64;

const int ONE = 100005;

int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}

int n, a[ONE];
int r = 1;
int INF = 2147483640;

int C[ONE];

int lowbit(int i) {return i & -i;}
int Add(int R)
{
		for(int i = R; i >= 1; i -= lowbit(i))
			C[i]++;
}
int Query(int R)
{
		int res = 0;
		for(int i = R; i <= n; i += lowbit(i))
			res += C[i];
		return res;
}

int pos[ONE], val[ONE];
int res1, res2;

int Check(int pos)
{
		int maxx = -1, res = 0;
		for(int i = 1; i <= n; i++)
		{
			if(pos == i) continue;
			if(maxx > a[i]) break;
			res++, maxx = a[i];
		}
		return res;
}

int b[ONE];

int main()
{
		n = get();
		for(int i = 1; i <= n; i++)
			a[i] = get(), pos[a[i]] = i;

		int maxx = 0;

		for(int i = 1; i <= n; i++)
		{
			if(maxx < a[i]) b[a[i]]--;
			else if(Query(a[i]) == 1) b[maxx]++;
			Add(a[i]);
			maxx = max(maxx, a[i]);
		}

		int Ans = -1, r = 1;
		for(int i = 1; i <= n; i++)
			if(b[i] > Ans) Ans = b[i], r = i;
		cout << r << endl;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64;

const int ONE = 100005;

int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}

int n, a[ONE];

int main()
{
		n = get();
		for(int i = 1; i <= n; i++)
			a[i] = get();
		for(int i = 1; i < n; i++)
			if(a[i] > a[i + 1]) {printf("%d", a[i]); return 0;}
		printf("%d", a[1]);
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64;

const int ONE = 100005;

int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}

int n, a[ONE];
int r = 1;
int INF = 2147483640;

int main()
{
		n = get();
		for(int i = 1; i <= n; i++)
			a[i] = get();

		for(int i = 1; i < n; i++)
			if(a[i] > a[i + 1]) {r = a[i], a[i] = -1; break;}

		int maxx = 0, Ans = 0;
		for(int i = 1; i <= n; i++)
		{
			if(a[i] == -1) continue;
			maxx = max(maxx, a[i]);
			if(maxx == a[i]) Ans++;
			else break;
		}

		printf("%d", Ans > 1 ? r : 1);
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64;

const int ONE = 100005;

int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}

int n, a[ONE];
int r = 1;
int INF = 2147483640;

int main()
{
		n = get();
		for(int i = 1; i <= n; i++)
			a[i] = get();

		for(int i = 1; i < n; i++)
			if(a[i] > a[i + 1]) {r = a[i], a[i] = -1; break;}

		int maxx = 0, Ans = 0;
		for(int i = 1; i <= n; i++)
		{
			maxx = max(maxx, a[i]);
			if(maxx == a[i]) Ans++; else break;
		}

		printf("%d", Ans > 1 ? r : 1);
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64;

const int ONE = 100005;

int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}

int n, a[ONE];

int main()
{
		n = get();
		for(int i = 1; i <= n; i++)
			a[i] = get();
		for(int i = 1; i <= n; i++)
			if(a[i] > a[i + 1]) {printf("%d", a[i]); return 0;}
		printf("%d", a[1]);
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		if(i)
			b[a[i]] = query(1, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = 1;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(1, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i) {
			bi[a[i]] = query(1, a[i] - 1);
			if (bi[a[i]] == i - 1)
				add2(a[i], 1);
		}
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = 1; i <= n; i++) {
		if (query2(1, i - 1) > big) {
			big = query2(1, i - 1);
			val = i;
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		a[i]--;
		if(i)
			b[a[i]] = query(0, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = 0;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(0, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans + 1 << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
		{
			bi[a[i]] = query(1, a[i] - 1);
			if(bi[a[i]] == a[i] - 2)
				add2(a[i], 1);
		}
		add(a[i], 1);
	}

	int big = maxn;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if(i < n - 1)
		{
			if(query2(1, a[i - 1]) < big)
			{
				big = query2(1, a[i - 1]);
				val = a[i];
			}
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i) {
			bi[a[i]] = query(1, a[i] - 1);
			if (bi[a[i]] == i - 1)
				add2(a[i], 1);
		}
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = 1; i <= n; i++) {
		if (query2(1, i - 1) > big) {
			big = query2(1, a[i] - 1);
			val = i;
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) >= big) {
				big = query2(1, a[i] - 1);
				val = a[i];
			}
		}
		if(bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

vector<int> listInits;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	listInits.push_back(1);

	for (int i = 2; i <= n; ++i)
		if (a[i] < a[i - 1])
			listInits.push_back(i);

	if (listInits.size() == 1) {
		cout << 1;
		return 0;
	}

	if(listInits.size() > 2 && listInits[0] == 1 && listInits[1] == 2 && listInits[2] == 3)
	{
		cout << 1;
		return 0;
	}

	int s;
	if (listInits.size() == 2)
		s = n - listInits[1] + 1;
	else
		s = listInits[2] - listInits[1];

	int b = 0;
	int val = 1;

	if (listInits[1] < n - 1) {
		if (a[listInits[1] - 1] < a[listInits[1] + 1]) {
			b = listInits[1] - 2 + s;
			val = a[listInits[1]];
		}
		if (a[listInits[1] - 2] < a[listInits[1]]) {
			int newB = listInits[1] - 2 + s;
			if (newB > b || (newB == b && a[listInits[1] - 1] < val)) {
				b = newB;
				val = a[listInits[1] - 1];
			}
		}
	} else
		val = a[n - 1];

	cout << val << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];
vector<int> l;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	l.push_back(1);

	for (int i = 2; i <= n; ++i)
		if (a[i] < a[i - 1])
			l.push_back(i);

	if(l.size() == 1)
	{
		cout << 1;
		return 0;
	}

	l.push_back(n + 1);

	int ans = 1;
	int val = 0;

	int s1 = l[1] - l[0];
	int s2 = l[2] - l[1];

	if(a[l[1] - 2] < a[l[1]])
	{
		if(s1 - 1 + s2 > val)
		{
			val = s1 - 1 + s2;
			ans = a[l[1] - 1];
		}
	}
	if(a[l[1] - 1] < a[l[1] + 1])
	{
		if(s1 + s2 - 1 > val)
		{
			val = s1 + s2 - 1;
			ans = a[l[1]];
		}
		else if(s1 + s2 - 1 == val && a[l[1]] < ans)
			ans = a[l[1]];
	}

	cout << ans << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
		{
			bi[a[i]] = query(1, a[i] - 1);
			if(bi[a[i]] == i - 1)
				add2(a[i], 1);
		}
		add(a[i], 1);
	}

	int big = maxn;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if(i < n - 1)
		{
			if(query2(1, a[i - 1]) < big)
			{
				big = query2(1, a[i - 1]);
				val = a[i];
			}
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		if(i)
			b[a[i]] = query(1, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = n;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(1, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		if(i)
			b[a[i]] = query(1, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = 1;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(1, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[1], 1, v2);
	}

	cout << ans << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

vector<int> listInits;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	listInits.push_back(1);

	for (int i = 2; i <= n; ++i)
		if (a[i] < a[i - 1])
			listInits.push_back(i);

	if (listInits.size() == 1) {
		cout << 1;
		return 0;
	}

	if(listInits.size() > 2 && listInits[0] == 1 && listInits[1] == 2 && listInits[2] == 3)
	{
		cout << 1;
		return 0;
	}

	int s;
	if (listInits.size() == 2)
		s = n - listInits[1] + 1;
	else
		s = listInits[2] - listInits[1];

	int b = 0;
	int val = 1;

	if (listInits[1] < n) {
		if (a[listInits[1] - 1] < a[listInits[1] + 1]) {
			b = listInits[1] - 2 + s;
			val = a[listInits[1]];
		}
		if (a[listInits[1] - 2] < a[listInits[1]]) {
			int newB = listInits[1] - 2 + s;
			if (newB > b || (newB == b && a[listInits[1] - 1] < val)) {
				b = newB;
				val = a[listInits[1] - 1];
			}
		}
	} else
		val = a[n];

	cout << val << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
		{
			bi[a[i]] = query(1, a[i] - 1);
			if(bi[a[i]] == i - 1)
				add2(a[i], 1);
		}
		add(a[i], 1);
	}

	int big = maxn;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if(i < n - 1)
		{
			if(query2(1, a[i] - 1) < big)
			{
				big = query2(1, a[i] - 1);
				val = a[i];
			}
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
ll a[maxn];

ll v[maxn];
ll v1[maxn];
int isrecord[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	//cout << "add2 " << where << endl;
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i) {
			bi[a[i]] = query(1, a[i] - 1);
			if (bi[a[i]] == i)
				isrecord[a[i]] = 1;
		}
		add(a[i], 1);
	}

	ll big = 0;
	int val = a[n - 1];
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			ll tmp = query2(1, a[i] - 1) - isrecord[a[i]];
			if (tmp > big) {
				big = tmp;
				val = a[i];
			} else if (tmp == big && a[i] < val)
				val = a[i];
		}
		if (i > 1 && bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		a[i]--;
		if(i)
			b[a[i]] = query(0, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = 1;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(0, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans + 1 << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	for (int i = 0; i < maxn; ++i)
		v[i] = 0;

	for (int i = n - 1; i >= 0; i--) {
		if(i < n - 1)
			sm[a[i]] = query(a[i] + 1, n);
		add(a[i], 1);
	}

	int big = maxn;
	int val = 1;
	for (int i = 1; i <= n; ++i) {
		if(bi[i] + sm[i] < big)
		{
			big = bi[i] + sm[i];
			val = i;
		}
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	if(from > to)
		return 0;
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	//cout << "add2 " << where << endl;
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	if(from > to)
		return 0;
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = a[n - 1];
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) > big) {
				//cout << a[i] << endl;
				//cout << query2(1, a[i] - 1) << endl;
				big = query2(1, a[i] - 1);
				val = a[i];
			}
			else if (query2(1, a[i] - 1) == big && a[i] < val)
				val = a[i];
		}
		if (bi[a[i]] == i - 1 && i > 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		a[i]--;
		if(i)
			b[a[i]] = query(0, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = 0;
	int ans = 1;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(0, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans << endl;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
ll a[maxn];

ll v[maxn];
ll v1[maxn];
int isrecord[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	//cout << "add2 " << where << endl;
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	isrecord[a[0]] = 1;
	for (int i = 0; i < n; ++i) {
		if (i) {
			bi[a[i]] = query(1, a[i] - 1);
			if (bi[a[i]] == i)
				isrecord[a[i]] = 1;
		}
		add(a[i], 1);
	}

	ll big = 0;
	int val = a[n - 1];
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			ll tmp = query2(1, a[i] - 1) - isrecord[a[i]];
			if (tmp > big) {
				big = tmp;
				val = a[i];
			} else if (tmp == big && a[i] < val)
				val = a[i];
		}
		if (i > 1 && bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query2(to) - query2(from - 1);
}

int bi[maxn];
int sm[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) >= big) {
				big = query2(1, a[i] - 1);
				val = a[i];
			}
		}
		if(bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	if(from > to)
		return 0;
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	//cout << "add2 " << where << endl;
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	if(from > to)
		return 0;
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) > big) {
				//cout << a[i] << endl;
				//cout << query2(1, a[i] - 1) << endl;
				big = query2(1, a[i] - 1);
				val = a[i];
			}
			else if (query2(1, a[i] - 1) == big && a[i] < val)
				val = a[i];
		}
		if (bi[a[i]] == i - 1 && i > 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


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

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	//cout << "add2 " << where << endl;
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = a[n - 1];
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) > big) {
				//cout << a[i] << endl;
				//cout << query2(1, a[i] - 1) << endl;
				big = query2(1, a[i] - 1);
				val = a[i];
			}
			else if (query2(1, a[i] - 1) == big && a[i] < val)
				val = a[i];
		}
		if (i > 1 && bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}