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

void solve(){
   ll n,i,j;
   cin >> n;

   map<ll,ll> mp;

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

   ll ans = n*(n-1)/2;

   for(auto k:mp)
   	ans -= ( (k.second-1) * k.second )  / 2;

   cout<< ans;
}

int main(){

	ll  n , t = 1 ,tt = 1;
	// cin >> tt;
	while(t <= tt){

	   solve();
	   t++;
	}
}

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
// #include <atcoder/all>

using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V< V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
#define RevREP(i,n,a) for(ll i=n;i>a;i--)  // (a,n]
#define REP(i,a,n) for(ll i=a;i<n;i++)  // [a,n)
#define rep(i, n) REP(i,0,n)
#define ALL(v) v.begin(),v.end()
#define eb emplace_back
#define pb push_back
#define sz(v) int(v.size())

template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
template< class A, class B > ostream& operator <<(ostream& out, const P<A, B> &p) {
    return out << '(' << p.first << ", " << p.second << ')';
}
template< class A > ostream& operator <<(ostream& out, const V<A> &v) {
    out << '[';
    for (int i=0;i<int(v.size());i++) {
        if (i) out << ", ";
        out << v[i];
    }
    return out << ']';
}

template<class A, class B> istream& operator >>(istream& in, P<A, B> &p) {
    return in >> p.first >> p.second;
}
template<class A> istream& operator >>(istream& in, V<A> &v) {
    for (int i = 0; i < int(v.size()); i++) in >> v[i];
    return in;
}

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vll a(n); cin >> a;
    map<ll, ll> acnt;
    rep(i, n) {
        acnt[a[i]]++;
    }
    ll ans = 0;
    for (PLL ap: acnt) {
        ans += ap.second * (n - ap.second);
    }
    cout << ans / 2 << '\n';
    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define rep3(i, x, y) for(ll i = x; i < y; i++)
#define rep4(i, x) for(ll i = x; i >= 0; i--)
#define all(a) (a).begin(),(a).end()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
//ax + by = cが整数解をもつための必要十分条件は c が gcd(a,b) で割り切れること。
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res += n % 10; n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y, ll m) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= m; }
		x = x * x % m; y /= 2;
	}
	return res;
}
ll kaijyo[600005];
void nCkinit(ll n, ll m) {
	ll cnt = 1; kaijyo[0] = 1;
	for (int h = 1; h <= n; h++) { cnt *= h; cnt %= m; kaijyo[h] = cnt; }
}
ll nCk(ll n, ll k, ll m) {
	/*ll a = 1, b = 1;
	for (int h = 1; h <= n; h++) { a *= h; a %= m; }
	for (int h = 1; h <= k; h++) { b *= h; b %= m; }
	for (int h = 1; h <= n - k; h++) { b *= h; b %= m; }*/
	ll a = kaijyo[n], b = kaijyo[k] * kaijyo[n - k] % m;
	return a * modpow(b, m - 2, m) % m;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <ll, string> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct status {
	ll d;
	ll c;
	ll s;

	bool operator<(const status& rhs) const { return d < rhs.d; };
	bool operator>(const status& rhs) const { return d > rhs.d; };
};

int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	//input
	ll n; string s, t; cin >> n >> s >> t;
	ll zero = 0;
	rep(i, n) { if (s[i] == '0') { zero++; } }
	rep(i, n) { if (t[i] == '0') { zero--; } }
	if (zero != 0) {
		cout << "-1" << endl; return 0;
	}
	ll sone = 0, tone = 0, l = -1, ans = 0;
	rep(i, n) {
		if (s[i] == '1') { sone++; }
		if (t[i] == '1') { tone++; }
		if (sone == tone) {
			if (sone != 0) ans += (i - l) - sone;
			sone = 0; tone = 0; l = i;
		}
	}
	cout << ans << endl;
	return 0;
}

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

int main() {
    int N;
    cin >> N;
    string s, t;
    cin >> s >> t;
    int s0 = 0;
    int t0 = 0;
    int ans = 0;
    for(int i = 0; i < N; i++) {
        if(s[i] == '0' && t[i] == '1') {
            ans += 1;
            s0 += 1;
        } else if(s[i] == '1' && t[i] == '0') {
            t0 += 1;
        } else if(s0 != t0 && s[i] == '0') {
            ans += 1;
        }
    }
    if(s0 != t0) {
        cout << -1 << endl;
        return 0;
    }
    cout << ans << endl;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <cstdio>
#include <math.h>
#include <string>
#include <sstream>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cassert>
#include <iterator>
#include <cstring>
#include <fstream>
#include <cstdio>
#include<ctime>
using namespace std;
typedef long long ll;

const int maxl = 110;

int n, k, mod, lim; ll ans;
ll dp[maxl][maxl * maxl * maxl];

inline void prework()
{
	scanf("%d%d%d", &n, &k, &mod);
	lim = k * n * (n - 1) / 2;
}

inline void add(ll& x, ll y)
{
	x = (x + y) % mod;
	if (x < 0)
		x += mod;
}

inline void mainwork()
{
	dp[0][0] = 1;
	for (int i = 1; i < n; i++)
	{
		for (int j = 0; j <= lim; j++)
		{
			dp[i][j] = dp[i - 1][j];
			if (j >= i)
				add(dp[i][j], dp[i][j - i]);
		}
		for (int j = lim; j >= (k + 1) * i; j--)
			add(dp[i][j], -dp[i][j - (k + 1) * i]);
	}
}

inline void print()
{
	for (int x = 1; x <= n; x++)
	{
		ans = 0;
		for (int i = 0; i <= lim; i++)
			add(ans, dp[x - 1][i] * dp[n - x][i]);
		ans = ans * (k + 1) % mod;
		ans = (ans - 1 + mod) % mod;
		printf("%lld\n", ans);
	}
}

int main()
{
	int t = 1;
	//scanf("%d",&t);
	for (int i = 1; i <= t; i++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define gnr(i,a,b) for(ll i=ll(b)-1;i>=ll(a);i--)
#define per(i,b) gnr(i,0,b)
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define V vector
#define P pair<int,int>
#define Pbb pair<bool,bool>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using ld = long double;
using namespace std;
const int INF =1001001001;
const int MAX = 2e6+1;

int main() {
    ll R,x,y;cin>>R>>x>>y;
    ll z=x*x+y*y;
    R*=R;
    if(z==R){
        cout<<1<<endl;
        return 0;
    }
    ll ans=2;
    while(1){
        if(ans*ans>=z){
            cout<<ans<<endl;
            return 0;
        }
        if(ans*ans*R>=z){
            cout<<ans<<endl;
            return 0;
        }
        ans++;
    }
}


#include <bits/stdc++.h>
using namespace std;
using db = double;
bool win(string s, string t) {
  int num[2][10];
  for (int i = 0; i < 2; i ++) {
    for (int j = 0; j < 10; j ++) {
      num[i][j] = 0;
    }
  }
  for (int i = 0; i < 5; i ++) {
    int kj = (int)s[i] - (int)'0';
    num[0][kj] ++;
  }
  for (int j = 0; j < 5; j ++) {
    int kj = (int)t[j] - (int)'0';
    num[1][kj] ++;
  }
  int X[2];
  for (int i = 0; i < 2; i ++) {
    X[i] = 0;
    for (int j = 0; j < 10; j ++) {
      int pk = j;
      for (int k = 0; k < num[i][j]; k ++) {
        pk *= 10;
      }
      X[i] += pk;
    }
  }
  //cout << s << ' ' << t << ' ' << X[0] << ' ' << X[1] << endl;
  return X[0] > X[1];
}
int main () {
  long long K;
  cin >> K;
  long long numb[10];
  for (int i = 1; i < 10; i ++) {
    numb[i] = K;
  }
  long long k_ = K;
  k_ *= 9;
  long long bo = ((k_ - 8) * (k_ - 9)) / 2;
  //cout << bo << endl;
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < 4; i ++) {
    int kj = (int)s[i] - (int)'0';
    numb[kj] --;
  }
  for (int i = 0; i < 4; i ++) {
    int kj = (int)t[i] - (int)'0';
    numb[kj] --;
  }
  db ans = 0;
  long long ansn = 0;
  string key = "0123456789";
  for (int x = 1; x < 10; x ++) {
    for (int y = 1; y < 10; y ++) {
      s[4] = key[x];
      t[4] = key[y];
      if (win(s, t)) {
        long long si = numb[x] * numb[y];
        if (x == y) {
          si -= numb[x];
        }
        ansn += si;
        //cout << x << ' ' << y << ' ' << ansn << endl;
      }
    }
  }
  //cout << ansn << endl;
  ans = (db)ansn / bo;
  cout << setprecision(20) << ans / 2 << endl;
}


#include <array>
#include <iostream>
using namespace std;
using arr = array<int, 10>;

int func_g1(const arr& a) {
  int t = 0;
  for (int i = 9; i >= 0; i--) {
    for (int jj = 0; jj < a[i]; jj++) {
      t = t * 10 + i;
    }
  }
  return t;
}

int func_g2(const arr& a) {
  int t = 0;
  for (int i = 0; i < 10; i++) {
    for (int jj = 0; jj < a[i]; jj++) {
      t = t * 10 + i;
    }
  }
  return t;
}

int get_length(const int x) {
  if (x < 10) return 1;
  if (x < 100) return 2;
  if (x < 1000) return 3;
  if (x < 10000) return 4;
  if (x < 100000) return 5;
  if (x < 1000000) return 6;
  if (x < 10000000) return 7;
  if (x < 100000000) return 8;
  if (x < 1000000000) return 9;
  return 10;
}

arr decomp(const int x) {
  arr t{};
  int tmp = x;
  for (int i = 0, length = get_length(x); i < length; i++) {
    t[tmp % 10]++;
    tmp /= 10;
  }
  return t;
}

int main() {
  // C
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    auto A = decomp(N);
    auto g1 = func_g1(A);
    auto g2 = func_g2(A);
    N = g1 - g2;
  }
  cout << N << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)

int main(){
  int n,s,d;cin>>n>>s>>d;
  string ans="No";
  REP(i,n){
    int x,y;cin>>x>>y;
    if(s>x&&d<y) ans="Yes";
    //if(d<y) ans="Yes";
  }
  cout<<ans<<endl;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const long long Maxn=2e5+10;
const long long Maxm=Maxn<<2;
const long long mod=998244353;
long long a[Maxn],c[Maxn];
long long tag[Maxm],sum[Maxm];
long long p[Maxn];
long long n,ans;
inline long long read()
{
	long long s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
inline bool cmp(long long x,long long y)
{
	return a[x]<a[y];
}
long long power(long long x,long long y)
{
	long long ret=1ll,base=x;
	while(y)
	{
		if(y & 1)ret=(ret*base)%mod;
		base=(base*base)%mod;
		y>>=1;
	}
	return ret;
}
inline long long inv(long long x)
{
	return power(x,mod-2);
}
inline void push_up(long long k)
{
	sum[k]=(sum[k<<1]+sum[k<<1|1])%mod;
}
inline void upd(long long k,long long v)
{
	tag[k]=(tag[k]*v)%mod;
	sum[k]=(sum[k]*v)%mod;
}
inline void push_down(long long k)
{
	if(tag[k]==1ll)return;
	upd(k<<1,tag[k]);
	upd(k<<1|1,tag[k]);
	tag[k]=1ll;
}
void build(long long k,long long l,long long r)
{
	tag[k]=1ll;
	if(l==r)
	{
		if(l==1)sum[k]=(a[c[l]])%mod;
		else sum[k]=(p[l-2]*a[c[l]])%mod;
		return;
	}
	long long mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	push_up(k);
}
void modify(long long k,long long l,long long r,long long x,long long y,long long v)
{
	if(x<=l && r<=y)return upd(k,v);
	push_down(k);
	long long mid=(l+r)>>1;
	if(x<=mid)modify(k<<1,l,mid,x,y,v);
	if(mid<y)modify(k<<1|1,mid+1,r,x,y,v);
	push_up(k);
}
long long query(long long k,long long l,long long r,long long x,long long y)
{
	if(x<=l && r<=y)return sum[k];
	push_down(k);
	long long mid=(l+r)>>1,ret=0ll;
	if(x<=mid)ret=query(k<<1,l,mid,x,y);
	if(mid<y)ret=(ret+query(k<<1|1,mid+1,r,x,y))%mod;
	return ret;
}
int main()
{
	// freopen("in.txt","r",stdin);
	n=read();
	for(long long i=1;i<=n;++i)
	a[i]=read(),c[i]=i;
	sort(c+1,c+1+n,cmp);
	p[0]=1ll;
	for(long long i=1;i<=n;++i)
	p[i]=(p[i-1]*2ll)%mod;
	build(1,1,n);
	long long tmp=inv(2ll);
	for(long long i=1;i<=n;++i)
	{
		ans=(ans+(query(1,1,n,i,n)*a[c[i]])%mod)%mod;
		if(i+2<=n)modify(1,1,n,i+2,n,tmp);
	}
	printf("%lld\n",ans);
	return 0;
}

#include <iostream>
#include <vector>
#include <utility>
#include<algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#include <tuple>
#include <set>
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main(){
  ll n;
  cin >> n;
  vector<vector<ll>> a(n,vector<ll>(2));
  vector<tuple<ll,ll>> x(n);
  vector<tuple<ll,ll>> y(n);
  rep(i,n){
    cin >> a[i][0] >> a[i][1];
    x[i] = make_tuple(a[i][0],i);
    y[i] = make_tuple(a[i][1],i);
  }
  sort(x.begin(),x.end());
  sort(y.begin(),y.end());
  vector<vector<ll>> c;
  c = {{get<1>(x[0]),get<1>(x[n-1])},{get<1>(x[1]),get<1>(x[n-1])},{get<1>(x[0]),get<1>(x[n-2])}};
  vector<ll> ans;
  rep(i,3){
    vector<ll> d;
    d = c[i];
    ll m = abs(a[d[0]][0]-a[d[1]][0]);
    ll p = abs(a[d[0]][1]-a[d[1]][1]);
    if(m >= p){
      ans.push_back(-m);
    }
  }
  c = {{get<1>(y[0]),get<1>(y[n-1])},{get<1>(y[1]),get<1>(y[n-1])},{get<1>(y[0]),get<1>(y[n-2])}};
  rep(i,3){
    vector<ll> d;
    d = c[i];
    ll m = abs(a[d[0]][0]-a[d[1]][0]);
    ll p = abs(a[d[0]][1]-a[d[1]][1]);
    if(m < p){
      ans.push_back(-p);
    }
  }
  sort(ans.begin(),ans.end());
  cout << -ans[1] << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
ll GetDigit(ll num){ return log10(num)+1; } //numの桁数を求める

int main()
{
    ll n;
    cin >> n;
    ll a,b;
    ll total = 0;
    rep(i,n){
        cin >> a >> b;
        total += (b*(b+1)/2) - (a*(a-1))/2;
    }
    cout << total << endl;
    return 0;
}

#pragma Ofast
//#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
#define int long long
#define float double
using namespace std;
signed main(){
srand(time(NULL));
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int a;
cin>>a;
string b;
cin>>b;
bool fail=0;
int suc=-1;
int three=3;
for(int ii=0;ii<3;ii++){
    fail=0;
for(int i=ii;i<a;i++){
    if(i%3==2-ii){
        //cout<<i<<" "<<b[i]<<" "<<0<<endl;
        if(b[i]!='0'){
            fail=1;
            break;
        }
    }
    else{
        //cout<<i<<" "<<b[i]<<" "<<1<<endl;
        if(b[i]!='1'){

            fail=1;
            break;
        }
    }
}
if(fail==0){
    suc=ii;
    break;
}
}
if(suc==-1){
    cout<<0<<endl;
}
else if(b=="111"){
    cout<<0<<endl;
}
else if(b=="00"||b=="000"||b=="010"||b=="100"||b=="001"){
    cout<<0<<endl;
}
else if(a==1){
    if(b=="1")
        cout<<20000000000<<endl;
    if(b=="0"){
        cout<<10000000000<<endl;
    }
}
else{
    a+=suc;
    //cout<<a<<endl;
    suc=(a+2)/3;
    cout<<10000000000-suc+1<<endl;
}

return 0;
}


#include<iostream>

using namespace std;

int N, X;
string S;

int main() {
    cin >> N >> X >> S;
    int res = X;
    for (int i = 0; i < S.size(); i++) {
        if(S[i] == 'o')
            res++;
        if(S[i] == 'x') {
            res--;
            res = max(0, res);
        }
    }
    cout << res << endl;
}

/*  Jai Shree Ram 🚩🚩🚩 */
#include "bits/stdc++.h"
#define ll long long int
#define oo 1000000000000000000
#define forr(i,n) for(int i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(x) x.begin(),x.end()
#define unique(v) sort(all(v)); v.resize(distance(v.begin(),unique(all(v))))
#define eb emplace_back
#define FF first
#define SS second
#define mem(a,v) memset(a,v,sizeof(a))
#define pb push_back
#define popcount(x) __builtin_popcount(x)

using namespace std;

template<typename T>
ostream &operator<<(ostream &output,const vector<T> &v){
    if(v.empty()) return output;
    for(int i=0;i<v.size()-1;i++) output << v[i] <<" ";
    output << v.back();
    return output;
}
template<typename T>
istream &operator>>(istream &input,vector<T> &v){
    for(auto &i: v) cin >> i;
    return input;
}

template <int MOD=1000000007>
struct Modular {
    int value;
    static const int MOD_value = MOD;

    Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
    Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

    Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
    Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
    Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

    friend Modular mexp(Modular a, long long e) {
        Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
        return res;
    }

    friend Modular inverse(Modular b, long long m = MOD) {
        long long u = 0, v = 1, a = b.value;
        while (a != 0) {
            long long t = m / a;
            m -= t * a; swap(a, m);
            u -= t * v; swap(u, v);
        }
        return Modular(u);
    }

    Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
    friend Modular operator+(Modular a, Modular const b) { return a += b; }
    friend Modular operator-(Modular a, Modular const b) { return a -= b; }
    friend Modular operator-(Modular const a) { return 0 - a; }
    friend Modular operator*(Modular a, Modular const b) { return a *= b; }
    friend Modular operator/(Modular a, Modular const b) { return a /= b; }
    friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
    friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
    friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};
using mint = Modular<>;

vector<mint> fac, inv;
void preFac(int _n){
    fac.resize(_n, 1);
    inv.resize(_n, 1);
    for (int i = 1; i < _n; ++i) fac[i] = i * fac[i-1];
    inv[_n-1] = mint(1, fac[_n-1].value);
    for (int i = _n-2; i >= 0; --i) inv[i] = (i+1) * inv[i+1];
}

mint nCr(int _n, int _r) {
    if (_r < 0 || _r > _n) return 0;
    return fac[_n] * inv[_r] * inv[_n-_r];
}

mint nPr(int _n, int _r) {
    if (_r < 0 || _r > _n) return 0;
    return fac[_n] * inv[_n-_r];
}

void __sol(){
    preFac(2e6+10);
    int n,m,k; cin >> n >> m >> k;
    if(n - m > k) cout << 0;
    else cout << nCr(n + m , m ) - nCr(n + m , m +  k + 1);
    return;
}

int main(){
    fastio;
    int tc=1;  // cin >> tc;
    while(tc--) __sol();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}
signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int n;
    cin >> n;
    vi a(2 * n);
    cin >> a;
    vi sign(2 * n);
    V <ii> ord;
    FOR (i, 2 * n) {
        ord.app(mp(a[i], i));
    }
    sort(all(ord));
    FOR (i, n) {
        sign[ord[i].y] = -1;
    }
    FOR (i, n) {
        sign[ord[i + n].y] = 1;
    }
    string ans;
    vi S;
    trav (e, sign) {
        if (S.empty() || S.back() == e) {
            ans += '(';
            S.app(e);
        }
        else {
            ans += ')';
            S.pop_back();
        }
    }
    assert(S.empty());
    cout << ans << endl;
}

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

map<int64_t, int64_t> memo;

int64_t solve(int64_t X, int64_t Y) {
    if (Y <= X) {
        return X - Y;
    }

    if (Y == X + 1 || Y == 2 * X) {
        return 1;
    }

    if (memo.count(Y) > 0) {
        return memo[Y];
    }

    int64_t ans = abs(X - Y);

    //最後にi回2倍を適用した後, 足す or 引くどちらかの連打で終わる
    for (int64_t i = 1; i < 63; i++) {
        int64_t bit = (1ull << i);
        int64_t mask = bit - 1;

        //最後に足す連打で終わる場合
        int64_t target1 = (Y >> i);
        int64_t curr_ans1 = solve(X, target1) + i + (Y & mask);

        //最後に引く連打で終わる場合
        int64_t target2 = target1 + 1;
        int64_t curr_ans2 = solve(X, target2) + i + bit - (Y & mask);

        ans = min({ ans, curr_ans1, curr_ans2 });
    }

    return memo[Y] = ans;
}

int main() {
    int64_t X, Y;
    cin >> X >> Y;

    if (Y <= X) {
        cout << X - Y << endl;
        return 0;
    }

    cout << solve(X, Y) << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
#define MOD 1000000007
#define foru(i,a,b) for(int i=(a); i<(b); i++)
#define forue(i,a,b) for(int i=(a); i<=(b); i++)
#define forb(i,n) for(int i=n-1; i>=0; i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define pb push_back
#define fst first
#define snd second
#define pt(x) cout << x << "\n"
#define sz(c) ((int)c.size())
#define add(a, b, w) edges[a].pb(make_pair(b, w))
typedef pair<int,int> ii;
typedef long long int ll;

string s;
int l;
int dp[20][200];

int solve(int i, int sum){
    if(!sum) return INF;
    else if(!(sum%3)) return 0;
    if(i>=l) return INF;
    else if(dp[i][sum]) return dp[i][sum];

    int r1 = 1 + solve(i+1,sum-(s[i]-'0'));
    int r2 = solve(i+1,sum);
    return dp[i][sum] = min(r1,r2);
}

int main(){

    //freopen("input.in", "r", stdin);
    //cin.tie(0);
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> s;
    int actualSum = 0;
    l = s.size();
    foru(i,0,l){
        actualSum += s[i] - '0';
    }
    int r = solve(0,actualSum);
    if(r == INF) pt(-1);
    else pt(r);
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll a;
    cin >> a;
    int sum = 0;
    bool mod1 = false, mod2 = false, mod0 = false;
    int d = 0;
    while (a) {
        d++;
        int t = a % 10;
        sum += t;
        if (t % 3 == 1) {
            mod1 = true;
        }
        if (t % 3 == 2) {
            mod2 = true;
        }
        if (t % 3 == 0) {
            mod0 = true;
        }
        a = a / 10;
    }
    if (d < 3 && sum % 3 != 0 && !mod0) {
        cout << -1 << endl;
        return 0;
    }
    if (sum % 3 == 0) {
        cout << 0 << endl;
    } else if (sum % 3 == 1) {
        if (mod1) {
            cout << 1 << endl;
        } else {
            cout << 2 << endl;
        }
    } else {
        if (mod2) {
            cout << 1 << endl;
        } else {
            cout << 2 << endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define READ freopen("out.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout);
#define pb push_back
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define sf(x) scanf("%d",&x)
#define sf2(x,y) scanf("%d %d",&x,&y)
#define sf3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sl2(x,y) scanf("%lld %lld",&x,&y)
#define sl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sd(x) scanf("%lf",&x);
#define pii pair<int,int>
#define pLL pair<long long,long long>
#define pDB pair<double,double>
#define ff first
#define sn second
#define PRINT_CASE  printf("Case %d: ",tc++)
#define PRINT_CASENL  printf("Case %d:\n",tc++)
#define lnd tree[ind<<1]
#define rnd tree[(ind<<1)+1]
#define cnd tree[ind]
#define lndp b,(b+e)>>1,(ind<<1)
#define rndp ((b+e)>>1)+1,e,(ind<<1)+1
#define IN(a,x,y) (a>=x && a<=y)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
/// int other=mask^((1<<n)-1);
typedef long long ll;
typedef long long int lln;
typedef unsigned long long int ull;
//typedef long long lld;
//const double pi=acos(-1.0);
int fx[]={1,-1,0,0}; //direction array
int fy[]={0,0,1,-1};
int dir[4][2]={1,0,-1,0,0,-1,0,1};
//int X[8]={0,0,1,-1,-1,1,1,-1};
int Y[8]={1,-1,0,0,-1,-1,1,1};
int knight[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1};
const long double EPS=1e-16;
//#define INF 10000

ll lcm(ll a,ll b)
{
    return ((a*b)/__gcd(a,b));
}
//struct compare
//{
//  bool operator()(const pLL& l, const pLL& r)
//  {
////      if(l.ff==r.ff)
////      {
////          return l.sn<r.sn;
////      }
//      return l.sn>r.sn;
////        if(l.ff==r.ff)
////        {
////            return l.sn>rnd;
////        }
//  }
//};

ll INF=1e18;
const int mx=201050;
//const int mod=998244353;
const int mod=1e9+7;
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
//////cout<<*X.find_by_order(1)<<endl;
////cout<<X.order_of_key(-5)<<endl;

//4352348930241
//const int mod=11092019;
typedef pair<ll,pair<int,int > > pair3;
struct compare
{
  bool operator()(const pLL& l, const pLL& r)
  {
//      if(l.ff==r.ff)
//      {
//          return l.sn<r.sn;
//      }
      return l.sn>r.sn;
//        if(l.ff==r.ff)
//        {
//            return l.sn>rnd;
//        }
  }
};

int main()
{

//        double start = clock();
//        double tim = (clock() - start)/CLOCKS_PER_SEC;

    int t=1,tc=1;
//    sf(t);
    while(t--)
    {
//        cout<<INF<<endl;
        int n;
        sf(n);
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int x=i*j;
                if(x>n)
                    break;
                int y=n/x;
                ans+=y;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main()
{
    int k;
    cin >> k;
    int a=1,b=1,c=1;
    ll ans = 0;
    while(a*b*c<=k){
        while(a*b*c<=k){
            while(a*b*c<=k){
                if(a!=b&&b!=c){
                    ans += 6;
                }else if(a==b&&b==c){
                    ans += 1;
                }else{
                    ans += 3;
                }
                c++;
            }
            c = ++b;
        }
        b=++a;
        c=b;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define N 214514
#define all(x) (x).begin(), (x).end()
const ll mod = 1000000007;
const ll inf = 1000000000000000000;

int main() {
    vector<ll> cnt(10);
    ll k;
    cin >> k;
    for (int i = 1; i <= 9; i++) cnt[i] = k;
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < 4; i++) {
        cnt[s[i] - '0']--;
        cnt[t[i] - '0']--;
    }
    long double sum = (9 * k - 8) * (9 * k - 9), num = 0.0;
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            if ((i == j && cnt[i] < 2) ||
                (i != j && (cnt[i] < 1 || cnt[j] < 1)))
                continue;
            ll score_a = 0, score_t = 0;
            vector<ll> ca(10), ct(10);
            for (int i = 0; i < 4; i++) {
                ca[t[i] - '0']++;
                ct[s[i] - '0']++;
            }
            ca[i]++;
            ct[j]++;
            for (int i = 1; i <= 9; i++) {
                score_a += pow(10, ca[i]) * i;
                score_t += pow(10, ct[i]) * i;
            }
            if (score_t > score_a) {
                if (i == j)
                    num += (long double)cnt[i] * (cnt[i] - 1.0);
                else
                    num += (long double)cnt[i] * cnt[j];
            }
        }
    }
    cout << fixed << setprecision(15) << num / sum << endl;
    return 0;
}

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}

struct mint {
    long long x;
    mint(long long x=0):x((x%MOD+MOD)%MOD){}
    long long val(){
        return x;
    }
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
    	return *this;
    }
    mint& operator*=(const mint a) {
    	(x *= a.x) %= MOD;
    	return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
    	if (t&1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
    	return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

struct combination {
    vector<mint> fact, ifact;
    combination(long long n):fact(n+1),ifact(n+1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(long long n, long long k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
};

void Main(){
    int N,M; cin >> N >> M;
    int S = 0;
    VL A(N); REP(i,N) cin >> A[i], S+=A[i];
    if(M<S){
        cout << 0 << en;
        return;
    }

    mint ans(1);
    REP(i,N+S){
        ans *= M+N-i;
    }
    REP(i,N+S){
        ans /= i+1;
    }
    cout << ans.val() << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//begin of def
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define endl '\n'

using lli = long long int;
using ulli = unsigned long long int;
using Ld = long double;
using pii = pair<int, int>;
using pll = pair<lli, lli>;
using pld = pair<Ld, Ld>;
#define X first
#define Y second

#define rep(I, S, E) for(int I = (S); I < (E); I++)
#define repq(I, S, E) for(int I = (S); I <= (E); I++)

#define pb push_back
#define epb emplace_back

#define ALL(X) X.begin(), X.end()
//end of def

vector<lli> v;

int main(){
    fastio;
    vector<lli> p;
    lli test = 1;
    repq(i, 2, 50){
        bool f = true;
        rep(j, 2, i){
            if(i % j == 0){
                f = false;
                break;
            }
        }
        if(f){
            p.pb(i);
            test *= i;
        }
    }
    v.resize(p.size());

    int n;
    cin >> n;
    rep(i, 0, n){
        int k;
        cin >> k;
        rep(j, 0, p.size()){
            if(k % p[j] == 0)
                v[j] |= (1LL << i);
        }
    }
    lli mn = 4e18;
    rep(t, 1, 1LL << p.size()){
        lli x = 0, mul = 1;
        rep(j, 0, p.size()){
            if(t & (1LL << j)){
                x |= v[j];
                mul *= p[j];
            }
        }
        if(x == (1LL << n) - 1){
            mn = min(mn, mul);
        }
    }
    cout << mn;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
const lint MOD =1000000007;
//const lint MOD=998244353;
const lint MAX = 4000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#pragma GCC optimize("Ofast")
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)

lint fac[MAX], finv[MAX], inv[MAX];

void COMinit()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (lint i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long COM(lint n, lint k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0;

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return 0;
        }
    }

    return 1;
}
   long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
    lint lcm(lint a,lint b){
        return a/__gcd(a,b)*b;
    }
     lint gcd(lint a,lint b){
        return __gcd(a,b);
    }

    int main(){
        lint n,d,h;
        cin>>n>>d>>h;
        P p[n];
     double ans=0;
        rep(i,n){
            lint a,b;
            cin>>a>>b;
            long double kata=(double)(h-b)/(d-a);
            ans=max(ans, double(h-kata*d));

        }

    printf("%.12f\n",ans);

        }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    cout<<(1<<n)-1<<endl;
    for(int i=1;i<(1<<n);i++)
    {
        for(int j=0;j<(1<<n);j++)
            putchar('A'+(__builtin_popcount(i&j)&1));
        puts("");
    }
    return 0;
}

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

vector<string> ret[8];

int main(){
	ret[0].push_back("AB");
	for(int i=1;i<8;i++){
		for(string s: ret[i-1]){
			string t="";
			for(int j=0;j<s.size();j++){
				t+=s[j]; t+=s[j];
			}
			ret[i].push_back(t);
		}
		for(int j=0;j<(1<<i);j++){
			string t="";
			if(j==0){
				for(int k=0;k<(1<<i);k++){
					t+="AB";
				}
			}
			else {
				for(int k=0;k<(1<<i);k++){
					if(ret[i-1][j-1][k]=='A'){
						t+="AB";
					}
					else t+="BA";
				}
			}
			ret[i].push_back(t);
		}
	}
	int n;
	scanf("%d",&n); n--;
	printf("%d\n",(int)ret[n].size());
	for(string s: ret[n]){
		cout<<s<<"\n";
	}
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct UnionFind {
  vector<ll> par, s;
  UnionFind(ll N) : par(N), s(N) { rep(i,N) { par[i] = i; s[i] = 1; } }
  ll root(ll x) { return par[x]==x ? x : par[x] = root(par[x]); }
  ll size(ll x) { return par[x]==x ? s[x] : s[x] = size(root(x)); }
  void unite(ll x, ll y) { ll rx=root(x), ry=root(y); if (rx!=ry) { s[rx] += s[ry]; par[ry] = rx; } }
  bool same(ll x, ll y) {  ll rx=root(x), ry=root(y); return rx==ry; }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll H, W; cin >> H >> W;
  string S[H]; rep(i,H) cin >> S[i];

  UnionFind uf(H+W);
  rep(i,H) rep(j,W) {
    if ((i==0||i==H-1)&&(j==0||j==W-1)) uf.unite(i,j+H);
    if (S[i][j]=='#') uf.unite(i,j+H);
  }
  set<ll> sh, sw;
  rep(i,H) sh.insert(uf.root(i));
  rep(i,W) sw.insert(uf.root(i+H));

  // for (auto x: sh) cout << x << " "; cout << endl;
  // for (auto x: sw) cout << x << " "; cout << endl;
  ll result = min(sh.size(),sw.size()) - 1;
  cout << result << endl;
  return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <iomanip>
#include <utility>
typedef long long ll;
typedef long double ld;
using namespace std;

int A[100010], B[100010];

int main() {
	int N, L;
	cin >> N >> L;
	A[0]=B[0]=0, A[N+1]=B[N+1]=L+1;
	for(int i=1; i<=N; ++i) cin >> A[i];
	for(int i=1; i<=N; ++i) cin >> B[i];
	vector<int> v;
	for(int i=0; i<=N+1; ++i){
		if(A[i]==B[i]) v.emplace_back(i);
	}
	ll ans=0;
	for(int t=0; t+1<v.size(); ++t){
		int i=v[t]+1;
		while(A[i]>B[i]) ++i;
		int j=i, k=i+1;
		while(j<v[t+1]){
			while(k<=v[t+1]){
				if(A[k]-B[j]==k-j) break;
				++k;
			}
			if(k>v[t+1]){
				cout << -1 << endl;
				return 0;
			}
			ans += k-j;
			int m=j+1;
			while(m<v[t+1]){
				if(B[m]-B[j]>m-j) break;
				++m;
			}
			j=m;
		}
		j=i-1, k=i-2;
		while(j>v[t]){
			while(k>=v[t]){
				if(B[j]-A[k]==j-k) break;
				--k;
			}
			if(k<v[t]){
				cout << -1 << endl;
				return 0;
			}
			ans += j-k;
			int m=j-1;
			while(m>v[t]){
				if(B[j]-B[m]>j-m) break;
				--m;
			}
			j=m;
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n){
        cin >>a[i];
    }
    ll ans;
    ans = n*(n-1)/2;
    sort(a.begin(),a.end());
    ll cnt=0;
    rep(i,n){
        if(i == 0){
            continue;
        }
        if(a[i-1]==a[i]){
            cnt++;
        ans = ans - cnt;
        }
        else{
            cnt =0;
        }
    }
    cout << ans << endl;

}

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

int main() {
    long long L;
    cin >> L;

    long long res = 1;
    for(int i = 1; i <= 11; i++) {
        res *= L-i;
        res /= i;
    }
    cout << res << endl;
}


#include <iostream>
#include <algorithm>

#define rep(i,N) for(int i=0;i<N;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
#define PInt std::pair<int,int>

int main(void){
  int H,W,N,M;
  cin >> H >> W >> N >> M;
  enum kind{
    EMPTY,
    BLOCK,
    LIGHT,
    MAX
  };

  int h,w;
  kind stage[1500][1500];
  rep(i,H)rep(j,W)stage[i][j]=EMPTY;
  rep(i,N){
    cin >> h >> w;
    stage[h-1][w-1]=LIGHT;
  }
  rep(i,M){
    cin >> h >> w;
    stage[h-1][w-1]=BLOCK;
  }

  int res =0;
  int dh[]={-1,0,1,0},dw[]={0,-1,0,1};
  rep(i,H)rep(j,W){
    if(stage[i][j]==EMPTY){

      int flag = 0;
      rep(d,4){
        h=i,w=j;
        while(0<=h && h <H && 0<=w && w < W && stage[h][w]==EMPTY){
          h+=dh[d];
          w+=dw[d];
        }
        if(!(0<=h && h <H && 0<=w && w < W) || stage[h][w]==BLOCK){
          //届いてないので次
          flag++;
        }else{
          break;
        }
      }
      if(flag == 4){
        res++;
      }
    }
  }

  cout << ((ll)H*(ll)W-(ll)(M+res)) << endl;

  return 0;
}

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

// #include <atcoder/all>
// using namespace atcoder;

#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;

void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B){
    vector<vector<int>> e(N);
    rep(i, M) {
        A[i]--;
        B[i]--;
        e[A[i]].push_back(B[i]);
    }
    ll ans = 0;
    rep(i, N) {
        set<int> seen;
        queue<int> q;
        seen.insert(i);
        q.push(i);
        while (!q.empty()) {
            int v = q.front(); q.pop();
            for (int u: e[v]) {
                if (seen.count(u)) continue;
                seen.insert(u);
                q.push(u);
            }
        }
        ans += seen.size();
    }
    cout << ans << endl;
}

// Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long N;
    std::scanf("%lld", &N);
    long long M;
    std::scanf("%lld", &M);
    std::vector<long long> A(M);
    std::vector<long long> B(M);
    for(int i = 0 ; i < M ; i++){
        std::scanf("%lld", &A[i]);
        std::scanf("%lld", &B[i]);
    }
    solve(N, M, std::move(A), std::move(B));
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>

const ll mod = 1000000007;
const ll INF = 1001001001001001001;

string x;
ll m;
vector<ll> s;

bool f(ll b){
    vector<ll> y;
    ll a = m;
    while(a > 0){
        y.push_back(a%b);
        a /= b;
    }
    reverse(y.begin(), y.end());

    // cout << b << endl;
    // rep(i, s.size()){
    //     cout << s[i] << " ";
    // }cout << endl;
    // rep(i, y.size()){
    //     cout << y[i] << " ";
    // }cout << endl;
    // cout << endl;

    if(y.size() > s.size()) return true;
    else if(y.size() == s.size()){
        rep(i, y.size()){
            if(y[i] > s[i]) return true;
            else if(y[i] < s[i]) return false;
        }
        return true;
    }else return false;
}

int main(){
    cin >> x >> m;
    int k = 0;
    int mx = 1;
    while(x[k] != 0) {
        int c = x[k]-'0';
        mx = max(mx, c);
        k++;
        s.push_back(c);
    }

    if(k == 1){
        int c = x[0]-'0';
        if(c > m) cout << 0 << endl;
        else cout << 1 << endl;
        return 0;
    }
    // rep(i, s.size()){
    //     cout << s[i];
    // }cout << endl;

    ll l = 1, r = INF;
    while(l < r - 1){
        ll mid = (l+r) / 2;
        if(f(mid)){
            l = mid;
        }else{
            r = mid;
        }
    }
    l = min(l, (ll)1000000000000000000);
    // cout << l << " " << mx << endl;
    cout << max((ll)0, l - mx) << endl;
}

//#define local
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i,n); for(long long i = 0;i < (n);i++)
using ll = long long;
using P = pair<long long,long long>;
template <class T> using vec = vector<T>;
#ifdef local
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout << fixed << setprecision(10);
    ll n; cin >> n;
    string s; cin >> s;
   for(ll i=2;i<s.size();i++){
       if(s[i-2]=='f'&&s[i-1]=='o'&&s[i]=='x'){
           s.erase(i-2,3);
           i-=3;
       }
   }
   cout << s.size() << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back
#define mp make_pair
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 2e5+10;

const string YES = "Takahashi\n", NO = "Aoki\n";

int N;
string s, x;
int dp[MAX_N][7];

int go(int at, int rem)
{
	if(at == N){
		return (rem == 0)?1:0;
	}
	int& res = dp[at][rem];
	if(res != -1) return res;
	if(x[at] == 'T'){
		if(go(at+1, (10*rem)%7)) return res=1;
		if(go(at+1,(10*rem+(s[at]-'0'))%7)) return res=1;
		return res=0;
	}else{
		if(!go(at+1, (10*rem)%7)) return res=0;
		if(!go(at+1,(10*rem+(s[at]-'0'))%7)) return res=0;
		return res=1;
	}
}

string solve()
{
	memset(dp,-1,sizeof(dp));
	int res = go(0, 0);
	if(res) return YES;
	return NO;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> N;
	cin >> s >> x;
	cout<< solve();
	return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("-O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
#ifdef ON_LINUX
#include <sys/resource.h>
#define meminc             rlimit rlim;if (getrlimit(RLIMIT_STACK, &rlim)) return 1;rlim.rlim_cur = 26843556;if (setrlimit(RLIMIT_STACK, &rlim)) return 2;
#else
#define meminc
#endif
#ifdef LOCAL
#define dbg(args...)       { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#else
#define dbg(args...)
#endif
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
template<typename T,typename U> std::ostream& operator<<(std::ostream& out, std::pair<T,U> a) {
    out<< a.first << " " << a.second;
    return out;
}

template<typename T,typename U> std::istream& operator>>(std::istream& in, std::pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}
#define int                long long int
#define ll                 int
#define x                  first
#define y                  second
#define pii                pair<int, int>
#define vii                vector<pii>
#define vi                 vector<int >
#define ordered_set        tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2,  1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------

#define rep(i, a, b)       for(int  i=a;i<b;i++)
#define all(a)             (a).begin(),(a).end()
#define sz(x)              (int )x.size()
#define yes                cout << "YES" << endl
#define no                 cout << "NO" << endl
#define endl               '\n'
// const int hell = (int)998244353;
const int hell = (int)1e9 + 7;
const int inf = 3223372036854775807;
const double PI = 3.14159265;
const int N = (int) 1e6 + 5;
int n, m, k, a[N];

int32_t main()
{
  //  meminc;
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cerr.precision(10);
    cout.precision(25);
    cout << fixed;
    #ifdef ON_LINUX
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
    #endif
    int tests = 1;
    // cin >> tests;
    rep(test, 1, tests+1)
    {
        cin >> n;
        rep(i, 0, n)cin >> a[i];
        int gcd = a[0];
        rep(i, 1, n)gcd = __gcd(a[i], gcd);
        cout << gcd;
    }
    #ifdef LOCAL
        // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <iostream>
using namespace std;

long long power(long long x, long long k, long long mod){
  long long ret = 1;
  while(k > 0){
    if(k&1) ret *= x, ret %= mod;
    x *= x, x %= mod;
    k >>= 1;
  }
  return ret;
}

int main(){
  long long N, M;
  cin >> N  >> M;
  long long ans = power(10,N,M*M);
  ans /= M;
  cout << ans << endl;
}

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
#define G getchar()
int read()
{
	int x=0; bool flg=false; char ch=G;
	for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
	for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
	return flg?-x:x;
}
#undef G
#define fi first
#define se second
typedef long long ll;
const int mod=998244353;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,int y){x=upd(x+y-mod);}
inline void iadd(int &x,int y){x=upd(x-y);}
int qpow(int x,int y){
	int res=1;
	for (;y;y>>=1,x=1LL*x*x%mod)
		if (y&1) res=1LL*res*x%mod;
	return res;
}
#define rep(i,l,r) for (int i(l);i<=r;i++)
#define per(i,l,r) for (int i(r);i>=l;i--)

ll n; int m;
struct C{
	int x,y,z;
};
C f[2][10010]; bool e=0;
void trans(){
	e^=1;
//	rep(i,0,m-1) f[e][i]=(C){0,0,0};
	rep(i,0,m-1){
		C &g=f[e^1][i],&h=f[e^1][g.z],&o=f[e][i];
		o.x=g.x*h.x%m,o.y=(g.y*h.x+h.y)%m,o.z=h.z;
	}
}
void solve(){
	scanf("%lld%d",&n,&m);
	rep(i,0,m-1){
		f[e][i]=(C){10%m,(i*10)/m,i*10%m};
	}
	int a=0,b=1;
	for (ll k=n;k;k>>=1,trans())
		if (k&1){
			C &t=f[e][b];
			a=(a*t.x+t.y)%m,b=t.z;
		}
	printf("%d\n",a);
}
int main()
{
	for (int T=1;T--;) solve();
}


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

const int maxn=100005;

int n,l;
int a[2][maxn];
int v[maxn],li[maxn],ri[maxn],lj[maxn],rj[maxn],t;

int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>n>>l;
    for(int t=0;t<2;t++){
        for(int i=1;i<=n;i++)cin>>a[t][i],a[t][i]-=i;
        a[t][0]=0;a[t][n+1]=l-n;
    }
    for(int i=0;i<=n+1;i++){
        if(i==0||a[0][i]!=a[0][i-1]){
            ++t;v[t]=a[0][i];li[t]=ri[t]=i;
            lj[t]=-1;
        }else ri[t]=i;
    }
    int sp=0;
    for(int i=0;i<=n+1;i++){
        while(sp<=0||a[1][i]>v[sp])++sp;
        if(a[1][i]!=v[sp]){
            cout<< -1;
            return 0;
        }
        if(lj[sp]==-1)lj[sp]=i;
        rj[sp]=i;
    }
    long long ans=0;
    for(int i=1;i<=t;i++){
        if(lj[i]!=-1)ans+=max(li[i]-lj[i],0)+max(rj[i]-ri[i],0);
    }
    cout<<ans;
    return 0;
}


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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int, int> > vii;
typedef   vector<long long> vl;
typedef   pair<int, int> pii;
typedef   pair<ll, ll> pll;
typedef   vector< pair<ll, ll> > vll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};

void omae_wa_mou_shindeiru(int tc) {
	ll n;
	cin >> n;
	vl p(n + 1), f(n), ans;
	fi(1, n + 1) {
		ll temp;
		cin >> temp;
		p[i] = temp;
	}
	vl temp = p;
	sort(temp.begin(), temp.end());
	set<int>odd, even;
	fi(1, n) {
		if (p[i] > p[i + 1]) {
			if (i % 2)
				odd.insert(i);
			else
				even.insert(i);
		}
	}

	fi(1, n * n + 1) {
		if (p == temp) {
			break;
		}
		if (i % 2) {
			int kk;
			if (odd.size() == 0) {
				kk = n - 1 - (n % 2);
			}
			else {
				kk = *odd.begin();
				odd.erase(odd.begin());
			}
			swap(p[kk], p[kk + 1]);
			ans.push_back(kk);
			if (even.find(kk + 1) != even.end()) {
				even.erase(kk + 1);
			}
			if (even.find(kk - 1) != even.end()) {
				even.erase(kk - 1);
			}
			if (kk + 2 <= n) {
				if (p[kk + 1] > p[kk + 2]) {
					even.insert(kk + 1);
				}
			}
			if (kk - 1 >= 1) {
				if (p[kk - 1] > p[kk]) even.insert(kk - 1);
			}
		}
		else {
			int kk;
			if (even.size() == 0) {
				kk = n - 1 - (n + 1) % 2;
			}
			else {
				kk = *even.begin();
				even.erase(even.begin());
			}
			swap(p[kk], p[kk + 1]);
			ans.push_back(kk);
			if (odd.find(kk + 1) != odd.end()) {
				odd.erase(kk + 1);
			}
			if (odd.find(kk - 1) != odd.end()) {
				odd.erase(kk - 1);
			}
			if (kk + 2 <= n) {
				if (p[kk + 1] > p[kk + 2]) odd.insert(kk + 1);
			}
			if (kk - 1 >= 1) {
				if (p[kk - 1] > p[kk]) odd.insert(kk - 1);
			}
		}
		// pr(p);
	}
	cout << ans.size() << endl;
	pr(ans);

}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tc = 1;
	cin >> tc;
	fi(1, tc + 1) {
		omae_wa_mou_shindeiru(i);
	}
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 998244353

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

ll binpow(ll x,ll y)/* (x^y)%p in O(log y) */{ll res=1;while (y > 0){if(y&1)res=(res*x);y = y>>1;x=(x*x);}return res;}
ll binpowmod(ll x,ll y,ll p)/* (x^y)%p in O(log y) */{ll res=1;x=x%p;while (y > 0){if(y&1)res=(res*x)%p;y = y>>1;x=(x*x)%p;}return res;}
ll mod_inverse(ll n,ll p)/* Returns n^(-1) mod p */{return binpowmod(n,p-2,p);}
ll gcd(ll x,ll y)
{
    if(y==0)
        return x;
    return gcd(y,x%y);
}

bool isPowerOfTwo (ll x)
{
    /* First x in the below expression is for the case when x is 0 */
    return x && (!(x&(x-1)));
}

void swap(int &x,int &y){
    int temp = x;
    x = y;
    y = temp;
}

unsigned int onesComplement(unsigned int n)
{
   // Find number of bits in the given integer
   int number_of_bits = floor(log2(n))+1;

   // XOR the given integer with poe(2,
   // number_of_bits-1 and print the result
   return ((1 << number_of_bits) - 1) ^ n;
}

bool comp1(pair<int,int> x, pair<int,int> y)
{
    return x.second < y.second;
}

// class cmp      //comrootator for priority_queue
// {               //declaration: priority_queue<int,vector<int>,cmp>
// public:
//     bool operator()(E& A,E& B)
//     {
//         // if(A.first == B.first)
//         //     return A.second < B.second;
//         return A.wt > B.wt;
//     }
// };

void solve(){
    int n;
    cin >> n;
    vector<int> p(n + 1);
    for(int i = 1; i <= n; ++i)
        cin >> p[i];
    int par = 1;
    vector<int> moves;
    while(true) {
        bool f = true;
        bool f2 = false;
        for(int i = 1; i <= n - 1; ++i) {
            if(p[i] > p[i + 1]) {
                f = false;
                if(i % 2 == par) {
                    f2 = true;
                    swap(p[i], p[i + 1]);
                    moves.push_back(i);
                    par ^= 1;
                    break;
                }
            }
        }

        if(f)
            break;
        if(f2)
            continue;
        for(int i = n - 1; i >= 1; --i) {
            if(p[i]  < p[i + 1] and i % 2 == par) {
                moves.push_back(i);
                swap(p[i], p[i + 1]);
                par ^= 1;
                break;
            }
        }

    }

    cout << moves.size() << '\n';
    for(int x: moves)
        cout << x << ' ';
    cout << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << fixed << setprecision(15);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    // solve();

}


#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

//const int MAX=INT_MAX;

//const ll MAX=100000000000;

//const int mod = 1000000007;
//const int mod = 998244353;

//const ll A=100000000000;

const int INF=1000000000;

ll extgcd(ll a,ll b,ll& x,ll& y){
    if(b==0){
        x=1;
        y=0;
        return a;
    }else{
        ll d =extgcd(b,a%b,x,y);
        ll prv=x;
        x=y;
        y=prv-a/b*y;
        return d;
    }

}

int main(){

    int T;
    cin>>T;

    ll N,S,K;

    for(int t=0;t<T;t++){
        cin>>N>>S>>K;

        ll x=0;
        ll y=0;
        ll g=extgcd(N,-K,x,y);
        if(S%g!=0){
            cout<<-1<<endl;
        }else{
            ll s=S/g;
            ll n=N/g;
            ll k=K/g;
            ll nx=x*s;
            ll ny=y*s;

           // cout<<"ny:"<<ny<<"n:"<<n<<endl;

            ll t;
            if(n>0){
            if(-double(ny)/double(n)<0){
                t=-ny/n;
            }else{
                t=-ny/n+1;
            }
            }else{
                if(-double(ny)/double(n)<0){
                    t=-ny/n-1;
                }else{
                    t=-ny/n;
                }

            }

           // cout<<t<<endl;

           ll ans=n*t+ny;

           // cout<<g<<":"<<s<<":"<<x<<":"<<y<<":"<<ans<<endl;
            cout<<ans<<endl;

        }
    }
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
	x = y1 - (b / a) * x1;
	y = x1;
	return d;
}

void solve()
{
	ll n,s,k,gg;
	cin>>n>>s>>k;
	gg=__gcd(n,k);
	if(s%gg!=0)
	{
		ol("-1\n");
		return;
	}
	n/=gg,s/=gg,k/=gg;

	ll x,y;
	gcd(k,n,x,y);

	x=(x+n)%n;

	s=(n-s)%n;
	s=(s*x)%n;

	s=(s+n)%n;

	ol(s);on;
}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
	solve();
}

return 0;
}


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

int main() {
  int N;
  vector<int> V;
  vector<int> P;
  long X;
  long drink = 0;
  int ans;
  cin >> N >> X;
  int a, b;
  for(int i = 0; i < N; ++i){
    cin >> a >> b;
    V.push_back(a);
    P.push_back(b);
  }
  for(ans = 0; ans < N; ++ans){
    drink += V.at(ans) * P.at(ans);
    if(drink > X * 100){
      cout << ans + 1 << endl;
      return 0;
    }
  }

      cout << -1 << endl;
 }

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0;i<n;++i)
#define rep1(i, n) for(int i=1;i<=n;++i)
#define ll long long
int main(){
    int n;
    ll x, v, p;
    cin>>n>>x;
    x *= 100;
    vector<ll>a;
    rep(i, n){
        cin>>v>>p;
        a.emplace_back(v * p);
    }

    int ans = -1;
    ll sum = 0;

    rep(i, n){
        sum += a[i];
        if(sum > x){
            ans = i+1;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i,n) for(ll i=0;i<n;i++)
#define revf(i,n) for(ll i=n-1;i>=0;i--)
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define rep1(i,b) for(ll i=1;i<=b;i++)
#define inp(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define outp(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define outp1(a,n) for(ll i=1;i<n;i++)cout<<a[i]<<" "
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (((a)*(b))/gcd((a),(b)))
#define pb push_back
#define mp make_pair
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x,n) memset(x,n,sizeof(x))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define test ll t; cin>>t; while(t--)
#define cn cout<<"\n";
int main()
{
     fast
     ll n,x;
     cin>>n>>x;
     ll v[n],p[n];
     forr(i,n)
     cin>>v[i]>>p[i];
     ll s=0,s1=-1;
     forr(i,n)
     {
          s+=v[i]*p[i];
          if(s>x*100)
          {
               s1=i;break;
          }
     }
     if(s1!=-1)
     cout<<s1+1;
     else
     cout<<-1;
}

/*
 *    author:    Apiram
 *    created:   23.01.2021 17:34:22
*/

#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t n;
int64_t k;
cin>>n>>k;
k*=100;
vector<int64_t>arr(n);
for (auto i =0;i<n;++i){
	int64_t a,b;
	cin>>a>>b;
	arr[i]=a*b;

}
int64_t i=0;
while(k>=0&&i<n){
	k-=arr[i];
	++i;
}
if (k<0)
cout<<i;
else cout<<-1;
return 0;}


#include <iostream>
#include<string>
#include <vector>
#include <iomanip>
#include <stdio.h>
#include <math.h>

using namespace std;
int main()
{
    int n;
    string s;
    cin >> s;
    int result = 0;
    for (int i = 0; i < 9; i++)
    {
        if(s[i] == 'Z'){
            if(s[i+1] == 'O'){
                if(s[i+2] == 'N'){
                    if(s[i+3] == 'e'){
                        result++;
                    }
                }
            }
        }
    }

    cout << result << endl;
    return 0;
}

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

#define rep(i,a,b) for(int i=a;i<b;i++)
#define ll long long int
#define test() int tt; cin>>tt; while(tt--)
#define fst_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define lg 1000000000
#define vec vector<int>
void arrInput(int a[],int n){rep(i,0,n) cin>>a[i];}

void solve()
{
  string s;
  int c=0;
  cin>>s;;
  for(int i=0; i<=s.size()-4; i++)
  {
    if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e')
    	c++;
  }
  cout<<c;
}

int main()
{
  solve();
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vii = vector<vector<int>>;
using vll = vector<vector<ll>>;
using vcc = vector<vector<char>>;
using vs = vector<string>;
using Pi = pair<int , int>;
using Pl = pair<ll, ll>;
using vpi = vector<Pi>;
using vpl = vector<Pl>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(n) for (ll i = 0; i < n; i++)
#define repint(i, n) for (int i = 0; i < n; i++)
#define rep2(i, s, n) for (int i = (s); i < n; i++)
#define REP2(s, n) for (int i = (s); i < n; i++)
#define rep3(i ,j, n, m) rep(i, n)rep(j, m)
#define REP3(n, m) rep(i, n)rep(j, m)
#define sort(A) sort(A.begin(),A.end());
#define reverse(A) reverse(A.begin(),A.end());
#define k(s) cout << fixed << setprecision(s);
const long double pi=3.14159265358979323846;
const long long MOD=1000000007;

int main() {
    int n, m;
    cin >> n >> m;

    int t[2] = {0, 0};
    while(n--) {
        string s;
        cin >> s;
        int cc = 0;
        for(char&c : s) {
            cc ^= (c - '0');
        }
        t[cc]++;
    }

    cout << 1LL * t[0] * t[1];
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using ll = long long;
using namespace std;

int main()
{
    int n;
    cin >> n;
    ll ans = 0;
    rep(i, n)
    {
        ll a, b; cin >> a >> b;
        ll s1 = b * (b + 1) / 2;
        ll s2 = a * (a + 1) / 2;
        ans += s1 - s2 + a;
    }

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


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,k,m,sum(0),avg,ans(-1);
  cin >> n >> k >> m;
  for(int i = 0;i<n-1;i++){
    cin >> avg;
    sum += avg;
  }
  avg = sum/n;
  if(m <= avg){
    ans = 0;
  }else if((m*n-sum) <= k){
    ans = m*n - sum;
  }
  cout << ans << endl;
}

#include <stdio.h>
#include<iostream>
#include <string>
#include <string.h>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <climits>
#include <set>
#include <unordered_map>
#include <map>
#include <stack>
#include <unordered_set>
#define hash hassh
using namespace std;
int main() {
    int T=1;
    //cin>>T;
    while(T--) {
        int a,b,k;
        cin>>a>>b>>k;
        k*=1000;
        if(k<a){
            puts("UNSATISFIABLE");
            continue;
        }
        int t=k/a;
        if(t*b<k){
            puts("UNSATISFIABLE");
            continue;
        }
        swap(a,b);
        printf("%d %d\n",k/a+(k%a>0),k/b);
    }
    return 0;
}
//注意检查边界
//注意考虑某些数组/向量可能为空 可能引起错误
//注意各种边界是0还是1
//注意少加不必要的限制条件 避免出错
//不要把前期题想得太难，尝试找简单结论


#include<bits/stdc++.h>
using namespace std;
typedef  long long   ll;
ll inf=1e18,mod=1e9+7;
ll fact[1000000],ifact[1000000];
ll power (ll a,ll b){

    ll res=1;
    while(b){
        if(b&1)
        res=(res*a)%mod;
        b/=2;
        a=(a*a)%mod;
    }
    return res;

}
ll ncr(ll n,ll r){

    if(n<=r)
    return 1;
    ll res=1;
    res=fact[n];
    res=(res*ifact[n-r])%mod;
    res=(res*ifact[r])%mod;

    return res;
}
int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        #ifndef ONLINE_JUDGE
            freopen("input.txt","r",stdin);
            freopen("output.txt","w",stdout);
        #endif

        ll n;cin>>n;
        ll arr[3][n];
        for(ll i=0;i<n;i++)
        cin>>arr[0][i]>>arr[1][i]>>arr[2][i];

        ll mi=inf;
        for(ll i=0;i<n;i++){
            if(arr[2][i]-arr[0][i]>0)
            mi=min(mi,arr[1][i]);
        }
        if(mi==inf)
        cout<<-1<<endl;
        else
        cout<<mi;

}

#include <iostream>
using namespace std;
int main() {
int n,y,z,w,ab;
cin>>n;
cin>>y>>z>>w;
if(w>y) ab=z;
else ab=0;
for(int i=1;i<n;i++){
    int A,P,X;
    cin>>A>>P>>X;
    if(X>A){
        if(ab>P) ab=P;
        if(ab==0)ab=P;
    }
}
if(ab>0)cout<<ab;
else cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll long long
#define rep(i,N) for(ll i=0; i<N; i++)
const int mx = 2e5;

ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main(void){

  ll H,W,N,M;
  cin >> H >> W >> N >> M;

  int field[H+1][W+1];
  int field2[H+1][W+1];
  rep(i,H+1){
    rep(j,W+1){
      field[i][j] = 0;
      field2[i][j]=0;
    }
  }

  rep(i,N){//BOMB=N:1
    int A,B;
    cin >> A >> B;
    field[A][B] = 1;
  }
  rep(i,M){//WALL=M:2
    int C,D;
    cin>> C >> D;
    field[C][D] = 2;
  }

  vector<pair<int,int>> todo;
  for(int i=1; i<=H; i++){
    bool light = false;
    for(int j=1; j<=W; j++){
      if(field[i][j]==0){
        todo.pb(mp(i,j));
      }else if(field[i][j]==1){
        todo.pb(mp(i,j));
        light = true;
      }else if(field[i][j]==2){
        if(light){
          for(auto& k: todo){
            field2[k.first][k.second]+=3;
          }
        }
        todo.clear();
        light = false;
      }
      if(j==W){
        if(light){
          for(auto& k:todo){
            field2[k.first][k.second]+=3;
          }
        }
        todo.clear();
        light=false;
      }
    }
  }

  for(int i=1; i<=W; i++){
    bool light = false;
    for(int j=1; j<=H; j++){
      if(field[j][i]==0){
        todo.pb(mp(j,i));
      }else if(field[j][i]==1){
        todo.pb(mp(j,i));
        light = true;
      }else if(field[j][i]==2){
        if(light){
          for(auto& k: todo){
            field2[k.first][k.second]+=3;
          }
        }
        todo.clear();
        light = false;
      }
      if(j==H){
        if(light){
          for(auto& k:todo){
            field2[k.first][k.second]+=3;
          }
        }
        todo.clear();
        light=false;
      }
    }
  }

  ll ans=0;
  rep(i,H+1){
    //cout << "\n";
    rep(j,W+1){
      //cout << field2[i][j] << " " ;
      if(field2[i][j]>=3){
        ans++;
      }
    }
  }
  cout << ans;

  return 0;
}

#include<bits/stdc++.h>
#define forn(i, n) for(int i=0;i<(int)(n);i++)
#define for1(i, n) for(int i=1;i<=(int)(n);i++)
#define fore(i, l, r) for(int i=(int)(l);i<=(int)(r);i++)
#define ford(i, n) for(int i=(int)(n)-1;i>=0;i--)
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl "\n"
#define MAX 1000000007
using namespace std;
typedef int64_t ll;
typedef vector<int> vi;
typedef vector<int64_t> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main(int argc, char *argv[]){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	int n;
	string s;
	cin>>n>>s;
	int count=0;
	int a,t,g,c;
	for(int i=0;i<n-1;i++){
		a=0;t=0;g=0;c=0;
		for(int j=i;j<n;j++){
			if(s[j]=='A') ++a;
			if(s[j]=='T') ++t;
			if(s[j]=='G') ++g;
			if(s[j]=='C') ++c;
			count+=(a==t)&&(g==c);
		}
	}
	cout<<count;
	return 0;
}


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

//UnionFindテンプレ
class UnionFind {
    // まとめる 判定 サイズを知る
    public:
    // Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。
    vector<int> Parent;

    // 初期化
    UnionFind(int N) {
        Parent = vector<int>(N, -1);
    }

    // Aのrootを調べる
    int root(int A) {
        if (Parent[A] < 0) return A;  // マイナスならそれはroot
        return Parent[A] = root(Parent[A]);
    }

    // rootの値をプラスに戻して返す(サイズ)
    int size(int A) {
        return -Parent[root(A)];
    }

    // くっつける関数
    bool connect(int A, int B) {
        // AとBのroot同士をくっつける
        A = root(A);  // ここのAは、"rootの場所"の意味
        B = root(B);
        if (A == B) return false;  // 既にくっついている
        if (size(A) < size(B))
        swap(A, B);            // 大きい方にくっつけるために中身交換
        Parent[A] += Parent[B];  // 中身更新
        Parent[B] = A;
        return true;
    }

    //連結か調べる関数
    bool issame(int A, int B) {
        return root(A) == root(B);
    }
};

//chmin,chmax関数(DP用)
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//aのZ/mZでの逆元
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

//最大公約数
int64_t gcd(int64_t a, int64_t b) {
    if (a % b == 0) {
        return b;
    }
    return gcd(b, a%b);
}

//エラストテレスのふるい
vector<bool> make_is_prime(int N) {
    vector<bool> prime(N + 1, true);
    if (N >= 0) prime[0] = false;
    if (N >= 1) prime[1] = false;
    for (int i = 2; i * i <= N; i++) {
        if (!prime[i]) continue;
        for (int j = i * i; j <= N; j += i) {
            prime[j] = false;
        }
    }
    return prime;
}
//累乗a^b
int64_t pow(int a, int b) {
    int64_t ans = (int64_t)1;
    for (int i = 0; i < b; i++) {
        ans *= (int64_t)a;
    }
    return ans;
}

//ここから

int main() {
    int N,S,D; cin>>N>>S>>D;
    for (int i = 0; i < N; i++) {
        int X,Y; cin>>X>>Y;
        if (X < S && Y > D) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long llp;
#define mod 1000000007
#define deb(x) cerr << '\t' << "[" << #x << ": "<< x << "]\n";
#define f first
#define s second
#define pb push_back
/*
numeric_limits<llp>::max();
*/
void solution();
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    llp tc = 1;
    //cin >> tc;
    for (llp t = 0; t < tc; ++t) {
		//cout << "Case #" << tc+1 << ": ";
		solution();
	}
	//intmax = 2e9 (ten digits)
    return 0;
}

vector<int> seive(int n) {
/*Returns a vector<int> with all the prime numbers till n
 * - Time complexity: O(nlglg(n))
 * - n >= 2
 * - if n is less returns a empty vector
*/
	vector<bool> is_prime(n+1, 1);
	is_prime[0] = is_prime[1] = 0;
	for(int i = 2; i*i <= n; ++i) {
		if(is_prime[i]) {
			for(int j = i*i; j <= n; j+=i) is_prime[j] = 0;
		}
	}

	vector<int> primes;
	for(int i = 0; i < n+1; ++i) if(is_prime[i]) primes.push_back(i);
	return primes;
}

int fx(int p, int n) {
	int c = -1;
	while(n) {
		c++;
		n/=p;
	}
	return c;
}
void solution() {
	int n, s, d;
	int x, y;
	cin >> n >> s >> d;
	bool canwe = 0;
	for(int i = 0 ;i < n; ++i) {
		cin >> x >> y;
		if(x < s and y > d) canwe = 1;
	}
	cout << "No\0Yes\0"+3*canwe;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=998244353;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

class union_find_tree{
private:
    static constexpr int N=100005;
    int n;
    queue<int> q1;
    vector<int> parent;
    vector<int> num;
public:
    void init(){
        parent.assign(n,-1);
        num.assign(n,1);
    }
    union_find_tree(int n_init){
        assert(n_init>=0);
        n=n_init;
        init();
    }
    union_find_tree(){
        n=N;
        init();
    }

    int check_parent(int x){
        assert(x>=0 && x<n);
        if(parent[x]!=-1){
            q1.push(x);
            return check_parent(parent[x]);
        }
        int a;
        while(!q1.empty()){
            a=q1.front(),q1.pop();
            parent[a]=x;
        }
        return x;
    }

    bool connect(int x,int y){
        assert(x>=0 && x<n);
        assert(y>=0 && y<n);
        x=check_parent(x),y=check_parent(y);
        if(x==y)return true;
        if(num[x]>num[y])swap(x,y);
        parent[x]=y;
        num[y]+=num[x];
        return false;
    }

    int size(int pos){
        pos=check_parent(pos);
        return num[pos];
    }

    bool check(int x,int y){//connect:true
        assert(x>=0 && x<n);
        assert(y>=0 && y<n);
        return check_parent(x)==check_parent(y);
    }
};

long long int pow_mod(long long int a,long long int n,long long int p=1e9+7){
    //a^n mod p
    long long int b=1,t=1;
    for(;b<=n;b<<=1);
    for(b>>=1;b>0;b>>=1){
        t*=t;
        if(t>=p)t%=p;
        if(n&b)t*=a;
        if(t>=p)t%=p;
    }
    return t;
}

namespace sol{

    void solve(){
        int n,m;
        int i,j,k;
        int a,b,c;
        cin>>n;
        union_find_tree ua(n);
        for(i=0;i<n;i++){
            cin>>a;
            a--;
            ua.connect(a,i);
        }
        int s=0;
        for(i=0;i<n;i++){
            if(ua.check_parent(i)==i)s++;
        }
        LL ans=pow_mod(2,s,MAX)-1;
        if(ans<0)ans+=MAX;
        cout<<ans<<endl;
    }
}

int main(){
    sol::solve();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll z = 0, mod = 998244353;

ll ruijo(ll a, ll b) {
  ll res = 1;
  for (a %= mod; b; a = a * a % mod, b >>= 1)
    if (b & 1) res = res * a % mod;
  return res;
}

int main(){
  ll N;
  cin >> N;

  vector<ll> f(N + 1);
  for (ll i = 1; i <= N; i++){
    cin >> f[i];
    //f[i] = i;
  }

  vector<ll> par(N + 1, -1);
  ll loopnum = 0;
  for (ll i = 1; i <= N; i++){
    if (par[i] > -1) continue;

    ll now = i;
    while (par[now] == -1){
      par[now] = i;
      now = f[now];
    }
    if (par[now] == i) loopnum++;
  }
  //cout << loopnum << endl;
  cout << ruijo(2, loopnum) - 1 << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
using pii=std::pair<int,int>;
using u32=unsigned;
using u64=unsigned ll;
inline bool up(int&x,const int y){return x<y?x=y,1:0;}
inline bool down(int&x,const int y){return x>y?x=y,1:0;}
#define N 1005
using vec=int[N];
vec dp,a,b,cpy;
vec l,r;
#define FAIL std::cout<<"No\n"
int main(){
	std::ios::sync_with_stdio(0);
	int n,cnt=0; std::cin>>n;
	rep(i,1,n){
		int x,y; std::cin>>x>>y;
		if(x>0&&y>0&&x>y){
			return FAIL,0;
		}
		if(x>0) l[x]=1,a[x]=std::max(y,0);
		if(y>0) r[y]=1,b[y]=std::max(x,0);
		cpy[++cnt]=x,cpy[++cnt]=y;
	}
	std::sort(cpy+1,cpy+cnt+1);
	rep(i,2,cnt){
		if(cpy[i]==cpy[i-1]&&cpy[i]>0&&cpy[i-1]>0){
			return FAIL,0;
		}
	}
	dp[0]=1;
	rep(i,1,n) rep(j,0,i-1) if(dp[j]){
		bool ty=1;
		rep(k,j+1,i){
			if((a[j+k]&&a[j+k]!=i+k)||l[i+k]||(b[i+k]&&b[i+k]!=j+k)||r[j+k])
				ty=0;
			if(l[j+k]&&r[i+k]&&a[j+k]==0&&b[i+k]==0) ty=0;
		}
		dp[i]|=ty;
	}
	std::cout<<(dp[n]?"Yes":"No")<<'\n';
	return 0;
}

#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
#include <complex>

using namespace std;

#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define INF 1001001001001001011
#define ld(val) printf("%s : %lld\n", #val, val);
#define sd(val) printf("%s : %s\n", #val, val.c_str());

typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
typedef tuple<ll, ll, ll> TP;

int main() {
  string x;
  ll m;
  cin >> x >> m;
  ll md = 0;
  rep(i, x.size()) {
    ll t = x[i]-'0';
    md = max(md, t);
  }

  if(x.length() == 1) {
    if(x[0]-'0'<=m) cout << 1 << endl;
    else cout << 0 << endl;
    return 0;
  }

  ll l=md;
  ll r=m+1;
  while(r-l>1) {
    ll v = (l+r)/2;
    ll ret = 0;
    for(ll i=0; i<x.size(); i++) {
      ll t = x[i] - '0';
      if (ret > m/v) {
        ret = m+1;
        break;
      } else {
        ret = (ret*v) + t;
      }
    }
    if(ret>m) {
      r=v;
    } else {
      l=v;
    }
  }
  cout << l-md << endl;
  return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::vector<int> P;
typedef std::pair<int,int> pii;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
template <typename T> bool umax(T& a,T t){if(t>a)return a=t,1;return 0;}
template <typename T> bool umin(T& a,T t){if(t<a)return a=t,1;return 0;}
/**********/
const int MAXN = 200011;
struct one{int t,x,y;}a[MAXN];
int fx[MAXN],diff=0;
int place(int val){return std::lower_bound(fx+1,fx+diff+1,val)-fx;}
struct sgt
{
	struct node
	{
		ll cnt,sum;
	}t[MAXN<<2|1];
#define rt t[num]
	void modify(un pos,ll k,un l=1,un r=diff,un num=1)
	{
		rt.cnt+=k,rt.sum+=k*fx[pos];
		if(l==r)return;
		un mid=(l+r)>>1;
		if(pos<=mid)modify(pos,k,l,mid,num<<1);
		else modify(pos,k,mid+1,r,num<<1|1);
	}
	ll Qcnt(un ql,un qr,un l=1,un r=diff,un num=1)
	{
		if(ql>qr)return 0;
		if(ql<=l&&r<=qr)return rt.cnt;
		un mid=(l+r)>>1;
		ll res=0;
		if(ql<=mid)res+=Qcnt(ql,qr,l,mid,num<<1);
		if(qr>mid)res+=Qcnt(ql,qr,mid+1,r,num<<1|1);
		return res;
	}
	ll Qsum(un ql,un qr,un l=1,un r=diff,un num=1)
	{
		if(ql>qr)return 0;
		if(ql<=l&&r<=qr)return rt.sum;
		un mid=(l+r)>>1;
		ll res=0;
		if(ql<=mid)res+=Qsum(ql,qr,l,mid,num<<1);
		if(qr>mid)res+=Qsum(ql,qr,mid+1,r,num<<1|1);
		return res;
	}
}t[2];
int seq[2][MAXN];
ll f[2][MAXN];
int main()
{
	int n1=read(),n2=read(),m=read();
	fx[++diff]=0;
	for(int i=1;i<=m;++i)a[i].t=read()-1,a[i].x=read(),fx[++diff]=a[i].y=read();
	std::sort(fx+1,fx+diff+1),diff=std::unique(fx+1,fx+diff+1)-fx-1;
	t[0].modify(1,n1),t[1].modify(1,n2);
	for(int i=1;i<=n1;++i)seq[0][i]=1;
	for(int i=1;i<=n2;++i)seq[1][i]=1;
	ll ans=0;
	for(int i=1;i<=m;++i)
	{
		int tp=a[i].t,val=place(a[i].y);
		int& pre=seq[tp][a[i].x];
		///printf("%d to %d(%d)\n",pre,val,a[i].y);
		if(pre<=val)
		{
			//printf("%d\n",a[i].y-fx[pre]);
			ans+=t[!a[i].t].Qcnt(1,pre)*(a[i].y-fx[pre])+ t[!a[i].t].Qcnt(pre+1,val)*a[i].y-t[!a[i].t].Qsum(pre+1,val);
		}
		else
		{
			ans-=t[!a[i].t].Qcnt(1,val)*(fx[pre]-a[i].y)+ t[!a[i].t].Qcnt(val+1,pre)*fx[pre]-t[!a[i].t].Qsum(val+1,pre);
		}
		t[a[i].t].modify(pre,-1);
		t[a[i].t].modify(pre=val,1);
		printf("%lld\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int ll
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 mrand(time(0));
#define debug(...) fprintf(stderr, __VA_ARGS__)
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }

int n, m, dis[100100], head[100100], tot, v[100100];
struct Edge {
    int to, nxt, c, d, sd;
    int f(int t) {
        t = max(t, sd - 1);
        return c + d / (t + 1) + t;
    }
} e[200100];
void addEdge(int a, int b, int c, int d) {
    e[++tot] = (Edge){ b, head[a], c, d, (int)round(sqrt(d))}, head[a] = tot;
    e[++tot] = (Edge){ a, head[b], c, d, (int)round(sqrt(d))}, head[b] = tot;
}
signed main() {
    scanf("%lld %lld", &n, &m);
    for(int i = 1, a, b, c, d; i <= m; i++) {
        scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
        addEdge(a, b, c, d);
    }
    memset(dis, 0x3f, sizeof(dis)); dis[1] = 0;
    priority_queue<pii> q; q.push(pii(0, 1)); // <-val, pos>
    while(q.size()) {
        pii x = q.top(); q.pop();
        if(v[x.se]) continue; v[x.se] = 1;
        for(int i = head[x.se]; i; i = e[i].nxt) {
            int y = e[i].to;
            if(dis[y] > e[i].f(-x.fi)) {
                dis[y] = e[i].f(-x.fi);
                q.push(pii(-dis[y], y));
            }
        }
    }
    printf("%lld\n", dis[n] > 1e18 ? -1 : dis[n]);
    debug("time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for (int i =0;i<(n);++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;
int main(){
 int X,Y,Z;
 cin >>X>>Y>>Z;
 int tmp=Y*Z;
 if(tmp%X==0)cout <<tmp/X-1;
 else cout <<tmp/X;
}

#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const int MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;}

int main() {
    ll x,y,z;
    scanf("%lld %lld %lld", &x, &y, &z);

    ll mav = 0;
    for (int i = 1; i <= 10000000; i++) {
        // y/x > t/z
        // x*t > z*y;
        if (y*z > i*x) mav = max(mav, (ll)i);
    }

    printf("%lld", mav);
}

// author: rdd6584


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 200100
#define MOD 1000000007  //998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
	Setup_io() {
		ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
		cout << fixed << setprecision(15);
	}
} setup_io;

int main() {
	ll n, m,mm, x, y;
	x = (ll)10;
	y = (ll)1;
	cin >> n >> m;
	mm = m * m;
	while (n > 0) {
		if (n & 1)y = (y*x) % mm;
		x = (x*x) % mm;
		n /= 2;
	}
	y /= m;
	cout << y << endl;
	return 0;
}

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

long long binpow(long long i, long long x, long long MOD)
{
    long long rs=1;
    long long a=x;
    while(i)
    {
        if (i&1)
        {
            i--;
            rs*=a;
            rs%=MOD;
        }
        else
        {
            i/=2;
            a*=a;
            a%=MOD;
        }
    }
    return rs%MOD;
}

void solve()
{
    long long n,m;
    cin >> n >> m;
    long long MOD = m*m;

    long long x = binpow(n,10,MOD);
    //cout << x << "\n" ;

    //x+=MOD;
    x = (x-x%m)/m; //cout << x << "\n";
    cout << x-(x%m)/m;
}

int main()
{
int t=1;
//cin >> t;
while (t--) solve();
}


#include <bits/stdc++.h>
//#include <atcoder/lazysegtree>
//#include <atcoder/segtree>
//#define rep(i,a,n) for (int i=a;i<n;i++)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pause "read -p 'Press Enter to continue...' var"

using namespace std;
//using namespace atcoder;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

/*
テスト通りますように
　　●
　　/⌒ヽ
　 ｜ 　|/⌒ヽ(ヽ
　 (｀　∥ー⌒) |
　|￣||￣￣￣￣￣|
　|―||―――――|
　|　Ｕ　　　　　|
　|￣￣￣￣￣￣￣|
　|＿＿＿＿＿＿＿|
　　|―――――|
　　|―――――|
ｗｗＷｗｗＷｗＷＷｗ
*/

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 200005;
const int MOD = 3;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
  ll n,k;
  cin >> n >> k;
  vpll p;
  rep(i,n){
    ll a,b;
    cin >> a >> b;
    p.pb(mp(a,b));
  }
  sort(ALL(p));
  ll ans = k;
  rep(i,n){
    if(p[i].fi <= ans) ans += p[i].se;
  }
  cout << ans << endl;
  return 0;
}

/* Goal to be a Master */
      #include <algorithm>
      #include <bits/stdc++.h>
      #include<string>
      using namespace std;
      #define endl "\n"
      #define fo1(i,n) for(int i=1;i<=n;i++)
      #define min3(a,b,c) min(a,min(b,c))
      #define s(v) (ll)v.size()
      #define e(v) v.empty()
      #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
      #define vi vector<int>
      #define ll long long
      #define fi first
      #define se second
      #define db double
      #define U unsigned
      #define P pair<int,int>
      #define vll vector<ll>
      #define pll pair<ll,ll>
      #define pb push_back
      #define eb emplace_back
      #define mp make_pair
      #define all(x) x.begin(),x.end()
      #define CLR(i,a) memset(i,a,sizeof(i))
      #define fo(i,a,b) for(int i = a;i <= b;++i)
      #define ro(i,a,b) for(int i = a;i >= b;--i)
      #define ppll pair < pll , pll >
      #define sd(x) scanf("%d",&x)
      #define sd2(x,y) scanf("%d %d",&x,&y)
      #define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z)
      #define sd4(x,y,z,u) scanf("%d %d %d %d",&x,&y,&z,&u)
      #define sld(x) scanf("%lld",&x)
      #define INF 1e18
      #define eps 0.00001
      #define le length
     #define debug(n1) cout << n1 << "\n"

int binex(int base,int pow)
{
    int res=1;
    while(pow)
    {
    if(pow%2)
    { res*=base;pow--;}
    else
    {base*=base;pow=pow/2;}
    }
     return res;
}
      void sieve(int a[],int N)
       {
       for(int i=1;i<=N;i++)
        a[i]=1;
         a[1]=0;
       for(int i= 2;i*i<=N;i++)
       {
         if(a[i])
        {
        for(int j=i*i;j<=N;j+=i)
         a[j]=0;
        }
       }
       }
string CONVERT_TO_BINARY(int s) {
    string res = "";
    while(s != 0) {
        res += (char)('0' + s % 2);
        s /= 2;
    }
    reverse(res.begin() , res.end());
    return res;
}
int STOI(string s) {
    int num = 0;
    int po = 1;
    for(int i=s.length()-1;i>=0;i--)
    {
        num += po * (s[i] - '0');
        po *= 10;
    }
    return num;
}
int main()
{
	IOS
    int n,m;
    cin >> n >> m;
    ll sum=0;
    vector<pair<ll,int>> v;
    for(int i=1;i<=n;i++)
    {
        ll x,y;
        cin>>x>>y;
        v.pb(mp(x,y));
    }
    sort(all(v));
    ll curr=m;
    //int prev=0;
    /*for(int i=0;i<v.size();i++)
    {

        if(curr<(v[i].first-prev))
         break;
        curr-=(v[i].first-prev);
        sum+=(v[i].first-prev);
        prev=v[i].first;
        curr+=v[i].second;
    }*/
    for(int i=0;i<v.size();i++)
    {
       if(v[i].first<=curr)
        curr+=v[i].second;

    }
    cout<<sum+curr;
    return 0;
}


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

int main()
{
	int len;
	cin >> len;
	string arr;
	LL ans=1;
	rec[3]=1;rec[4]=2;rec[2]==1;
	for(int i=0;i<4;++i)
	{
		char tem; cin >> tem; arr+=tem;
	}
	if(len<4||arr[0]=='A'&&arr[1]=='A'||arr[1]=='B'&&arr[3]=='B'){cout << 1;return 0;}
	for(int i=5;i<=len;i++)
		rec[i]=(rec[i-1]+rec[i-2])%MOD;
	for(int i=4;i<=len;++i)
		ans=(ans<<1)%MOD;
	if(arr=="BAAB"||arr=="BBBA"||arr=="ABBA"||arr=="BAAA")
	{
		cout <<rec[len]; return 0;
	}
	else
		cout << ans;
	return 0;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1003;
const ll MOD = int(1e9) + 7;

ll add(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  ll ans = (a + b) % MOD;
  return ans;
}

ll sub(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  ll ans = (a - b + MOD) % MOD;
  return ans;
}

ll mul(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  ll ans = (a * b) % MOD;
  ans += MOD; // in case of negative result
  ans %= MOD;
  return ans;
}

// Iterative Function to calculate (x^y) % MOD in O(log y)
ll fastPow(ll x, ll y) {
  ll res = 1;
  x %= MOD;
  while(y > 0) {
    if(y % 2 == 1) {
      res = mul(res, x);
    }
    y /= 2;
    x = mul(x, x);
  }
  return res;
}

// Returns n^(-1) mod p (Fermat's little theorem)
ll modInverse(ll n) {
  return fastPow(n, MOD - 2);
}

ll fact[N];

// Returns nCr % p using Fermat's little theorem.
ll comb(ll a, ll b) {
  if(b == 0) return 1;
  return mul(mul(fact[a], modInverse(fact[b])), modInverse(fact[a - b]));
}

void initFact() {
  fact[0] = 1;
  for(int i = 1; i < N; ++i) fact[i] = mul(ll(i), fact[i - 1]);
}

ll one(int n) {
  ll ans = 0;
  int top = n - 2, bot = 0;
  while(top >= bot) {
    ans = add(ans, comb(top, bot));
    --top;
    ++bot;
  }
  return ans;
}

ll two(int n) {
  return fastPow(ll(2), ll(n - 3));
}

int main() {
  ios_base::sync_with_stdio(false);

  initFact();

  int n;
  char caa, cab, cba, cbb;
  cin >> n;
  cin >> caa >> cab >> cba >> cbb;

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

  /*
  one: fibonacci, two: all possible strings (2^n)
  AAAA -> 1
  AAAB -> 1
  AABA -> 1
  AABB -> 1
  ABAA -> two
  ABAB -> 1
  ABBA -> one
  ABBB -> 1
  BAAA -> one
  BAAB -> one
  BABA -> two
  BABB -> two
  BBAA -> two
  BBAB -> 1
  BBBA -> one
  BBBB -> 1
  */
  // invariant: all generated string starts with A, ends with AB pattern
  if(cab == 'A') {
    // A..AB
    if(caa == 'A') {
      cout << "1\n"; // only A...AB (no change)
    } else {
      // A..ABA..ABA..AB (can insert 1 B between the As)
      if(cba == 'A') {
        cout << one(n) << "\n"; // only A...ABA...ABA...AB (no change)
      } else {
        // A..AB..BA..AB..BA..AB (can insert multiple Bs)
        // whether cbb is A or B, the pattern will not change
        cout << two(n) << "\n";
      }
    }
  } else {
    // AB...B
    if(cbb == 'B') {
      cout << "1\n"; // only AB...B (no change)
    } else {
      // AB..BAB..BAB..BAB (can insert 1 A between the Bs)
      if(cba == 'B') {
        cout << one(n) << "\n"; // only AB..BAB..BAB..BAB (no change)
      } else {
        // AB..BA..AB..BA..AB..BA..AB (can insert multiple As)
        // whether caa is A or B, the pattern will not change
        cout << two(n) << "\n";
      }
    }
  }

  return 0;
}