#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#define ll long long

// 9 7 6 3 6 6

using namespace std;

int main()
{
    ll n, elemRev = -1, elemV, pom, xxx;
    bool b = false;
    map<int, int>m;
    map<int, int>ajde;

    cin >> n;

    vector<ll>v(n, 0);

    for (ll i = 0; i < n; i++)
    {
        cin >> v[i];
        ajde[v[i]] = i;
    }

    vector<ll>rev(v);
    sort(rev.rbegin(), rev.rend());

    for (ll i = 0; i < v.size(); i++)
    {
        m[v[i]]++;
        if (m[i] == 2)
            b = true;

        if (v[i] == rev[i])
            continue;

        elemRev = ajde[rev[i]];
        elemV = i;
        break;
    }

    if (elemRev != -1)
    {
        pom = v[elemV];
        v[elemV] = v[elemRev];
        v[elemRev] = pom;
    }

    else if (!b)
    {
        pom = v[v.size() - 2];
        v[v.size() - 2] = v[v.size() - 1];
        v[v.size() - 1] = pom;
    }

    for (ll i = 0; i < v.size(); i++)
        cout << v[i] << " ";


    return 0;
}