//#include<iostream>
//#include<cstring>
//#include<cstdio>
//#include<map>
//#include<queue>
//#include<cmath>
//#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define PI pair<int,int>
const int maxm=2e5+5;
const int mod=1e9+7;
char s[maxm];
int d[3];
int n;
signed main(){
    cin>>n;
    scanf("%s",s+1);
    int p=1;
    for(int i=1;i<=n;i++){
        if(s[i]=='a'){
            d[0]+=p;
        }else if(s[i]=='b'){
            d[1]+=d[0];
        }else if(s[i]=='c'){
            d[2]+=d[1];
        }else if(s[i]=='?'){
            d[2]=d[2]*3+d[1];
            d[1]=d[1]*3+d[2];
            d[0]=d[0]*3+p;
            p=p*3%mod;
        }
        d[0]%=mod;
        d[1]%=mod;
        d[2]%=mod;
    }
    cout<<d[2]<<endl;
    return 0;
}
/*

*/


#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <string>
#include <set>

#define INVERSE2 ((long long int)500000004)
#define INVERSE3 ((long long int)333333336)
#define INVERSE9 ((long long int)111111112)
#define MOD ((long long int)1000000007)

int main()
{
    int n;
    std::cin >> n;

    std::string s;
    std::cin >> s;

    int aGlob = 0;
    int abGlob = 0;
    long long int aQM = 0;
    long long int bQM = 0;
    long long int ans = 0;
    int k = 0;
    long long int kPow3 = 1;

    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == 'a')
        {
            aGlob += 1;
        }
        else if (s[i] == 'b')
        {
            abGlob += aGlob;
            bQM += (long long int)k;
        }
        else if (s[i] == 'c')
        {
            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;
        }
        else
        {
            ans *= 3;

            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;

            aQM += (long long int)aGlob;
            kPow3 *= 3;
            kPow3 %= MOD;
            ++k;
        }
    }
    std::cout << ans << std::endl;;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <string>
#include <set>

#define INVERSE2 ((long long int)500000004)
#define INVERSE3 ((long long int)333333336)
#define INVERSE9 ((long long int)111111112)
#define MOD ((long long int)1000000007)

int main()
{
    int n;
    std::cin >> n;

    std::string s;
    std::cin >> s;

    int aGlob = 0;
    int abGlob = 0;
    long long int aQM = 0;
    long long int bQM = 0;
    long long int ans = 0;
    int k = 0;
    long long int kPow3 = 1;

    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == 'a')
        {
            aGlob += 1;
        }
        else if (s[i] == 'b')
        {
            abGlob += aGlob;
            bQM += (long long int)k;
            bQM %= MOD;
        }
        else if (s[i] == 'c')
        {
            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;
        }
        else
        {
            ans *= 3;

            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;

            aQM += (long long int)aGlob;
            aQM %= MOD;
            kPow3 *= 3;
            kPow3 %= MOD;
            ++k;
        }
    }
    std::cout << ans << std::endl;;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <string>
#include <set>

#define INVERSE2 ((long long int)500000004)
#define INVERSE3 ((long long int)333333336)
#define INVERSE9 ((long long int)111111112)
#define MOD ((long long int)1000000007)

int main()
{
    int n;
    std::cin >> n;

    std::string s;
    std::cin >> s;

    int aGlob = 0;
    long long int abGlob = 0;
    long long int aQM = 0;
    long long int bQM = 0;
    long long int ans = 0;
    int k = 0;
    long long int kPow3 = 1;

    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == 'a')
        {
            aGlob += 1;
        }
        else if (s[i] == 'b')
        {
            abGlob += aGlob;
            abGlob %= MOD;
            bQM += (long long int)k;
            bQM %= MOD;
        }
        else if (s[i] == 'c')
        {
            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;
        }
        else
        {
            ans *= 3;

            ans += (abGlob * kPow3) % MOD;
            ans %= MOD;

            long long int toAdd = ((long long int)(k - 1) * (long long int)k) % MOD;
            toAdd *= INVERSE2;
            toAdd %= MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;

            aQM += (long long int)aGlob;
            aQM %= MOD;
            kPow3 *= 3;
            kPow3 %= MOD;
            ++k;
        }
    }
    std::cout << ans << std::endl;;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <string>
#include <set>

#define INVERSE2 ((long long int)500000004)
#define INVERSE3 ((long long int)333333336)
#define INVERSE9 ((long long int)111111112)
#define MOD ((long long int)1000000007)

int main()
{
    int n;
    std::cin >> n;

    std::string s;
    std::cin >> s;

    int aGlob = 0;
    int abGlob = 0;
    long long int aQM = 0;
    long long int bQM = 0;
    long long int ans = 0;
    int k = 0;
    long long int kPow3 = 1;

    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == 'a')
        {
            aGlob += 1;
        }
        else if (s[i] == 'b')
        {
            abGlob += aGlob;
            bQM += (long long int)k;
        }
        else if (s[i] == 'c')
        {
            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k * INVERSE2) % MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;
        }
        else
        {
            ans *= 3;

            ans += abGlob * kPow3;
            long long int toAdd = ((long long int)(k - 1) * (long long int)k * INVERSE2) % MOD;
            toAdd *= kPow3;
            toAdd %= MOD;
            toAdd *= INVERSE9;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (aQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            toAdd = (bQM * kPow3) % MOD;
            toAdd *= INVERSE3;
            toAdd %= MOD;
            ans += toAdd;

            ans %= MOD;

            aQM += (long long int)aGlob;
            kPow3 *= 3;
            kPow3 %= MOD;
            ++k;
        }
    }
    std::cout << ans << std::endl;;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int MOD = 1e9 + 7;

LL frac[maxn],ifrac[maxn];
char str[maxn];
LL sum[4][maxn];
int n;

LL fpow(LL a,LL b){
    if(b < 0) return 0;
    LL ret = 1;
    while(b){
        if(b & 1) ret = (ret * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return ret;
}

void init(){
    frac[0] = 1;
    for(int i = 1 ; i < maxn ; i++ )
        frac[i] = frac[i - 1] * i % MOD;
    ifrac[maxn - 1] = fpow(frac[maxn - 1],MOD - 2);
    for(int i = maxn - 2 ; i >= 0 ; i-- )
        ifrac[i] = ifrac[i + 1] * (i + 1) % MOD;
}

LL C(int a,int b){
    if(a < b || b < 0) return 0;
    return frac[a] * ifrac[b] % MOD * ifrac[a - b] % MOD;
}
bool check(int dex){
    if(str[dex] == 'a' && str[dex + 1] == 'b') return true;
    if(str[dex] == 'a' && str[dex + 2] == 'c') return true;
    if(str[dex + 1] == 'b' && str[dex + 2] == 'c') return true;
    return false;
}

int main(){
    init();
    scanf("%d",&n);
    scanf("%s",str + 1);
    int cnt = 0;
    for(int i = 1 ; i <= n ; i++ ){
        for(int j = 0 ; j < 4 ; j++ )
            sum[j][i] = sum[j][i - 1];
        if(str[i] == '?') ++sum[0][i],++cnt;
        else ++sum[str[i] - 'a' + 1][i];
    }
    // printf("cnt = %d\n", cnt);
    // cout << fpow(3,1) << endl;
    LL ans = 0;
    for(int i = 2 ; i <= n ; i++ ){
        if(str[i] == 'b' || str[i] == '?'){
            // ans = (ans + min(sum[1][i - 1] + sum[0][i - 1],sum[3][n] - sum[3][i - 1] + sum[0][n] - sum[0][i - 1]) * ) % MOD;
            ans = (ans + sum[1][i - 1] * (sum[3][n] - sum[3][i - 1]) % MOD * fpow(3,cnt - (str[i] == '?' ? 1 : 0)) % MOD) % MOD;
            ans = (ans + sum[0][i - 1] % MOD * (sum[0][n] - sum[0][i]) % MOD * fpow(3,(cnt - 2 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
            ans = (ans + sum[0][i - 1] % MOD * (sum[3][n] - sum[3][i - 1]) % MOD * fpow(3,(cnt - 1 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
            ans = (ans + (sum[0][n] - sum[0][i]) % MOD * sum[1][i - 1] % MOD * fpow(3,(cnt - 1 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
        }
    }
    cout << ans << endl;
    return 0;
}

/*
100
accbaccabccbbbbabacabaaccacbcbcababbbcbcbcccabcbbc?caaabcabcaaccbccabaaaaccacabbaabcbbccbbababaacbca
14634
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int MOD = 1e9 + 7;

LL frac[maxn],ifrac[maxn];
char str[maxn];
int sum[4][maxn];
int n;

LL fpow(LL a,LL b){
    if(b < 0) return 0;
    LL ret = 1;
    while(b){
        if(b & 1) ret = (ret * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return ret;
}

void init(){
    frac[0] = 1;
    for(int i = 1 ; i < maxn ; i++ )
        frac[i] = frac[i - 1] * i % MOD;
    ifrac[maxn - 1] = fpow(frac[maxn - 1],MOD - 2);
    for(int i = maxn - 2 ; i >= 0 ; i-- )
        ifrac[i] = ifrac[i + 1] * (i + 1) % MOD;
}

LL C(int a,int b){
    if(a < b || b < 0) return 0;
    return frac[a] * ifrac[b] % MOD * ifrac[a - b] % MOD;
}
bool check(int dex){
    if(str[dex] == 'a' && str[dex + 1] == 'b') return true;
    if(str[dex] == 'a' && str[dex + 2] == 'c') return true;
    if(str[dex + 1] == 'b' && str[dex + 2] == 'c') return true;
    return false;
}

int main(){
    init();
    scanf("%d",&n);
    scanf("%s",str + 1);
    int cnt = 0;
    for(int i = 1 ; i <= n ; i++ ){
        for(int j = 0 ; j < 4 ; j++ )
            sum[j][i] = sum[j][i - 1];
        if(str[i] == '?') ++sum[0][i],++cnt;
        else ++sum[str[i] - 'a' + 1][i];
    }
    LL ans = 0;
    for(int i = 2 ; i <= n ; i++ ){
        if(str[i] == 'b' || str[i] == '?'){
            // ans = (ans + min(sum[1][i - 1] + sum[0][i - 1],sum[3][n] - sum[3][i - 1] + sum[0][n] - sum[0][i - 1]) * ) % MOD;
            ans = (ans + min(sum[1][i - 1],sum[3][n] - sum[3][i - 1]) * fpow(3,cnt - (str[i] == '?' ? 1 : 0)) % MOD);
            ans = (ans + sum[0][i - 1] % MOD * (sum[0][n] - sum[0][i]) % MOD * fpow(3,(cnt - 2 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
            ans = (ans + sum[0][i - 1] % MOD * (sum[3][n] - sum[3][i - 1]) % MOD * fpow(3,(cnt - 1 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
            ans = (ans + (sum[0][n] - sum[0][i]) % MOD * sum[1][i - 1] % MOD * fpow(3,(cnt - 1 - (str[i] == '?' ? 1 : 0)) % MOD)) % MOD;
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int MOD = 1e9 + 7;

LL frac[maxn],ifrac[maxn];
char str[maxn];
int sum[4][maxn];
int n;

LL fpow(LL a,LL b){
    LL ret = 1;
    while(b){
        if(b & 1) ret = (ret * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return ret;
}

void init(){
    frac[0] = 1;
    for(int i = 1 ; i < maxn ; i++ )
        frac[i] = frac[i - 1] * i % MOD;
    ifrac[maxn - 1] = fpow(frac[maxn - 1],MOD - 2);
    for(int i = maxn - 2 ; i >= 0 ; i-- )
        ifrac[i] = ifrac[i + 1] * (i + 1) % MOD;
}

LL C(int a,int b){
    if(a < b || b < 0) return 0;
    return frac[a] * ifrac[b] % MOD * ifrac[a - b] % MOD;
}
bool check(int dex){
    if(str[dex] == 'a' && str[dex + 1] == 'b') return true;
    if(str[dex] == 'a' && str[dex + 2] == 'c') return true;
    if(str[dex + 1] == 'b' && str[dex + 2] == 'c') return true;
    return false;
}

int main(){
    init();
    scanf("%d",&n);
    scanf("%s",str + 1);
    int cnt = 0;
    for(int i = 1 ; i <= n ; i++ ){
        for(int j = 0 ; j < 4 ; j++ )
            sum[j][i] = sum[j][i - 1];
        if(str[i] == '?') ++sum[0][i],++cnt;
        else ++sum[str[i] - 'a' + 1][i];
    }
    LL ans = 0;
    for(int i = 2 ; i <= n ; i++ ){
        if(str[i] == 'b' || str[i] == '?'){
            // ans = (ans + min(sum[1][i - 1] + sum[0][i - 1],sum[3][n] - sum[3][i - 1] + sum[0][n] - sum[0][i - 1]) * ) % MOD;
            ans = (ans + min(sum[1][i - 1],sum[3][n] - sum[3][i - 1]) * fpow(3,cnt) % MOD);
            ans = (ans + sum[0][i - 1] * (sum[0][n] - sum[0][i]) % MOD * C(cnt - 2 - (str[i] == '?' 1 : 0),2) % MOD) % MOD;
            ans = (ans + sum[0][i - 1] * (sum[3][n] - sum[3][i - 1]) % MOD * C(cnt - 1- (str[i] == '?' 1 : 0),2) % MOD) % MOD;
            ans = (ans + (sum[0][n] - sum[0][i - 1]) * sum[1][i - 1] % MOD * C(cnt - 1- (str[i] == '?' 1 : 0),2) % MOD) % MOD;
        }
    }
    cout << ans << endl;
    return 0;
}


//November
#include<iostream>
using namespace std;
const int maxn=2e6+7;
int dp[3];
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string ss;
    cin>>ss;
    string s="0";
    s+=ss;
    int k=1;
    for(int i=1; i<s.size(); i++)
    {
        if(s[i]=='a')
            dp[0]+=k;
        else if(s[i]=='b')
            dp[1]+=dp[0];
        else if(s[i]=='c')
            dp[2]+=dp[1];
        else
        {
            dp[0]=dp[0]*3+k;
            dp[1]=dp[1]*3+dp[0];
            dp[2]=dp[2]*3+dp[1];
            k*=3;
            k%=3;
        }

        for(int j=0; j<3; j++)
            dp[j]%=mod;
    }
    cout<<dp[2]<<endl;
}


//November
#include<iostream>
using namespace std;
const int maxn=2e6+7;
int dp[3];
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string ss;
    cin>>ss;
    string s="0";
    s+=ss;
    int k=1;
    for(int i=1; i<=n; i++)
    {
        if(s[i]=='a')
            dp[0]+=k;
        else if(s[i]=='b')
            dp[1]+=dp[0];
        else if(s[i]=='c')
            dp[2]+=dp[1];
        else
        {
            dp[2]=dp[2]*3+dp[1];
            dp[1]=dp[1]*3+dp[0];
            dp[0]=dp[0]*3+k;

            k*=3;
            k%=3;
        }
        for(int j=0; j<3; j++)
            dp[j]%=mod;
    }
    cout<<dp[2]<<endl;
}


//November
#include<iostream>
using namespace std;
const int maxn=2e6+7;
int dp[3];
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string ss;
    cin>>ss;
    string s="0";
    s+=ss;
    int k=1;
    for(int i=1; i<s.size(); i++)
    {
        if(s[i]=='a')
            dp[0]+=k;
        else if(s[i]=='b')
            dp[1]+=dp[0];
        else if(s[i]=='c')
            dp[2]+=dp[1];
        else
        {
            dp[2]=dp[2]*3+dp[1];
            dp[1]=dp[1]*3+dp[0];
            dp[0]=dp[0]*3+k;

            k*=3;
            k%=3;
        }
        for(int j=0; j<3; j++)
            dp[j]%=mod;
    }
    cout<<dp[2]<<endl;
}


//November
#include<iostream>
using namespace std;
const int maxn=2e6+7;
int dp[3];
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string ss;
    cin>>ss;
    string s="0";
    s+=ss;
    int k=1;
    for(int i=1; i<=n; i++)
    {
        if(s[i]=='a')
            dp[0]+=k;
        else if(s[i]=='b')
            dp[1]+=dp[0];
        else if(s[i]=='c')
            dp[2]+=dp[1];
        else
        {
            dp[2]=dp[2]*3+dp[1];
            dp[1]=dp[1]*3+dp[0];
            dp[0]=dp[0]*3+k;
            k=k*3%mod;
        }
        dp[0]%=mod;
        dp[1]%=mod;
        dp[2]%=mod;
    }
    cout<<dp[2]<<endl;
}


//November
#include<iostream>
using namespace std;
const int maxn=2e6+7;
long long  dp[3];
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string ss;
    cin>>ss;
    string s="0";
    s+=ss;
    long long  k=1;
    for(int i=1; i<=n; i++)
    {
        if(s[i]=='a')
            dp[0]+=k;
        else if(s[i]=='b')
            dp[1]+=dp[0];
        else if(s[i]=='c')
            dp[2]+=dp[1];
        else
        {
            dp[2]=dp[2]*3+dp[1];
            dp[1]=dp[1]*3+dp[0];
            dp[0]=dp[0]*3+k;
            k*=3;
            k%=mod;
        }
        dp[0]%=mod;
        dp[1]%=mod;
        dp[2]%=mod;
    }
    cout<<dp[2]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e5+5;
const int mod=1e9+7;
char s[maxm];
int d[3];
int n;
signed main(){
    cin>>n;
    scanf("%s",s+1);
    int p=1;
    for(int i=1;i<=n;i++){
        if(s[i]=='a'){
            d[0]+=p;
        }else if(s[i]=='b'){
            d[1]+=d[0];
        }else if(s[i]=='c'){
            d[2]+=d[1];
        }else if(s[i]=='?'){
            d[2]=d[2]*3+d[1];
            d[1]=d[1]*3+d[0];
            d[0]=d[0]*3+p;
            p=p*3%mod;
        }
        d[0]%=mod;
        d[1]%=mod;
        d[2]%=mod;
    }
    cout<<d[2]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5, mod = 1e9 + 7;
int n;
char str[N];
int dp[N][4][4];

int quickpow(int a, int b)
{
	int res = 1;
	while (b) {
		if (b & 1)
			res = 1ll * res * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d %s", &n, str + 1);
	dp[0][0][0] = dp[1][0][0] = 1;
	for (int i = 1; i <= n; i++)
		for (int j = 0; j < 4; j++)
			for (int k = 0; k < 4; k++) {
				dp[i + 1][j][k] = (dp[i + 1][j][k] + dp[i][j][k]) % mod;
				if (j < 3 && (str[i] == '?' || str[i] - 'a' == j))
					dp[i][j + 1][k + (str[i] == '?')] = (dp[i][j + 1][k + (str[i] == '?')] + (str[i] == '?' ? dp[i - 1][j][k] : dp[i][j][k])) % mod;
			}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		cnt += str[i] == '?';
	int res = 0;
	for (int i = 0; i <= min(3, cnt); i++)
		res = (res + 1ll * dp[n][3][i] * quickpow(3, cnt - i) % mod) % mod;
	printf("%d\n", res);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5, mod = 1e9 + 7;
int n;
char str[N];
int dp[N][4][4];

int quickpow(int a, int b)
{
	int res = 1;
	while (b) {
		if (b & 1)
			res = 1ll * res * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d %s", &n, str + 1);
	dp[0][0][0] = 1;
	for (int i = 0; i <= n; i++)
		for (int j = 0; j < 4; j++)
			for (int k = 0; k < 4; k++) {
				dp[i + 1][j][k] = dp[i][j][k];
				if (j < 3 && (str[i] == '?' || str[i] - 'a' == j))
					dp[i][j + 1][k + (str[i] == '?')] = (dp[i][j + 1][k + (str[i] == '?')] + dp[i - 1][j][k]) % mod;
			}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		cnt += str[i] == '?';
	int res = 0;
	for (int i = 0; i <= min(3, cnt); i++)
		res = (res + 1ll * dp[n][3][i] * quickpow(3, cnt - i) % mod) % mod;
	printf("%d\n", res);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5, mod = 1e9 + 7;
int n;
char str[N];
int dp[N][4][4];

int quickpow(int a, int b)
{
	int res = 1;
	while (b) {
		if (b & 1)
			res = 1ll * res * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d %s", &n, str + 1);
	dp[0][0][0] = dp[1][0][0] = 1;
	for (int i = 1; i <= n; i++)
		for (int j = 0; j < 4; j++)
			for (int k = 0; k < 4; k++) {
				dp[i + 1][j][k] = (dp[i + 1][j][k] + dp[i][j][k]) % mod;
				if (j < 3 && (str[i] == '?' || str[i] - 'a' == j))
					dp[i][j + 1][k + (str[i] == '?')] = (dp[i][j + 1][k + (str[i] == '?')] + dp[i - 1][j][k]) % mod;
			}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		cnt += str[i] == '?';
	int res = 0;
	for (int i = 0; i <= min(3, cnt); i++)
		res = (res + 1ll * dp[n][3][i] * quickpow(3, cnt - i) % mod) % mod;
	printf("%d\n", res);
	return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int n;
char a[N];
const int mod=1e9+7;
int add(int x,int y){
  x+=y;
  if(x>=mod)x-=mod;
  return x;
}
int mul(int x,int y){
  return 1ll*x*y%mod;
}
int tmp[N][2];
void dfs(int x,int y,int z){
  //cout<<x-1<<' '<<y<<' '<<z<<endl;
  tmp[x-1][0]+=y;
  tmp[x-1][1]+=z;
  if(x>n){
    return;
  }
  if(a[x]!='?'){
    if(a[x]=='a')dfs(x+1,y+1,z);
    else if(a[x]=='b')dfs(x+1,y,z+1);
    else dfs(x+1,y,z);
    return ;
  }
  dfs(x+1,y+1,z);
  dfs(x+1,y,z+y);
  dfs(x+1,y,z);
}
int f[N],cnt[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>a+1;
//  dfs(1,0,0);
  int ans=0,x=0,y=0,z;
  f[0]=1;
  for(int i=1;i<=n;i++)f[i]=mul(f[i-1],3);
  for(int i=1;i<=n;i++){
    cnt[i]=cnt[i-1];
    if(a[i]=='?'){
      cnt[i]++;
      ans=add(mul(3,ans),y);
      y=add(mul(3,y),x);
      x=add(mul(3,x),f[cnt[i-1]]);
    }else if(a[i]=='a'){
      x=add(x,1);
    }else if(a[i]=='b'){
      y=add(y,x);
    }else{
      ans=add(ans,y);
    }
    //cout<<i<<' '<<x<<' '<<y<<' '<<ans<<' ';
  //  cout<<tmp[i][0]<<' '<<tmp[i][1]<<endl;
  }
  cout<<ans<<'\n';
  return 0;
}
//aa b
// ac b
// ab b

// ab c
// aa c
// ac c

// aba
// aaa
// aca


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int n;
char a[N];
const int mod=1e9+7;
LL add(LL x,LL y){
  x+=y;
  if(x>=mod)x-=mod;
  return x;
}
LL mul(LL x,LL y){
  return 1ll*x*y%mod;
}
int tmp[N][2];
void dfs(int x,int y,int z){
  //cout<<x-1<<' '<<y<<' '<<z<<endl;
  tmp[x-1][0]+=y;
  tmp[x-1][1]+=z;
  if(x>n){
    return;
  }
  if(a[x]!='?'){
    if(a[x]=='a')dfs(x+1,y+1,z);
    else if(a[x]=='b')dfs(x+1,y,z+1);
    else dfs(x+1,y,z);
    return ;
  }
  dfs(x+1,y+1,z);
  dfs(x+1,y,z+y);
  dfs(x+1,y,z);
}
LL f[N];
int cnt[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>a+1;
//  dfs(1,0,0);
  LL ans=0,x=0,y=0;
  f[0]=1;
  for(int i=1;i<=n;i++)f[i]=mul(f[i-1],3);
  for(int i=1;i<=n;i++){
    cnt[i]=cnt[i-1];
   // a[i]='?';
    if(a[i]=='?'){
      cnt[i]++;
      ans=add(mul(3,ans),y);
      y=add(mul(3,y),x);
      x=add(mul(3,x),f[cnt[i-1]]);
    }else if(a[i]=='a'){
      x=add(x,f[cnt[i-1]]);
    }else if(a[i]=='b'){
      y=add(y,x);
    }else{
      ans=add(ans,y);
    }
    //cout<<i<<' '<<x<<' '<<y<<' '<<ans<<' ';
  //  cout<<tmp[i][0]<<' '<<tmp[i][1]<<endl;
  }
  cout<<ans<<'\n';
  return 0;
}
//aa b
// ac b
// ab b

// ab c
// aa c
// ac c

// aba
// aaa
// aca


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int n;
char a[N];
const int mod=1e9+7;
LL add(LL x,LL y){
  x+=y;
  if(x>=mod)x-=mod;
  return x;
}
LL mul(LL x,LL y){
  return 1ll*x*y%mod;
}
int tmp[N][2];
void dfs(int x,int y,int z){
  //cout<<x-1<<' '<<y<<' '<<z<<endl;
  tmp[x-1][0]+=y;
  tmp[x-1][1]+=z;
  if(x>n){
    return;
  }
  if(a[x]!='?'){
    if(a[x]=='a')dfs(x+1,y+1,z);
    else if(a[x]=='b')dfs(x+1,y,z+1);
    else dfs(x+1,y,z);
    return ;
  }
  dfs(x+1,y+1,z);
  dfs(x+1,y,z+y);
  dfs(x+1,y,z);
}
LL f[N];
int cnt[N];
int main() {
  ios::sync_with_stdio(false);
  //cout<<1ll*(mod-1)*(mod-1)<<endl;
  cin>>n>>a+1;
//  dfs(1,0,0);
  LL ans=0,x=0,y=0;
  f[0]=1;
  //cout<<(mod-1+mod-1)<<endl;
  for(int i=1;i<=n;i++)f[i]=mul(f[i-1],3);
  for(int i=1;i<=n;i++){
    cnt[i]=cnt[i-1];
    if(a[i]=='?'){
      cnt[i]++;
      ans=add(mul(3,ans),y);
      y=add(mul(3,y),x);
      x=add(mul(3,x),f[cnt[i-1]]);
    }else if(a[i]=='a'){
      x=add(x,1);
    }else if(a[i]=='b'){
      y=add(y,x);
    }else{
      ans=add(ans,y);
    }
    //cout<<i<<' '<<x<<' '<<y<<' '<<ans<<' ';
  //  cout<<tmp[i][0]<<' '<<tmp[i][1]<<endl;
  }
  cout<<ans<<'\n';
  return 0;
}
//aa b
// ac b
// ab b

// ab c
// aa c
// ac c

// aba
// aaa
// aca


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define cp complex<double>
using namespace std;
const double pi=3.1415926535897;
int m,n,pre[5][200005],mod=1000000007,sum;
ll ans;
char wa[200005];
inline ll quick(int e,ll f,int o)
{
	ll s=1;
	while(e)
	{
		if(e%2) { e--; s=s*f%o; }
		else { e>>=1; f=f*f%o; }
	}
	return s;
}
signed main()
{
    std::ios::sync_with_stdio(0); std::cin.tie(0);
    cin>>m>>wa+1;
    for(int i=1;i<=m;++i)
    {
    	pre[0][i]=pre[0][i-1];
    	pre[1][i]=pre[1][i-1];
    	pre[2][i]=pre[2][i-1];
    	pre[3][i]=pre[3][i-1];
		if(wa[i]=='a') ++pre[1][i];
		if(wa[i]=='b') ++pre[2][i];
		if(wa[i]=='c') ++pre[3][i];
		if(wa[i]=='?') ++pre[0][i];
	}
	ll r=3;
	ll tp=quick(pre[0][m],r,mod);
	ll tp1=quick(max(0,pre[0][m]-1),r,mod);
	ll tp2=quick(max(0,pre[0][m]-2),r,mod);
	ll tp3=quick(max(0,pre[0][m]-3),r,mod);
	for(int i=1;i<=m;++i)
	{
		if(wa[i]=='?') ans+=pre[1][i-1]*(pre[3][m]-pre[3][i])%mod*tp1;
		else if(wa[i]=='a') ans+=(pre[0][m]-pre[0][i])*(pre[0][m]-pre[0][i]-1)/2%mod*tp2;
		else if(wa[i]=='b')
		{
			ans+=(pre[0][i-1]*tp2%mod+pre[1][i-1]*tp1%mod)*(pre[0][m]-pre[0][i])%mod+tp1*pre[0][i-1]%mod*(pre[3][m]-pre[3][i]);
			sum+=pre[1][i-1]*(pre[3][m]-pre[3][i]);
			sum%=mod;
		}
		else ans+=pre[0][i-1]*(pre[0][i-1]-1)/2%mod*tp2;
		ans%=mod;
	}
	ans=(ans+sum*tp%mod)%mod;
	ans+=tp3*pre[0][m]%mod*(pre[0][m]-1)*(pre[0][m]-2)/6%mod;
	cout<<ans%mod;
	return 0;
}


//=============================================================
// Name        : 489a
// Date        : Tue Jun 11 10:05:36 CST 2019
// Author      : landcold7
// Description : Actions speak louder more than words
//=============================================================
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
#define sz(x) ((int)(x).size())
#ifndef _has_trace
#define trace(...)
#endif
typedef long long ll;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> b = a;
  sort(a.begin(), a.end());
  trace(a, b);
  vector<pair<int, int>> ans;
  for (int i = 0; i < n; ++i) {
    if (b[i] != a[i]) {
      int ix = -1;
      for (int j = 0; j < n; ++j) {
        if (b[j] == a[i]) {
          ix = j;
          break;
        }
      }
      ans.emplace_back(i, ix);
      swap(b[i], b[ix]);
    }
  }
  trace(a, b);
  cout << (int) ans.size() << "\n";
  for (int i = 0; i < (int) ans.size(); ++i) {
    cout << ans[i].x << " " << ans[i].y << "\n";
  }
}

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


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long int i,n,m,j,k,l,c=0,x,tmp;
    cin>>n;
    long long int a[n];

    vector<long long int> v;

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

    for(i=0;i<n-1;i++)
    {
        tmp=i;
        for(j=i+1;j<n;j++)
        {
            if(a[tmp]>a[j])
            tmp=j;
        }

        //if(a[j]>a[j+1])
        //{
            //x=a[j];a[j]=a[j+1];a[j+1]=x;
            c++;
            v.push_back(i);
            v.push_back(tmp);
        //}
    }

    cout<<c<<endl;

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

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long int i,n,m,j,k,l,c=0,x;
    cin>>n;
    long long int a[n];

    vector<long long int> v;

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

    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]<a[j+1])
            {
                x=a[j];a[j]=a[j+1];a[j+1]=x;
                c++;
                v.push_back(j);
                v.push_back(j+1);
            }
        }
    }

    cout<<c<<endl;

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

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long int i,n,m,j,k,l,c=0,x,tmp;
    cin>>n;
    long long int a[n];

    vector<long long int> v;

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

    for(i=0;i<n-1;i++)
    {
        tmp=i;
        for(j=i+1;j<n;j++)
        {
            if(a[tmp]>a[j])
            tmp=j;
        }

        //if(a[j]>a[j+1])
        //{
            x=a[tmp];a[tmp]=a[i];a[i]=x;
            c++;
            v.push_back(i);
            v.push_back(tmp);
        //}
    }

    cout<<c<<endl;

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

    //for(i=0;i<n;i++)
    //cout<<a[i]<<" ";
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long int i,n,m,j,k,l,c=0,x,tmp;
    cin>>n;
    long long int a[n];

    vector<long long int> v;

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

    for(i=0;i<n-1;i++)
    {
        tmp=i;
        for(j=i+1;j<n;j++)
        {
            if(a[tmp]>a[j])
            tmp=j;
        }

        //if(a[j]>a[j+1])
        //{
            x=a[tmp];a[tmp]=a[i];a[i]=x;
            c++;
            v.push_back(i);
            v.push_back(tmp);
        //}
    }

    cout<<c<<endl;

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

    for(i=0;i<n;i++)
    cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
using namespace std;
/*void minimum(int x){

}*/
int main(){
    int n, c[20000], b[20000];
    cin>>n;
    int a[n];
   for(int u=0; u<n; u++){
    cin>>a[u];
   }
int k=0, p=0, z=0, d=-1;
   int minimum;
   while(k!=n-2){
       minimum=a[k];
      for(int y=k+1; y<n; y++){
        if(a[y]<minimum){
            minimum=a[y];
            z=y;
            }
    }
if(a[k]!=minimum){
int f;
f=a[k]; a[k]=minimum; a[z]=f;
p++; d++; c[d]=k; b[d]=z;
}
k++;
}
cout<<p<<endl;
for(int u=0; u<=d; u++){
    cout<<c[u]<<" "<<b[u]<<endl;
   }
   return 0;
}


#include<iostream>
using namespace std;
/*void minimum(int x){

}*/
int main(){
    int n, c[20000], b[20000];
    cin>>n;
    int a[n];
   for(int u=0; u<n; u++){
    cin>>a[u];
   }
int k=0, p=0, z=0, d=-1;
   int minimum;
   while(k!=n-1){
       minimum=a[k];
      for(int y=k+1; y<n; y++){
        if(a[y]<minimum){
            minimum=a[y];
            z=y;
            }
    }
if(a[k]!=minimum){
int f;
f=a[k]; a[k]=minimum; a[z]=f;
p++; d++; c[d]=k; b[d]=z;
}
k++;
}
cout<<p<<endl;
for(int u=0; u<=d; u++){
    cout<<c[u]<<" "<<b[u]<<endl;
   }
   return 0;
}


#include<iostream>
using namespace std;
/*void minimum(int x){

}*/
int main(){
    int n, b[20000];
    cin>>n;
    int a[n];
   for(int u=0; u<n; u++){
    cin>>a[u];
   }
int k=0, p=0, z=0, d=-1;
   int minimum;
   while(k!=n-2){
       minimum=a[k];
      for(int y=k+1; y<n; y++){
        if(a[y]<minimum){
            minimum=a[y];
            z=y;
            }
    }
if(a[k]!=minimum){
int f;
f=a[k]; a[k]=minimum; a[z]=f;
p++; d++; b[d]=k; d++; b[d]=z;
}
k++;
}
cout<<p<<endl;
for(int u=0; u<d; u+=2){
    cout<<b[u]<<" "<<b[u+1]<<endl;
   }
   return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define sz size()
#define pb push_back
#define po pop_back
#define fr first
#define sc second

using namespace std;
typedef pair<int , int > pii;
typedef long long int ll;

const int N= 3e3+10;
vector<pii > vp, vp2;

long long A;
int  m, n, t, f[N];
bool mark[N][N], a;

int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL),cerr<<"";
    cin>>n;     for(int i=0; i<n; i++)  cin>>f[i];
    cout<<n;
    for(int i=0; i<n; i++){
        int ja = i;
        for(int j=i+1; j<n; j++){
            if( f[ja]<f[j] ) ja=j;
        }
        cout<<f[i]<< "  "<<f[ja]<< '\n';
        swap(f[i], f[ja]);
    }
}
//489A


#include<bits/stdc++.h>
#define mp make_pair
#define sz size()
#define pb push_back
#define po pop_back
#define fr first
#define sc second

using namespace std;
typedef pair<int , int > pii;
typedef long long int ll;

const int N= 3e3+10;
vector<pii > vp;

long long A;
int  m, n, t;
bool mark[N][N], a;

int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL),cerr<<"";
    cin>>n;     for(int i=0; i<n; i++){ int a; cin>>a; vp.pb(mp(a, i));}
    sort(vp.begin(), vp.end());
    for(int i=0; i<n; i++){
        if(vp[i].sc != i && !mark[vp[i].sc][i] ){
            cout<<vp[i].sc<<"  "<<i<< '\n';
            mark[i][vp[i].sc] = true;  mark[vp[i].sc][i] = true;
            a= true;
        }
    }
    if(!a)return cout<<0,0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define sz size()
#define pb push_back
#define po pop_back
#define fr first
#define sc second

using namespace std;
typedef pair<int , int > pii;
typedef long long int ll;

const int N= 3e3+10;
vector<pii > vp, vp2;

long long A;
int  m, n, t;
bool mark[N][N], a;

int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL),cerr<<"";
    cin>>n;     for(int i=0; i<n; i++){ int a; cin>>a; vp.pb(mp(a, i));}
    sort(vp.begin(), vp.end());
    for(int i=0; i<n; i++){
        if(vp[i].sc != i && !mark[vp[i].sc][i] ){
            vp2.pb(mp(vp[i].sc,i));
            mark[i][vp[i].sc] = true;  mark[vp[i].sc][i] = true;
            a= true;
        }
    }
    if(!a)return cout<<0,0;
    cout<<vp2.sz<< '\n';
    for(int i=0; i<vp2.sz ;i++){
        cout<<vp2[i].fr<< "  "<<vp2[i].sc<<'\n';
    }
}


#include<bits/stdc++.h>
#define mp make_pair
#define sz size()
#define pb push_back
#define po pop_back
#define fr first
#define sc second

using namespace std;
typedef pair<int , int > pii;
typedef long long int ll;

const int N= 3e3+10;
vector<pii > vp, vp2;

long long A;
int  m, n, t, f[N], ja;
bool mark[N][N], a;

int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL),cerr<<"";
    cin>>n;     for(int i=0; i<n; i++)  cin>>f[i];
    cout<<n<<'\n';
    for(int i=0; i<n; i++){
        ja = i;
        for(int j=i; j<n; j++){
            if( f[j]<f[ja] ) ja=j;
        }
        cout<<i<< "  "<<ja<< '\n';
        swap(f[i], f[ja]);
    }
}
//489A


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

int main() {
    FIO;
    ll n,i,j;
    ll cnt = 0;
    cin >> n;
    ll a[n];

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

    vector<pair<ll,ll>> v;

    ll ind;

    for(i = 0 ;i < n;i++){
        ind = i;
        for(j = i;j < n;j++){
            if(a[j] < a[ind]) ind = j;
        }

        if(i != ind){
            cnt ++;
            v.pb({i,ind});
        }
    }
    cout << cnt << "\n";

    for(auto &it : v){
        cout << it.first << " " << it.second <<"\n";
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

int main() {
    FIO;
    ll n,i,j;
    ll cnt = 0;
    cin >> n;
    ll a[n];

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

    vector<pair<ll,ll>> v;
    for(i = 0;i < n;i++){
        for(j = i;j < n;j++){
            if(a[i] > a[j]) {
                swap(a[i],a[j]); cnt ++;
                v.pb({i,j});
            }
        }
    }
    cout << cnt << "\n";

    for(auto &it : v){
        cout << it.first << " " << it.second <<"\n";
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

int main() {
    FIO;
    ll n,i,j;
    ll cnt = 0;
    cin >> n;
    ll a[n];

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

    vector<pair<ll,ll>> v;

    ll ind;

    for(i = 0 ;i < n;i++){
        ind = i;
        for(j = i;j < n;j++){
            if(a[j] < a[ind]) ind = j;
        }

        if(i != ind){
            cnt ++;
            v.pb({i,ind});
            swap(a[i],a[ind]);
        }
    }
    cout << cnt << "\n";

    for(auto &it : v){
        cout << it.first << " " << it.second <<"\n";
    }
}


///created: 1/Jul/2019 18:22:05

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int modulo = 1e9 + 7;

#ifdef LOCAL_DEBUG_NTV1612
template<class __Ty>
string to_string(__Ty a) {
	string res = "[ ";
	bool f = true;
	for (auto &p : a) { res += (f ? string("") : string(", ")) + to_string(p); f = false;	}
	return res + " ]";
}
void _debug() { fprintf(stderr, "\n"); }
template<typename __Ty_head, typename... __Ty_tail>
void _debug(__Ty_head head, __Ty_tail... tail) { fprintf(stderr, "%s ", to_string(head).c_str()); _debug(tail...); }
#define debug(...) fprintf(stderr, "DEBUG [%s]: ", #__VA_ARGS__), _debug(__VA_ARGS__)
#else
#define debug(...) 0
#endif

template<class __Ty> bool mini(__Ty& a, __Ty b) { return a > b ? ((a = b) | 1) : (0); }
template<class __Ty> bool maxi(__Ty& a, __Ty b) { return a < b ? ((a = b) | 1) : (0); }
template<class __Ty> __Ty add(__Ty a, __Ty b) { return (a + b) % (__Ty(modulo)); }
template<class __Ty> __Ty sub(__Ty a, __Ty b) { __Ty m(modulo); return ((a - b) % m + m) % m; }

int a[3005], n, k;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	vector<ii> f;
	for (int i = 1; i <= n - 1; ++i)
		for (int j = i + 1; j <= n; ++j)
			if (a[i] > a[j]) {
				swap(a[i], a[j]);
				f.push_back(ii(i - 1, j - 1));
			}
	cout << int(f.size()) << '\n';
	for (ii p : f)
		cout << p.first << ' ' << p.second << '\n';
	return 0;
}


///created: 1/Jul/2019 18:22:05

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int modulo = 1e9 + 7;

#ifdef LOCAL_DEBUG_NTV1612
template<class __Ty>
string to_string(__Ty a) {
	string res = "[ ";
	bool f = true;
	for (auto &p : a) { res += (f ? string("") : string(", ")) + to_string(p); f = false;	}
	return res + " ]";
}
void _debug() { fprintf(stderr, "\n"); }
template<typename __Ty_head, typename... __Ty_tail>
void _debug(__Ty_head head, __Ty_tail... tail) { fprintf(stderr, "%s ", to_string(head).c_str()); _debug(tail...); }
#define debug(...) fprintf(stderr, "DEBUG [%s]: ", #__VA_ARGS__), _debug(__VA_ARGS__)
#else
#define debug(...) 0
#endif

template<class __Ty> bool mini(__Ty& a, __Ty b) { return a > b ? ((a = b) | 1) : (0); }
template<class __Ty> bool maxi(__Ty& a, __Ty b) { return a < b ? ((a = b) | 1) : (0); }
template<class __Ty> __Ty add(__Ty a, __Ty b) { return (a + b) % (__Ty(modulo)); }
template<class __Ty> __Ty sub(__Ty a, __Ty b) { __Ty m(modulo); return ((a - b) % m + m) % m; }

int a[3005], n, k;
vector<ii> f;

void QuickSort(int l, int r) {
	if (l < r) {
		int key = a[rand() % (r - l + 1) + l];
		int i = l, j = r;
		while (i <= j) {
			while (a[i] < key) ++i;
			while (a[j] > key) --j;
			if (i <= j) {
				if (a[i] != a[j]) {
					swap(a[i], a[j]);
					f.push_back(ii(i, j));
				}
				++i;
				--j;
			}
		}
		if (l < j) QuickSort(l, j);
		if (i < r) QuickSort(i, r);
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	for (int i = 1; i <= n; ++i) {
		int p = i;
		for (int j = i + 1; j <= n; ++j)
			if (a[p] > a[j])
				p = j;
		if (a[p] != a[i]) {
			f.push_back(ii(i - 1, p - 1));
			swap(a[p], a[i]);
		}
	}
	cout << f.size() << '\n';
	for (ii p : f)
		cout << p.first << ' ' << p.second << '\n';
	return 0;
}


///created: 1/Jul/2019 18:22:05

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int modulo = 1e9 + 7;

#ifdef LOCAL_DEBUG_NTV1612
template<class __Ty>
string to_string(__Ty a) {
	string res = "[ ";
	bool f = true;
	for (auto &p : a) { res += (f ? string("") : string(", ")) + to_string(p); f = false;	}
	return res + " ]";
}
void _debug() { fprintf(stderr, "\n"); }
template<typename __Ty_head, typename... __Ty_tail>
void _debug(__Ty_head head, __Ty_tail... tail) { fprintf(stderr, "%s ", to_string(head).c_str()); _debug(tail...); }
#define debug(...) fprintf(stderr, "DEBUG [%s]: ", #__VA_ARGS__), _debug(__VA_ARGS__)
#else
#define debug(...) 0
#endif

template<class __Ty> bool mini(__Ty& a, __Ty b) { return a > b ? ((a = b) | 1) : (0); }
template<class __Ty> bool maxi(__Ty& a, __Ty b) { return a < b ? ((a = b) | 1) : (0); }
template<class __Ty> __Ty add(__Ty a, __Ty b) { return (a + b) % (__Ty(modulo)); }
template<class __Ty> __Ty sub(__Ty a, __Ty b) { __Ty m(modulo); return ((a - b) % m + m) % m; }

int a[3005], n, k;
vector<ii> f;

void QuickSort(int l, int r) {
	if (l < r) {
		int key = a[rand() % (r - l + 1) + l];
		int i = l, j = r;
		while (i <= j) {
			while (a[i] < key) ++i;
			while (a[j] > key) --j;
			if (i <= j) {
				if (a[i] != a[j]) {
					swap(a[i], a[j]);
					f.push_back(ii(i, j));
				}
				++i;
				--j;
			}
		}
		if (l < j) QuickSort(l, j);
		if (i < r) QuickSort(i, r);
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	QuickSort(1, n);
	assert(int(f.size()) <= n);
	cout << f.size() << '\n';
	for (ii p : f)
		cout << p.first - 1 << ' ' << p.second - 1 << '\n';
	return 0;
}


///created: 1/Jul/2019 18:22:05

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int modulo = 1e9 + 7;

#ifdef LOCAL_DEBUG_NTV1612
template<class __Ty>
string to_string(__Ty a) {
	string res = "[ ";
	bool f = true;
	for (auto &p : a) { res += (f ? string("") : string(", ")) + to_string(p); f = false;	}
	return res + " ]";
}
void _debug() { fprintf(stderr, "\n"); }
template<typename __Ty_head, typename... __Ty_tail>
void _debug(__Ty_head head, __Ty_tail... tail) { fprintf(stderr, "%s ", to_string(head).c_str()); _debug(tail...); }
#define debug(...) fprintf(stderr, "DEBUG [%s]: ", #__VA_ARGS__), _debug(__VA_ARGS__)
#else
#define debug(...) 0
#endif

template<class __Ty> bool mini(__Ty& a, __Ty b) { return a > b ? ((a = b) | 1) : (0); }
template<class __Ty> bool maxi(__Ty& a, __Ty b) { return a < b ? ((a = b) | 1) : (0); }
template<class __Ty> __Ty add(__Ty a, __Ty b) { return (a + b) % (__Ty(modulo)); }
template<class __Ty> __Ty sub(__Ty a, __Ty b) { __Ty m(modulo); return ((a - b) % m + m) % m; }

int a[3005], n, k;
vector<ii> f;

void QuickSort(int l, int r) {
	if (l < r) {
		int key = a[rand() % (r - l + 1) + l];
		int i = l, j = r;
		while (i <= j) {
			while (a[i] < key) ++i;
			while (a[j] > key) --j;
			if (i <= j) {
				if (a[i] != a[j]) {
					swap(a[i], a[j]);
					f.push_back(ii(i, j));
				}
				++i;
				--j;
			}
		}
		if (l < j) QuickSort(l, j);
		if (i < r) QuickSort(i, r);
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	QuickSort(1, n);
	cout << f.size() << '\n';
	for (ii p : f)
		cout << p.first - 1 << ' ' << p.second - 1 << '\n';
	return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E9+1;
    int index = 0;
    int temp;
    int minInd;
    int b[10];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin>> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E9+1
        for(int i=index;i<n; i++)
            if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if(i %2 == 0 )
            cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E9+1;
    int index = 0;
    int temp;
    int minInd;
    int b[10];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin>> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E9+1;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if(i %2 == 0 )
            cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E9+1;
    int index = 0;
    int temp;
    int minInd;
    int b[(2*n)+10];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin>> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E9+1;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if((i % 2) == 0 ) cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 1000000001;
    int index = 0;
    int temp;
    int minInd;
    int b[(2*n)+10];
    int c=0;
    int d=0 ;

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

    if(index != minInd)
    {
        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 1000000001;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        if(index != minInd)
        {
            temp = a[index];
            a[index]= a[minInd];
            a[minInd] =  temp;
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if((i % 2) == 0 ) cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E90+1;
    int index = 0;
    int temp;
    int minInd;
    int b[(2*n)+10];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin >> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E90+1;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if((i % 2) == 0 ) cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E9+1;
    int index = 0;
    int temp;
    int minInd;
    int b[(2*n)+10];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin>> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E9+1;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            if((i%2) != 0) cout << b[i] << " ";
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 10E9+1;
    int index = 0;
    int temp;
    int minInd;
    int b[2*n];
    int c=0;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin>> a[i];
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    temp = a[index];
    a[index]= a[minInd];
    a[minInd] =  temp;
    if(index != minInd)
    {
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 10E9+1;
        for(int i=index;i<n; i++)
        if(a[i]< MIN)
        {
            MIN = a[i];
            minInd = i;
        }

        temp = a[index];
        a[index]= a[minInd];
        a[minInd] =  temp;

        if(index != minInd)
        {
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if(i %2 == 0 )
            cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


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

int main(){

    int n;
    cin>>n;
    int a[n];
    int MIN = 1000000001;
    int index = 0;
    int temp;
    int minInd;
    int b[(2*n)+10];
    int c=-1;
    int d=0 ;

    for(int i=0;i<n; i++)
    {
        cin >> a[i];
        if(a[i] < MIN)
        {
            MIN = a[i];
            minInd = i;
        }
    }
    //cout << minInd << endl ;
    if(index != minInd)
    {
        temp = a[index];
        a[index]= a[minInd];
        a[minInd] = temp;
        c++;
        b[c] = index;
        c++;
        b[c]=minInd;
        d++;
    }

    index++;

    while(index < n){
        MIN = 1000000001;
        for(int i=index;i<n; i++) {
        if(a[i] < MIN)
        {
            MIN = a[i];
            minInd = i;
        }
        }

        if(index != minInd)
        {
            temp = a[index];
            a[index]= a[minInd];
            a[minInd] = temp;
            c++;
            b[c] = index;
            c++;
            b[c] = minInd;
            d++;
        }
        index++;
    }

    cout << d << endl;
    if(d!=0){
        for(int i=0;i<=c;i++)
        {
            cout << b[i] ;
            if((i % 2) == 0 ) cout << " " ;
            else cout << endl;
        }
    }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<string, string> ps;
typedef list<int> li;
typedef list<string> ls;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fr(i, a, b) for (int i = a; i < b; ++i)

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

    int n;cin>>n;
    vi v(n);
    fr(i,0,n) cin>>v[i];
    vector<pi> p;
    int s=0;
    for(int i=0;i<n-1;i++){
        int min = v[i];
        int in=-1;
        for(int j=i+1;j<n;j++){
            if(min>v[j]){
                min = v[j];
                in = j;
            }
        }
        if(in!=-1){
            swap(v[i],v[in]);
            p.pb(mp(i, in));
            s++;
        }
    }
    cout<<s<<endl;
    fr(i,0,p.size()){
        cout<<p[i].f<<" "<<p[i].s<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<string, string> ps;
typedef list<int> li;
typedef list<string> ls;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fr(i, a, b) for (int i = a; i < b; ++i)

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif

    int n;cin>>n;
    vi v(n);
    fr(i,0,n) cin>>v[i];
    vector<pi> p;
    int s=0;
    for(int i=0;i<n-1;i++){
        if(v[i]>=v[i+1]){
            int min = INT_MAX;
            int in;
            for(int j=i+1;j<n;j++){
                if(min>v[j]){
                    min = v[j];
                    in = j;
                }
            }
            p.pb(mp(i,in));
            swap(v[i],v[in]);
            s++;
        }
    }
    cout<<s<<endl;
    fr(i,0,p.size()){
        cout<<p[i].f<<" "<<p[i].s<<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<string, string> ps;
typedef list<int> li;
typedef list<string> ls;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fr(i, a, b) for (int i = a; i < b; ++i)

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif

    int n;cin>>n;
    vi v(n);
    fr(i,0,n) cin>>v[i];
    vector<pi> p;
    int s=0;
    for(int i=0;i<n-1;i++){
        if(v[i]>=v[i+1]){
            int min = INT_MAX;
            int in;
            for(int j=i+1;j<n;j++){
                if(min>v[j]){
                    min = v[j];
                    in = j;
                }
            }
            p.pb(mp(i,in));
            swap(v[i],v[in]);
            s++;
        }
    }
    cout<<s<<endl;
    fr(i,0,p.size()){
        cout<<p[i].f<<" "<<p[i].s<<endl;
    }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
struct hhh
{
	int p,o;
}a[3010];
bool hhhh(hhh q,hhh w)
{
	if (q.p!=w.p)return q.p<w.p;
	return q.o<w.o;
}
using namespace std;
int main()
{
	int b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m].p=b[m];a[m].o=m;
	  }
	sort(a,a+c,hhhh);
	for (int m=0;m<c-1;m++)
	  if (a[m].p!=b[m]||a[m].o!=b[m].o)
	    	{
	    	  	d[++ans]=m;e[ans]=a[m].o;
	    	    swap(b[a[m].o],b[m]);
	    	    for (int n=0;n<c;n++)
	    	      if (a[n].o==m)
	    	        {
	    	        	a[n].o=a[m].o;break;
	    	        }
	    	}
	cout<<ans<<endl;
	for (int m=1;m<=ans;m++)
	  printf("%d %d\n",d[m],e[m]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
struct hhh
{
	int p,o;
}a[3010];
bool hhhh(hhh q,hhh w)
{
	if (q.p!=w.p)return q.p<w.p;
	return q.o<w.o;
}
using namespace std;
int main()
{
	int b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m].p=b[m];a[m].o=m;
	  }
	sort(a,a+c,hhhh);
	for (int m=0;m<c-1;m++)
	  if (a[m].p!=b[m]||a[m].o!=m)
	    	{
	    	  	d[++ans]=m;e[ans]=a[m].o;
	    	    swap(b[a[m].o],b[m]);
	    	    for (int n=0;n<c;n++)
	    	      if (a[n].o==m)
	    	        {
	    	        	a[n].o=a[m].o;break;
	    	        }
	    	}
	cout<<ans<<endl;
	for (int m=1;m<=ans;m++)
	  printf("%d %d\n",d[m],e[m]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main()
{
	int a[3010],b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m]=b[m];
	  }
	sort(a,a+c);
	for (int m=0;m<c-1;m++)
	  if (a[m]!=b[m])
	    for (int n=m+1;n<c;n++)
	    	if (b[n]==a[m])
	    	  {
	    	  	d[++ans]=m;e[ans]=n;swap(b[n],b[m]);break;
	    	  }
	cout<<ans;
	for (int m=1;m<=ans;m++)
	  printf("\n%d %d",d[m],e[m]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
struct hhh
{
	int p,o;
}a[3010];
bool hhhh(hhh q,hhh w)
{
	if (q.p!=w.p)return q.p<w.p;
	return q.o<w.o;
}
using namespace std;
int main()
{
	int b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m].p=b[m];a[m].o=m;
	  }
	sort(a,a+c,hhhh);
	for (int m=0;m<c-1;m++)
	  if (a[m].p!=b[m])
	    	{
	    	  	d[++ans]=m;e[ans]=a[m].o;
	    	    swap(b[a[m].o],b[m]);
	    	    for (int n=m+1;n<c;n++)
	    	      if (a[n].o==m)
	    	        {
	    	        	a[n].o=a[m].o;break;
	    	        }
	    	}
	cout<<ans<<endl;
	for (int m=1;m<=ans;m++)
	  printf("%d %d\n",d[m],e[m]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
struct hhh
{
	int p,o;
}a[3010];
bool hhhh(hhh q,hhh w)
{
	if (q.p!=w.p)return q.p<w.p;
	return q.o<w.o;
}
using namespace std;
int main()
{
	int b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m].p=b[m];a[m].o=m;
	  }
	sort(a,a+c,hhhh);
	for (int m=0;m<c-1;m++)
	  if (a[m].p!=b[m])
	    	{
	    	  	d[++ans]=m;e[ans]=a[m].o;
	    	    swap(b[a[m].o],b[m]);
	    	    for (int n=0;n<c;n++)
	    	      if (a[n].o==m)
	    	        {
	    	        	a[n].o=a[m].o;break;
	    	        }
	    	}
	cout<<ans<<endl;
	for (int m=1;m<=ans;m++)
	  printf("%d %d\n",d[m],e[m]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main()
{
	int a[3010],b[3010],c,d[3010],e[3010],ans=0;
	cin>>c;
	for (int m=0;m<c;m++)
	  {
	  	scanf("%d",&b[m]);
	  	a[m]=b[m];
	  }
	sort(a,a+c);
	for (int m=0;m<c-1;m++)
	  if (a[m]!=b[m])
	    for (int n=m+1;n<c;n++)
	    	if (b[n]==a[m])
	    	  {
	    	  	d[++ans]=m;e[ans]=n;swap(b[n],b[m]);
	    	  }
	cout<<ans;
	for (int m=1;m<=ans;m++)
	  printf("\n%d %d",d[m],e[m]);
}


#include <bits/stdc++.h>
#include <chrono>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define eps 1e-9

typedef long long ll;

using namespace std;

const int mod = 1e9+7;
const int N = 1e5+5;
//int a[N];

int main()

{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k = 0;

    cin >> n;

    int arr[n];
    pair <int, int> sortedArr[n];
    pair <int ,int> ans[n];

    for (int i = 0; i < n; i++) {

        cin >> arr[i];
        sortedArr[i].first = arr[i];
        sortedArr[i].second = i;
    }

    sort(sortedArr, sortedArr+n);

    for (int i = 0; i < n; i++) {

        if (arr[i] != sortedArr[i].first) {

            ans[k].first = i;
            ans[k].second = sortedArr[i].second;
            k++;
            swap(arr[i], arr[sortedArr[i].second]);
        }
    }

    cout << k << "\n";

    for (int i = 0; i < k; i++) {

        cout << ans[i].first << " " << ans[i].second;
        cout << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include <chrono>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define eps 1e-9

typedef long long ll;

using namespace std;

const int mod = 1e9+7;
const int N = 1e5+5;
//int a[N];

int main()

{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k = 0;

    cin >> n;

    int arr[n];
    pair <int, int> sortedArr[n];
    pair <int ,int> ans[n];
    pair <int, int> ind[n];

    for (int i = 0; i < n; i++) {

        cin >> arr[i];
        sortedArr[i].first = arr[i];
        sortedArr[i].second = i;
        ind[i].first = i;
        ind[i].second = arr[i];
    }

    sort(sortedArr, sortedArr+n);

    for (int i = 0; i < n; i++) {

        if (arr[i] != sortedArr[i].first) {

            ans[k].first = i;
            ans[k].second = sortedArr[i].second;
            k++;
            swap(arr[i], arr[sortedArr[i].second]);

            for (int j = 0; j < n; j++) {

                if (sortedArr[j].first == arr[i]) {

                    sortedArr[j].second = ans[k].second;
                    sortedArr[i].second = ans[k].first;
                    break;
                }
            }
        }
    }

    cout << k << "\n";

    for (int i = 0; i < k; i++) {

        cout << ans[i].first << " " << ans[i].second;
        cout << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define eps 1e-9

typedef long long ll;

using namespace std;

const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 1e5+5;

int main()

{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, ans = 0, ind1, ind2;

    cin >> n;

    int arr[n], sortArr[n];

    for (int i = 0; i < n; i++) {

        cin >> arr[i];
        sortArr[i] = arr[i];
    }

    sort(sortArr, sortArr+n);

    vector <pair <int, int>> v;

    for (int i = 0; i < n; i++) {

        if (arr[i] != sortArr[i]) {

            ans++;
            ind1 = i;

            for (int j = i+1; j < n; j++) {

                if (arr[j] == sortArr[i]) {

                    ind2 = j;
                    break;
                }
            }

            swap(arr[ind1], arr[ind2]);

            v.push_back(make_pair(ind1, ind2));
        }
    }

    cout << ans << "\n";

    for (auto i : v) {

        cout << i.first << " " << i.second << "\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main()
{int n,k=0;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
  b[i]=a[i];
}
sort(a,a+n);
    int c[n][2];
    for(int i=0;i<n;i++)
    { c[i][0]=-1;
      c[i][1]=-1;
    }
for(int i=0;i<n;i++)
{
if(b[i]!=a[i])
    {for(int j=i+1;j<n;j++)
        {if(a[i]==b[j])
            { c[i][0]=i;
               c[i][1]=j;
               k++;
            swap(b[i],b[j]);
            }
        }
    }
}
if(k>n)
    cout<<n<<endl;
else
cout<<k<<endl;
for(int i=0;i<n;i++)
{if(c[i][0]!=-1 && c[i][1]!=-1)
    cout<<c[i][0]<<' '<<c[i][1]<<endl;
}

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main()
{int n,k=0;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
  b[i]=a[i];
}
sort(a,a+n);
    int c[n][2];
    for(int i=0;i<n;i++)
    { c[i][0]=-1;
      c[i][1]=-1;
    }
for(int i=0;i<n;i++)
{
if(b[i]!=a[i])
    {for(int j=i+1;j<n;j++)
        {if(a[i]==b[j])
            { c[i][0]=i;
               c[i][1]=j;
               k++;
            swap(b[i],b[j]);
            if(k==n)
                break;
            }
        }
    }
}
cout<<k<<endl;
for(int i=0;i<n;i++)
{if(c[i][0]!=-1 && c[i][1]!=-1)
    cout<<c[i][0]<<' '<<c[i][1]<<endl;
}

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main()
{int n,k=0;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
  b[i]=a[i];
}
sort(a,a+n);
    int c[n][2];
    for(int i=0;i<n;i++)
    { c[i][0]=-1;
      c[i][1]=-1;
    }
for(int i=0;i<n;i++)
{if(b[i]!=a[i])
    {for(int j=i+1;j<n;j++)
        {if(a[i]==b[j])
            { c[i][0]=i;
               c[i][1]=j;
               k++;
            swap(b[i],b[j]);}
        }
    }
}
cout<<k<<endl;
for(int i=0;i<n;i++)
{if(c[i][0]!=-1 && c[i][1]!=-1)
    cout<<c[i][0]<<' '<<c[i][1]<<endl;
}

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main()
{int n,k=0;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
  b[i]=a[i];
}
sort(a,a+n);
    int c[n][2];
    for(int i=0;i<n;i++)
    { c[i][0]=-1;
      c[i][1]=-1;
    }
for(int i=0;i<n;i++)
{
if(b[i]!=a[i])
    {for(int j=i+1;j<n;j++)
        {if(a[i]==b[j])
            { c[i][0]=i;
               c[i][1]=j;
               k++;
            swap(b[i],b[j]);
            break;
            }
        }
    }
}
cout<<k<<endl;
for(int i=0;i<n;i++)
{if(c[i][0]!=-1 && c[i][1]!=-1)
    cout<<c[i][0]<<' '<<c[i][1]<<endl;
}

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main()
{int n,k=0;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{ cin>>a[i];
  b[i]=a[i];
}
sort(a,a+n);
    int c[n][2];
    for(int i=0;i<n;i++)
    { c[i][0]=-1;
      c[i][1]=-1;
    }
for(int i=0;i<n;i++)
{if(k==n)
goto p;
if(b[i]!=a[i])
    {for(int j=i+1;j<n;j++)
        {if(a[i]==b[j])
            { c[i][0]=i;
               c[i][1]=j;
               k++;
            swap(b[i],b[j]);}
        }
    }
}
p:
cout<<k<<endl;
for(int i=0;i<n;i++)
{if(c[i][0]!=-1 && c[i][1]!=-1)
    cout<<c[i][0]<<' '<<c[i][1]<<endl;
}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,count=0;
    cin>>n;
    int arr[n],temp[n];
    for (int i = 0; i <n ; ++i) {
        cin>>arr[i];
        temp[i]=arr[i];
        if(arr[i-1]>arr[i])
            count++;
    }
    sort(temp,temp+n);
    cout<<count<<endl;
    for (int j = 0; j <n ; ++j) {
        if(arr[j]==temp[j])
            continue;
        else
            for (int i = j+1; i <n ; ++i) {
                if(arr[i]==temp[j])
                {  swap(arr[i],arr[j]);
                cout<<j<<" "<<i<<endl;
                break;}
            }
    }

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, count = 0;
    cin >> n;
    int arr[n], temp[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        temp[i] = arr[i];
    }
    sort(temp, temp + n);
    map<int,int> m;
    for (int j = 0; j < n; ++j) {
        if (arr[j] == temp[j])
            continue;
        else
            for (int i = j + 1; i < n; ++i) {
                if (arr[i] == temp[j]) {
                    swap(arr[i], arr[j]);
                    count++;
                    m.insert({j,i});
                    break;
                }
            }
    }
    cout<<count<<endl;
    for (auto & it : m)
        cout << it.first << " " << it.second << endl;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,count=0;
    cin>>n;
    int arr[n],temp[n];
    for (int i = 0; i <n ; ++i) {
        cin>>arr[i];
        temp[i]=arr[i];
        if(arr[i-1]>arr[i]&&i>0)
            count++;
    }
    sort(temp,temp+n);
    cout<<count<<endl;
    for (int j = 0; j <n ; ++j) {
        if(arr[j]==temp[j])
            continue;
        else
            for (int i = j+1; i <n ; ++i) {
                if(arr[i]==temp[j])
                {  swap(arr[i],arr[j]);
                cout<<j<<" "<<i<<endl;
                break;}
            }
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
int arr[100005];
int arr2[100005];
//ll Pow(ll base , ll power)
//void sieve_eratosthenes(int n);
vector<int>v;
map<int,int>m;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,l_i=0,h_i=0,low=0,flag =0,count=0;
cin>>n;
for(int i=0;i<n;i++)scanf("%d",&arr[i]);
for(int i=0;i<=n-1;i++)
{
    low = arr[i];
    flag=0;
    l_i =  i;
    for(int j=i+1;j<n;j++)
    {
        if(arr[j]<low)
        {
            low = min(low,arr[j]);
            l_i = j;
            flag=1;
        }
    }
    if(flag){
        count++;
        v.push_back(l_i);
        m[l_i]=i;

    }
    swap(arr[l_i],arr[i]);
}
cout<<count<<endl;
for(int i=0;i<count;i++)
{
    cout<<v[i]<<" "<<m[v[i]]<<endl;
}
return 0 ;

}

/*
void sieve_eratosthenes(int n)
    {
bool primes[n+2]={false};
for(int i=3;i<=n;i+=2)
    primes[i]=1;
    primes[2]=1;
for(int i=3;i*i<=n;i+=2)
{
    if(primes[i]==1){
    for(int j=i*i;j<=n;j+=i)
        primes[j]=0;
    }
}
for(int i=2;i<=n;i++)
    if(primes[i]==1)
    cout<<i<<endl;
return;
    }
ll Pow(ll base , ll power)
        {
            if(power==0)return 1;
            ll x = Pow(base,power/2);
            x*=x;
            if(power&1)
                return base*x;
            else
                return x;
        }
*/


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
int arr[100005];
int arr2[100005];
//ll Pow(ll base , ll power)
//void sieve_eratosthenes(int n);
vector<int>v;
map<int,int>m;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,l_i=0,h_i=0,low=0,flag =0,cnt=0;
cin>>n;
for(int i=0;i<n;i++)cin>>arr[i];
for(int i=0;i<n-1;i++)
{
    low = arr[i];
    flag=0;
    l_i =  i;
    for(int j=i+1;j<n;j++)
    {
        if(arr[j]<low)
        {
            low = min(low,arr[j]);
            l_i = j;
            flag=1;
        }
    }
    if(flag){
        cnt++;
        v.push_back(l_i);
        m[l_i]=i;

    }
    swap(arr[l_i],arr[i]);
}
cout<<cnt<<endl;
for(int i=0;i<cnt;i++)
{
    cout<<v[i]<<" "<<m[v[i]]<<endl;
}
return 0 ;

}

/*
void sieve_eratosthenes(int n)
    {
bool primes[n+2]={false};
for(int i=3;i<=n;i+=2)
    primes[i]=1;
    primes[2]=1;
for(int i=3;i*i<=n;i+=2)
{
    if(primes[i]==1){
    for(int j=i*i;j<=n;j+=i)
        primes[j]=0;
    }
}
for(int i=2;i<=n;i++)
    if(primes[i]==1)
    cout<<i<<endl;
return;
    }
ll Pow(ll base , ll power)
        {
            if(power==0)return 1;
            ll x = Pow(base,power/2);
            x*=x;
            if(power&1)
                return base*x;
            else
                return x;
        }
*/


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
int arr[100005];
int arr2[100005];
//ll Pow(ll base , ll power)
//void sieve_eratosthenes(int n);
vector<int>v;
map<int,int>m;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,l_i=0,h_i=0,low=0,flag =0,cnt=0;
cin>>n;
for(int i=0;i<n;i++)scanf("%d",&arr[i]);
for(int i=0;i<=n-1;i++)
{
    low = arr[i];
    flag=0;
    l_i =  i;
    for(int j=i+1;j<n;j++)
    {
        if(arr[j]<low)
        {
            low = min(low,arr[j]);
            l_i = j;
            flag=1;
        }
    }
    if(flag){
        cnt++;
        v.push_back(l_i);
        m[l_i]=i;

    }
    swap(arr[l_i],arr[i]);
}
cout<<cnt<<endl;
for(int i=0;i<cnt;i++)
{
    cout<<v[i]<<" "<<m[v[i]]<<endl;
}
return 0 ;

}

/*
void sieve_eratosthenes(int n)
    {
bool primes[n+2]={false};
for(int i=3;i<=n;i+=2)
    primes[i]=1;
    primes[2]=1;
for(int i=3;i*i<=n;i+=2)
{
    if(primes[i]==1){
    for(int j=i*i;j<=n;j+=i)
        primes[j]=0;
    }
}
for(int i=2;i<=n;i++)
    if(primes[i]==1)
    cout<<i<<endl;
return;
    }
ll Pow(ll base , ll power)
        {
            if(power==0)return 1;
            ll x = Pow(base,power/2);
            x*=x;
            if(power&1)
                return base*x;
            else
                return x;
        }
*/


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
int arr[100005];
int arr2[100005];
//ll Pow(ll base , ll power)
//void sieve_eratosthenes(int n);
vector<int>v;
vector<int>f;
vector<int>l;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,l_i=0,h_i=0,low=0,flag =0,cnt=0;
cin>>n;
for(int i=0;i<n;i++)cin>>arr[i];
for(int i=0;i<n-1;i++)
{
    low = arr[i];
    flag=0;
    l_i =  i;
    for(int j=i;j<n;j++)
    {
        if(arr[j]<low)
        {
            low = min(low,arr[j]);
            l_i = j;
            flag=1;
        }
    }
    if(flag){
        cnt++;
        f.push_back(l_i);
        l.push_back(i);
    }
    swap(arr[l_i],arr[i]);
}
cout<<cnt<<endl;
for(int i=0;i<cnt;i++)
{
    cout<<f[i]<<" "<<l[i]<<endl;
}
return 0 ;

}

/*
void sieve_eratosthenes(int n)
    {
bool primes[n+2]={false};
for(int i=3;i<=n;i+=2)
    primes[i]=1;
    primes[2]=1;
for(int i=3;i*i<=n;i+=2)
{
    if(primes[i]==1){
    for(int j=i*i;j<=n;j+=i)
        primes[j]=0;
    }
}
for(int i=2;i<=n;i++)
    if(primes[i]==1)
    cout<<i<<endl;
return;
    }
ll Pow(ll base , ll power)
        {
            if(power==0)return 1;
            ll x = Pow(base,power/2);
            x*=x;
            if(power&1)
                return base*x;
            else
                return x;
        }
*/


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
int n;
int num[3005];
int Min,pos;
int main()
{
	scanf("%d",&n);
	for(int i = 0;i < n;i++)
		scanf("%d",&num[i]);
	printf("%d\n",n - 1);
	for(int i = 0;i < n - 1 ;i++){
		Min = num[i];
		for(int j = 0;j < n;j++){
			if(num[j] < Min){
				pos = j;
				Min = num[j];
			}
		}
		printf("%d %d\n",i,pos);
		num[pos] = num[i];
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
int n;
int num[3005];
int Min,pos;
int main()
{
	scanf("%d",&n);
	for(int i = 0;i < n;i++)
		scanf("%d",&num[i]);
	printf("%d\n",n - 1);
	for(int i = 0;i < n - 1 ;i++){
		Min = num[i];
		pos = i;
		for(int j = i;j < n;j++){
			if(num[j] < Min){
				pos = j;
				Min = num[j];
			}
		}
		printf("%d %d\n",i,pos);
		num[pos] = num[i];
	}
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;
struct NODE{
	int pos;
	int w;
}num[3005];
int n;
int head[30005],tail[30005],flag[3005];
bool cmp(NODE x,NODE y){
	if(x.w < y.w) return true;
	else return false;
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%d",&num[i].w);
		num[i].pos = i;
	}
	sort(num + 1,num + n + 1,cmp);
	for(int i = 1;i <= n;i++){
		if(num[i].pos != i&&!flag[num[i].pos]&&!flag[i]){
			head[++head[0]] = i - 1;
			tail[++tail[0]] = num[i].pos - 1;
			flag[i] = 1;
		}
	}
	printf("%d\n",head[0]);
	for(int i = 1;i <= head[0];i++)
		printf("%d %d\n",head[i],tail[i]);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;
struct NODE{
	int pos;
	int w;
}num[3005];
int n;
int head[30005],tail[30005],flag[3005];
bool cmp(NODE x,NODE y){
	if(x.w < y.w) return true;
	else return false;
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%d",&num[i].w);
		num[i].pos = i;
	}
	sort(num + 1,num + n + 1,cmp);
	for(int i = 1;i <= n;i++){
		if(num[i].pos != i&&!flag[num[i].pos]&&!flag[i]){
			head[++head[0]] = i - 1;
			tail[++tail[0]] = num[i].pos - 1;
	//		flag[num[i].pos] = 1;
	//		flag[i] = 1;
		}
	}
	printf("%d\n",head[0]);
	for(int i = 1;i <= head[0];i++)
		printf("%d %d\n",head[i],tail[i]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[3005]={0};
    for(int i=0; i<n; i++)
        cin>>a[i];
    vector<pair<int , int> >v;
    for(int i=0; i<n; i++){
            pair<int, int> tmp;
            int t=a[i];
        for(int j=i+1; j<n; j++)
        if(t>a[j]){
        t=a[j];
        tmp={i , j};
        }
        if(tmp.first!=0 || tmp.second!=0){
        v.push_back({tmp.first , tmp.second});
        int sw=a[tmp.first];
        a[tmp.first]=a[tmp.second];
        a[tmp.second]=sw;
        }
    }
    cout<<v.size()<< endl;
    for(int i=0; i<v.size(); i++)
        cout<<v[i].first<<" "<<v[i].second<< endl;
    /*for(int i=0; i<n; i++)
        cout<<a[i]<<" ";*/

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi pair<int , int>
bool so(pi x , pi y)
{
    if(x.first==y.first)
        return x.second<y.second;
    return x.first<y.first;
}
int main()
{
    int n;
    cin>>n;
    vector<pi> v;  // it`s better than map cuz it help me in duplication
    int a[3005];
    for(int i=0; i<n; i++){
        cin>>a[i];
        v.push_back({a[i] , i});
    }
    sort(v.begin() , v.end() , so);
    vector<pi> res;
    for(int i=0; i<n; i++)
    {
        if(a[i]==v[i].first)
            continue;
        else
        {
            int in=v[i].second;
            int sw=a[i];
            res.push_back({i , in});
            a[i]=v[i].first;
            a[in]=sw;
        }
    }
    cout<<res.size()<< endl;
    for(int i=0; i<res.size(); i++)
        cout<<res[i].first<<" "<<res[i].second<< endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[3005]={0};
    for(int i=0; i<n; i++)
        cin>>a[i];
    vector<pair<int , int> >v;
    for(int i=0; i<n; i++){
            pair<int, int> tmp;
            int t=a[i]; // new sub problem
        for(int j=i+1; j<n; j++)
        if(t>a[j]){  // it`s about getting the minimum placing it in i-th place and
                //going to the next new subproblem ignoring the previous ones
                //making new i as the new start and the min comes from the next element
                //we have form i till n
        t=a[j];
        tmp={i , j};
        }
        if(tmp.first!=0 || tmp.second!=0){
        v.push_back({tmp.first , tmp.second});
        int sw=a[tmp.first];
        a[tmp.first]=a[tmp.second];
        a[tmp.second]=sw;
        }
    }
    cout<<v.size()<< endl;
    for(int i=0; i<v.size(); i++)
        cout<<v[i].first<<" "<<v[i].second<< endl;
    /*for(int i=0; i<n; i++)
        cout<<a[i]<<" ";*/

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    map<int , int> index;
    int a1[3005]={0} , a2[3005]={0};
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a1[i];
        a2[i]=a1[i];
        index[a1[i]]=i;
    }
    sort(a2 , a2+n);
    vector<pair<int , int> > v;
    for(int i=0; i<n; i++)
    {
        if(a1[i]==a2[i])
            continue;
        int in=index[a2[i]];
        // store swapping
        v.push_back({i ,in});
        int sw=a1[i];
        a1[i]=a2[i];
        a1[in]=sw;
        // update index
        index[a1[i]]=i;
        index[a1[in]]=in;
    }
    cout<<v.size()<< endl;
    for(int i=0; i<v.size(); i++)
        cout<<v[i].first<<" "<<v[i].second<< endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=101100;

typedef pair<int,int> pii;

pii a[NN],b[NN];

#define x first
#define y second

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&a[i].x);
		a[i].y=i;
		b[i]=a[i];
	}

	sort(b,b+n);

	cout<<n<<endl;
	for(int i=0;i<n;i++){
		int f=1;
		for(int j=i+1;j<n;j++){
			if(a[i]==b[j]){
				f=0;
				cout<<i<<" "<<j<<endl;
				break;
			}
		}
		if(f)cout<<"0 0"<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=101100;

typedef pair<int,int> pii;

pii a[NN],b[NN];

#define x first
#define y second

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&a[i].x);
		a[i].y=i;
		b[i]=a[i];
	}

	sort(b,b+n);
	int ans=0;
	for(int i=0;i<n;i++)if(a[i].x>b[i].x)ans++;

	cout<<ans<<endl;
	for(int i=0;i<n;i++)if(a[i].x>b[i].x)cout<<a[i].y<<" "<<b[i].y<<endl;

	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int a[3033], p[3033];
int id[3033];

void sw(int u, int v) {
	cout << u <<' '<<v<<endl;
	swap(p[id[u]], p[id[v]]);
	swap(id[u], id[v]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r",  stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	for(int i=0; i<n; i++) cin >> a[i], p[i] = i;
	sort(p, p+n, [](int u, int v) {
		return a[u] < a[v];
	});
	for(int i=0; i<n; i++) id[p[i]] = i;
	cout << n << endl;
	for(int i=0; i<n; i++)
	{
		sw(i, p[i]);
	}
}


#include <bits/stdc++.h>
#define NN 100100
#define x first
#define y second
using namespace std;

typedef pair<int, int>pii;
int a[NN];
pii p[NN];
vector<pii> ans;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		p[i] = pii(a[i], i);
	}
	sort(p + 1, p + n + 1);
	for(int i = 1; i <= n; i++){
		if(p[i].y == i) continue;
		ans.push_back(pii(i, p[i].y));
		int t = lower_bound(p + 1, p + n + 1, pii(a[i], i)) - p;
		swap(p[i], p[t]);
	}
	printf("%d\n", ans.size());
	for(auto v : ans){
		printf("%d %d\n", v.x-1, v.y-1);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define NN 110000
#define MM 110000
#define inf 0x3f3f3f3f
#define x first
#define y second
vector <pair<int, int> > ans;
pair <int, int > p[3300];
int id[3300], pos[3300];
int main(){
#ifndef ONLINE_JUDGE
	freopen("A.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n; scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d", &p[i].x), p[i].y = i;
	sort(p, p + n);
	for(int i = 0; i < n; i++) id[i] = p[i].y, pos[p[i].y] = i;
	for(int i = 0; i < n; i++){
		if(i == id[i]) continue;
		ans.push_back({i, id[i]});
		id[pos[i]] = id[i];
	}
	printf("%d\n", ans.size());
	for(int i = 0; i < ans.size(); i++){
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
}


#include<bits/stdc++.h>

using namespace std;

using INT = long long;

#define NN 30100

int a[NN], b[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	vector<pair<int, int> > ans;
	int n; cin >> n;
	for(int i=1; i<=n; i++) scanf("%d", a+i), b[i] = a[i];
	sort(b+1, b+n+1);
	for(int i=1; i<=n; i++) if(b[i] != a[i]) {
		for(int j=i; j<=n; j++) if(a[j] == b[i]) {
			ans.push_back({i, j});
			swap(a[i], a[j]);
			break;
		}
	}
	cout << ans.size() << endl;
	for(auto p : ans) printf("%d %d\n",  p.first-1, p.second-1);
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int a[3033], p[3033];
int id[3033];

void sw(int u, int v) {
	cout << u <<' '<<v<<endl;
	swap(id[u], id[v]);
	swap(p[u], p[v]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r",  stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	for(int i=0; i<n; i++) cin >> a[i], id[i] = i;
	sort(id, id+n, [](int u, int v) {
		return a[u] < a[v];
	});
	for(int i=0; i<n; i++) p[id[i]] = i;
	cout << n << endl;
	for(int i=0; i<n; i++)
	{
		sw(i, p[i]);
	}
}


#include <bits/stdc++.h>
#define NN 3100

using namespace std;
typedef long long INT;
typedef pair<int, int>pii;

int a[NN], pos[NN], id[NN];

pii p[NN];

int main (){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)cin>>a[i];
	set<pii>s;
	set<pii> :: iterator it;
	for(int i=1; i<=n; i++)s.insert(pii(a[i], i));
	vector<pii>ans;
	int id=1;
	while(!s.empty()){
		int u=(*s.begin()).second, val=(*s.begin()).first;
		it=s.begin();
		while((*it).second!=id)it++;
		ans.push_back(pii(id, u));
		int v=(*it).first;
		s.erase(*it);
		s.insert(pii(v, u));
		s.erase(pii(val, u));
		id++;
	}
	cout<<ans.size()<<endl;
	for(auto p:ans)cout<<p.first<<" "<<p.second<<endl;

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

int a[3030];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

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

	printf("%d\n", n);
	for(int i=1; i<=n; i++) {
		int mx=*max_element(a+i, a+n+1);
		for(int j=i; j<=n; j++) if(a[j]==mx) {
			printf("%d %d\n", i, j);
			swap(a[i], a[j]);
			break;
		}
	}
}


#include <bits/stdc++.h>
#define NN 3100

using namespace std;
typedef long long INT;
typedef pair<int, int>pii;

int a[NN], pos[NN], id[NN];

pii p[NN];

int main (){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)cin>>a[i];
	set<pii>s;
	set<pii> :: iterator it;
	for(int i=1; i<=n; i++)s.insert(pii(a[i], i));
	vector<pii>ans;
	int id=1;
	while(!s.empty()){
		int u=(*s.begin()).second, val=(*s.begin()).first;
		it=s.begin();
		while((*it).second!=id)it++;
		ans.push_back(pii(id, u));
		int v=(*it).first;
		s.erase(*it);
		s.insert(pii(v, u));
		s.erase(pii(val, u));
		id++;
	}
	cout<<ans.size()<<endl;
	for(auto p:ans)cout<<p.first-1<<" "<<p.second-1<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=101100;

typedef pair<int,int> pii;

pii a[NN],b[NN];

#define x first
#define y second

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&a[i].x);
		a[i].y=i;
		b[i]=a[i];
	}

	sort(a,a+n);

	cout<<n<<endl;
	for(int i=0;i<n;i++){
		int f=1;
		for(int j=0;j<n;j++){
			if(a[i]==b[j]){
				f=0;
				cout<<i<<" "<<j<<endl;
				swap(b[i],b[j]);
				break;
			}
		}
		if(f)cout<<"0 0"<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back

using namespace std;
typedef pair<int, int> pii;

#define NN 3010
int a[NN], b[NN];
vector<pii> ans;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b+1, b+n+1);
  	for(int i=1; i<=n; i++){
		int id;
		for(int j=i; j<=n; j++){
			if(b[i] == a[j]){
				id = j;
				break;
			}
		}
		ans.pb(pii(i, id));
		swap(a[id], a[i]);
	}
	printf("%d\n", ans.size());
	for(pii u : ans){
		printf("%d %d\n", u.x-1, u.y-1);
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=101100;

typedef pair<int,int> pii;

pii a[NN],b[NN];

#define x first
#define y second

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&a[i].x);
		a[i].y=i;
		b[i]=a[i];
	}

	sort(b,b+n);

	cout<<n<<endl;
	for(int i=0;i<n;i++){
		int f=1;
		for(int j=0;j<n;j++){
			if(a[i]==b[j]){
				f=0;
				cout<<i<<" "<<j<<endl;
				swap(a[i],a[j]);
				break;
			}
		}
		if(f)cout<<"0 0"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

int a[3030];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

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

	printf("%d\n", n);
	for(int i=1; i<=n; i++) {
		int mx=*min_element(a+i, a+n+1);
		for(int j=i; j<=n; j++) if(a[j]==mx) {
			printf("%d %d\n", i, j);
			swap(a[i], a[j]);
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define NN 110000
#define MM 110000
#define inf 0x3f3f3f3f
#define x first
#define y second
vector <pair<int, int> > ans;
pair <int, int > p[3300];
int id[3300], pos[3300];
int main(){
#ifndef ONLINE_JUDGE
	freopen("A.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n; scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d", &p[i].x), p[i].y = i;
	sort(p, p + n);
	for(int i = 0; i < n; i++) id[i] = p[i].y, pos[p[i].y] = i;
	for(int i = 0; i < n; i++){
		if(i == id[i]) continue;
		ans.push_back({i, id[i]});
		id[pos[i]] = id[i];
		pos[id[i]] = pos[i];
	}
	printf("%d\n", ans.size());
	for(int i = 0; i < ans.size(); i++){
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
}


#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int a[3033], p[3033];
int id[3033];

void sw(int u, int v) {
	cout << u <<' '<<v<<endl;
	swap(p[id[u]], p[id[v]]);
	swap(id[u], id[v]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r",  stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	for(int i=0; i<n; i++) cin >> a[i], id[i] = i;
	sort(id, id+n, [](int u, int v) {
		return a[u] < a[v];
	});
	for(int i=0; i<n; i++) p[id[i]] = i;
	cout << n << endl;
	for(int i=0; i<n; i++)
	{
		sw(i, p[i]);
	}
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

int a[3030];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

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

	printf("%d\n", n);
	for(int i=1; i<=n; i++) {
		int mx=*min_element(a+i, a+n+1);
		for(int j=i; j<=n; j++) if(a[j]==mx) {
			printf("%d %d\n", i-1, j-1);
			swap(a[i], a[j]);
			break;
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define fi first
#define se second
#define mp make_pair

int main()
{
    int n;
    cin >> n;
    pair<int,int>sk[n];
    for(int i = 0;i<n;i++){
        cin >> sk[i].fi;
        sk[i].se = i;
    }
    sort(sk,sk+n);
    cout << n << "\n";
    for(int i = 0;i<n;i++){
        cout << i << " " << sk[i].se << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define fi first
#define se second
#define mp make_pair

int main()
{
    int n;
    cin >> n;
    pair<int,int>sk[n];
    for(int i = 0;i<n;i++){
        cin >> sk[i].fi;
        sk[i].se = i;
    }
    sort(sk,sk+n);
    cout << n << "\n";
    for(int i = 0;i<n;i++){
        cout << i+1 << " " << sk[i].se+1 << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fi first
#define se second

int main() {

    int n;
    cin >> n;
    int sk[n];
    for(int i = 0;i<n;i++){
        cin >> sk[i];
    }
    int mz,ind;
    for(int i = 0;i<n;i++){
        ind = -1;
        mz = sk[i];
        for(int x = i+1;x<n;x++){
            if(sk[x] < mz){
                mz = sk[x];
                ind = x;
            }
        }
        if(ind != -1){
            cout << ind << " " << i << "\n";
            swap(sk[ind],sk[i]);
        }
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fi first
#define se second

int main() {

    int n;
    cin >> n;
    int sk[n];
    for(int i = 0;i<n;i++){
        cin >> sk[i];
    }
    int mz,ind;
    cout << n << "\n";
    for(int i = 0;i<n;i++){
        ind = i;
        mz = sk[i];
        for(int x = i+1;x<n;x++){
            if(sk[x] < mz){
                mz = sk[x];
                ind = x;
            }
        }
        cout << ind << " " << i << "\n";
        swap(sk[ind],sk[i]);
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,i,j;
	cin >> n;
	ll a[n];
	vector<ll>v;
	vector<pair<ll,ll> >ans;
	for(i=0;i<n;i++)
	{
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(),v.end());
	for(i=0;i<v.size();i++)
	{
		ll w=v[i];
		for(j=0;j<n;j++)
		{
			if(a[j]==w && j!=i && a[i]!=a[j])
			{
				//cout<<a[i]<<" "<<a[j]<<"\n";
				swap(a[i],a[j]);
				ans.push_back({i,j});
				break;
			}
		}
	}
	cout<<ans.size()<<"\n";
	for(i=0;i<ans.size();i++)
		cout<<ans[i].first<<" "<<ans[i].second<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,i,j;
	cin >> n;
	ll a[n];
	vector<ll>v;
	vector<pair<ll,ll> >ans;
	for(i=0;i<n;i++)
	{
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(),v.end());
	for(i=0;i<n;i++)
	{
		for(j=i;j<n;j++)
		{
			if(v[i]==a[j])
			{
				swap(a[i],a[j]);
				ans.push_back({i,j});
				break;
			}
		}
	}
	cout<<ans.size()<<"\n";
	for(i=0;i<ans.size();i++)
		cout<<ans[i].first<<" "<<ans[i].second<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,i,j;
	cin >> n;
	ll a[n];
	vector<pair<ll,ll> >ans,v;
	for(i=0;i<n;i++)
	{
		cin >> a[i];
		v.push_back({a[i],i});
	}
	sort(v.begin(),v.end());
	for(i=0;i<n;i++)
	{
		if(v[i].first==a[i])
			continue;

		swap(a[i],a[v[i].second]);
		if(a[i]!=v[i].first)
			i--;

		ans.push_back({i,v[i].second});
	}
	cout<<ans.size()<<"\n";
	for(i=0;i<ans.size();i++)
		cout<<ans[i].first<<" "<<ans[i].second<<"\n";

}


#include <bits/stdc++.h>
#define int long long
#define ss second
#define ff first
#define N 100007
#define MOD 1000000007
using namespace std;

int n,a[N],i,j,mn;
vector <pair<int,int> > v;

main(){
	ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	for(i=0;i<n;i++){
		mn=i;
		for(j=i+1;j<n;j++)
			if(a[j]<a[mn])mn=j;
		if(mn!=i){
			swap(a[mn],a[i]);
			v.push_back({mn,i});
		}
	}
	cout<<v.size()<<endl;
	for(i=0;i<v.size();i++)
		cout<<v[i].ff<<" "<<v[i].ss<<endl;
}


#include <bits/stdc++.h>
#define int long long
#define ss second
#define ff first
#define N 100007
#define MOD 1000000007
using namespace std;

int n,a[N],i,j,mn;
vector <pair<int,int> > v;

main(){
	ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	for(i=0;i<n;i++){
		mn=i;
		for(j=i+1;j<n;j++)
			if(a[j]<a[mn])mn=j;
		if(mn!=i){
			swap(a[mn],a[i]);
			v.push_back({mn+1,i+1});
		}
	}
	cout<<v.size()<<endl;
	for(i=0;i<v.size();i++)
		cout<<v[i].ff<<" "<<v[i].ss<<endl;
}


#include <bits/stdc++.h>
#define int long long
#define ss second
#define ff first
#define N 100007
#define MOD 1000000007
using namespace std;

int n,a[N],i,j,mn;
vector <pair<int,int> > v;

main(){
	ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	for(i=0;i<n;i++){
		mn=0;
		for(j=0;j<n;j++)
			if(a[j]<a[mn])mn=j;
		if(mn!=i){
			swap(a[mn],a[i]);
			v.push_back({mn+1,i+1});
		}
	}
	cout<<v.size()<<endl;
	for(i=0;i<v.size();i++)
		cout<<v[i].ff<<" "<<v[i].ss<<endl;
}


#include <bits/stdc++.h>
#define int long long
#define ss second
#define ff first
#define N 100007
#define MOD 1000000007
using namespace std;

int n,a[N],i,j,mn;
vector <pair<int,int> > v;

main(){
	ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	for(i=0;i<n;i++){
		mn=0;
		for(j=i;j<n;j++)
			if(a[j]<a[mn])mn=j;
		if(mn!=i){
			swap(a[mn],a[i]);
			v.push_back({mn+1,i+1});
		}
	}
	cout<<v.size()<<endl;
	for(i=0;i<v.size();i++)
		cout<<v[i].ff<<" "<<v[i].ss<<endl;
}


#include <iostream>
#include <queue>
#include <set>
#include <algorithm>
#include <cmath>
#include <climits>
#include <algorithm>
#include <bitset>
#include <stack>
#include <sstream>
#include <vector>
#include <string>
#include <list>
#include <map>
#define DEBUG
#define ull long long
#define pii pair<int, int>
#define loop(xxx,n) for(int xxx = 0; xxx < n; xxx++)

using namespace std;

int zxc;

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	loop(i, n) cin >> a[i];
	vector<pii> res;
	int cur = n - 1;
	for (int k = 0; k < n; k++) {
		int mi = 0;
		loop(i, a.size()) if (a[i] > a[mi])	mi = i;
		swap(a[mi], a[cur]);
		res.push_back(make_pair(mi, cur--));
		a.pop_back();
	}
	cout << res.size() << endl;
	loop(i, res.size()) printf("%d %d\n", res[i].first, res[i].second);
	return 0;
}