**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	int a=0,b=1;char c=getchar();
	for(;c<'0'||c>'9';c=getchar())if(c=='-')b=-1;
	for(;c>='0'&&c<='9';c=getchar())a=a*10+c-'0';
	return a*b;
}
int a[100001],b[100001];
int main()
{
	int T=read();
	while(T--)
	{
		int n=read();
		int Max=0,Min=n+1;
		for(int i=1;i<=n;i++)b[i]=0;
		for(int i=1;i<=n;i++)
		{
			a[i]=read();
			if(a[i]!=i)Max=max(Max,a[i]),Min=min(Min,a[i]),b[a[i]]=i;
		}
		int can=0;
		for(int i=1;i<=n;i++)
		{
			if(b[i]==a[i])
			{
				if(can==0)can=i+(a[i]);
				else if(can!=i+a[i])
				{
					can=-1;
					break;
				}
			}
		}
		ll ans=0;
		if(Max==0&&Min==n+1)
		{
			cout<<(1+2*n)*n<<endl;
			continue;
		}
		for(int i=1;i<=Min+n;i++)
		{
			ans+=2*n-max(i,Max);
//			cout<<i<<' '<<2*n-max(i,Max)<<endl;
		}
		if(can!=0&&can!=-1)ans++;
		cout<<ans<<endl;
	}
	return 0;
}
/*
1
4
3 2 1 4

*/

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided buggy code, we need to focus on edge cases and scenarios that might cause the code to fail. Let's break down the requirements and identify potential issues:

1. **Edge Cases:**
   - A permutation where the elements are already sorted.
   - A permutation where the elements are in reverse order.
   - A permutation with a small number of elements (e.g., 2 or 3).
   - A permutation with a large number of elements (up to the limit of 100,000).

2. **Specific Scenarios:**
   - A permutation where the elements are mostly in order but with a few swaps needed.
   - A permutation where the elements are spread out such that the sum of the elements to be swapped is large.

3. **Buggy Code Analysis:**
   - The code has a loop that checks if `b[i] == a[i]` and tries to determine a specific value for `can`. This seems to be an attempt to identify a specific pattern in the permutation, but it might not handle all cases correctly.
   - The code calculates the number of valid pairs `(l, r)` but might not correctly handle the constraints for `l` and `r`.

Given these considerations, let's generate a test input that includes a permutation where the elements are mostly in order but with a few swaps needed, and also includes a permutation that is already sorted.

### Challenging Test Input
```plaintext
3
4
3 2 1 4
5
1 2 3 4 5
3
3 1 2
```

### Explanation:
1. **Test Case 1:**
   - Permutation: `3 2 1 4`
   - This permutation requires swaps to sort it. The elements `3` and `1` need to be swapped, and `2` and `1` need to be swapped. This should expose any issues in the logic that handles the constraints for `l` and `r`.

2. **Test Case 2:**
   - Permutation: `1 2 3 4 5`
   - This permutation is already sorted. The code should correctly handle this case and return the maximum number of pairs `(l, r)`.

3. **Test Case 3:**
   - Permutation: `3 1 2`
   - This permutation requires swaps to sort it. The elements `3` and `1` need to be swapped, and `1` and `2` need to be swapped. This should test the logic for handling smaller permutations.

This test input should help identify any issues in the buggy code and expose the logic errors.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#include <bits/stdc++.h>
using namespace std;
// typedef is used to create an alias for a data type

typedef long long ll;
typedef long long int lli;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<lli> vlli;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef long double ld;
typedef tuple<int, int, int> tiii;

// #define is used to define a constant value
#define int ll
#define sz(x) (int)x.size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define lfastio static const auto speedup = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0;}();
#define pb(a) push_back(a)
#define pbr(a, b) pb(make_pair(a, b))
#define F first
#define S second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rfr(i, a, b) for (int i = a; i >= b; i--)
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define pVec(vecName) for(auto a:vec){cout<<a<<" ";}

void solve(){
    int n;cin>>n;
    vector<int>per(n);
    forn(i,n)cin>>per[i];
    int mn=n+1,mx=0;
    int count=0;
    forn(i,n){
        if(per[i]!=i+1){
            count++;
            if(per[i]<mn)mn=per[i];
            if(per[i]>mx)mx=per[i];
        }
    }
    if(mn>mx){cout<<(2*n*(2*n+1))/2<<endl;return;}
    int ans=0;
    for(int i=1;i<=n+mn;i++){
        if(i<mx+1)ans+=2*n-mx;
        else{ans+=2*n-i;}
        // cout<<ans<<endl;
    }
    if(count==2)ans+=1;
    cout<<ans<<endl;

}
signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    // cout<<"Hello World"<<endl;
    int t;cin>>t;
    while (t--)
    {
        solve();
    }

    // solve();
    return 0;
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To expose the bug in the given code, we need to focus on edge cases and scenarios that might cause the algorithm to fail. The code seems to have issues with handling the constraints and the logic for calculating the number of valid pairs \((l, r)\).

### Analysis of the Buggy Code:
1. **Initialization of `mn` and `mx`:**
   - `mn` is initialized to `n + 1` and `mx` to `0`.
   - The code updates `mn` and `mx` based on the elements that are not in their correct positions.
   - If `mn` is greater than `mx` after the loop, it prints the total number of pairs \((l, r)\) which is \(\frac{(2n)(2n+1)}{2}\).

2. **Counting valid pairs:**
   - The code iterates from `1` to `n + mn` and calculates the number of valid pairs based on the values of `mn` and `mx`.
   - There is a special case where if `count` (number of elements out of place) is `2`, it adds `1` to the answer.

### Potential Bugs:
1. **Incorrect handling of `mn` and `mx` initialization:**
   - If `mn` is greater than `mx`, it should not always return the total number of pairs. This could be a bug.
2. **Incorrect logic for counting valid pairs:**
   - The loop that calculates the number of valid pairs might be missing some cases or overcounting.

### Test Input to Expose the Bug:
To expose the bug, we can use a permutation where the elements are not in their correct positions, and the values of `mn` and `mx` are such that the code's logic for counting valid pairs might fail.

### Example Test Input:
```plaintext
1
4
4 3 2 1
```

### Explanation:
- **Input:**
  - `n = 4`
  - Permutation: `[4, 3, 2, 1]`
- **Expected Output:**
  - The correct number of pairs \((l, r)\) that can sort the permutation is `23`.

### Why This Test Input?
- In this permutation, all elements are out of place, and the values of `mn` and `mx` will be `1` and `4`, respectively.
- The code might fail to correctly count the number of valid pairs due to the logic in the loop and the special case handling.
- This input should help expose any issues with the logic for calculating the number of valid pairs.

### Expected Behavior:
- The correct answer for this input should be `23`, but the buggy code might produce a different result, thus exposing the bug.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")

#define int long long
int a[100001],n;
void Delta() {
   cin >> n;
   for(int i=1;i<=n;++i) cin >> a[i];
   int cnt=0,mx=0,mn=2147483647,s=0;
   for(int i=1;i<=n;++i)
      if(a[i]!=i) {
         cnt++;
         mx=max(mx,a[i]);
         mn=min(mn,a[i]);
      }
   for(int i=1;i<=n;++i)
      s+=n*2-max(mx,i);
   for(int i=1;i<=n;++i)
      if(i<=mn)
         s+=n-i;
   cout << (cnt==0?(n*2)*(n*2+1)/2:s+(cnt==2)) << endl;
}
signed main() {
   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   int T=1; cin >> T;
   while(T--) Delta();
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided buggy code, we need to focus on edge cases and scenarios that maximize the failure probability. The key points to consider are:

1. **Permutations with specific patterns**: Create permutations that have specific elements out of order, which might expose issues in the logic for counting valid pairs.
2. **Boundary conditions**: Use small values of \( n \) to ensure the test case is small but still challenging.
3. **Special cases**: Consider permutations that are already sorted, permutations with a single swap needed, and permutations with multiple swaps needed.

Given these considerations, let's create a test input that includes a permutation that requires multiple swaps and has elements out of order in a way that might expose the bug.

### Test Input
```plaintext
3
3
3 1 2
4
4 2 1 3
5
5 3 1 2 4
```

### Explanation
1. **Test Case 1: \( n = 3 \)**
   - Permutation: \( [3, 1, 2] \)
   - This permutation requires multiple swaps to sort. The elements 3, 1, and 2 need to be moved to their correct positions. This case should expose issues in the logic for counting valid pairs.

2. **Test Case 2: \( n = 4 \)**
   - Permutation: \( [4, 2, 1, 3] \)
   - This permutation has elements out of order in a way that requires multiple swaps. The elements 4, 2, 1, and 3 need to be moved to their correct positions. This case should also expose issues in the logic for counting valid pairs.

3. **Test Case 3: \( n = 5 \)**
   - Permutation: \( [5, 3, 1, 2, 4] \)
   - This permutation has elements out of order in a way that requires multiple swaps. The elements 5, 3, 1, 2, and 4 need to be moved to their correct positions. This case should further expose issues in the logic for counting valid pairs.

These test cases are small but cover a range of scenarios that might expose the bug in the provided code.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import heapq
import os, sys
from io import BytesIO, IOBase

from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from heapq import heappush, heappop
from functools import lru_cache, reduce
from itertools import accumulate
from operator import xor
import random
import math
import sys

# sys.setrecursionlimit(10000)
# Fast IO Region
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
sys.setrecursionlimit(10000)

mag = random.getrandbits(20)

lsum = lambda l, r: (l + r) * (r - l + 1) // 2

class D:
    def __init__(self, type=int):
        self.d = defaultdict(type)
        self.mag = random.getrandbits(20)

    def __setitem__(self, key, value):
        self.d[key + self.mag] = value

    def __getitem__(self, key):
        return self.d[key + self.mag]

    def __delitem__(self, key):
        del self.d[key + self.mag]

    def __contains__(self, key):
        return key + self.mag in self.d

    def __iter__(self):
        self.keys = iter(self.d.keys())
        return self

    def __next__(self):
        return next(self.keys) - self.mag

    def __len__(self):
        return len(self.d)

ii = lambda: int(input())
mii = lambda: map(int, input().split())
fii = lambda: map(float, input().split())
lmii = lambda: list(map(int, input().split()))
i2c = lambda n: chr(ord('a') + n)
c2i = lambda c: ord(c) - ord('a')

# mod = 998244353

mod = 10 ** 9 + 7

def solve():
    n = ii()
    l = lmii()
    sl, sr = 0, n
    for i in range(n):
        if l[i] != i + 1:
            break
        sl += 1
    for i in range(n - 1, -1, -1):
        if l[i] != i + 1:
            break
        sr -= 1
    if sl == n:
        print(n * (2 * n + 1))
        return
    res = n * (n + 1) - 1
    if sl > 0:
        res += lsum(n - 2, n - sl - 1)
    if sr < n:
        res += lsum(sr, n - 1)
    S = set()
    for i in range(n):
        if l[i] != i + 1:
            S.add((l[i] + i + 1) ^ mag)
    if len(S) == 1:
        res += 1
    print(res)

for _ in range(ii()):
    solve()

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
1
1
2
2 1
3
3 1 2
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
from sys import stdout
from sys import stdin
from math import ceil
I=lambda:list(map(int,input().split()))
n=int(input())
for i in range(n):
    N=int(input())
    P=I()
    dis=0
    dmi=999999
    dma=0
    for i in range(N):
        if P[i]!=i+1:
            dis+=1
            dmi=min(dmi,i)
            dma=max(dma,i)
    if dis==0:print(N*(2*N+1));continue
    else:
        dmi,dma=dma+1,dmi+N+2
        ans=N*(2*N+1)
        ans-=dmi*(dmi+1)//2
        tmp=N*2-dma+1
        ans-=(tmp+1)*tmp//2
        ans-=dma-dmi-1
        if dis==2:ans+=1
        print(ans)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
1
1
2
2 1
4
4 3 2 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import sys
import heapq
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
for __ in range(fg()):
    n = fg()
    p = fgh()
    if p == sorted(p):
        print(2 * n * (2 * n + 1) // 2)
    else:
        c = 0
        while p[c] == c + 1:
            c += 1
        d = 0
        while p[n - d - 1] == n - d:
            d += 1
        L = c + 1
        R = n - d
        ans = 0
        fl = 1
        for i in range(L, R + 1):
            if p[i - 1] != R - i or p[R - (i - L) - 1] != i:
                fl = 1
        for l in range(1, 2 * n + 1):
            minr = R + 1
            k = l - L
            if l <= L + n:
                if R - L <= 2 and l % 2 and 2 * L + 1 <= l <= 2 * R - 1:
                    ans += max(0, 2 * n - max(minr, l) + 1)
                else:
                    ans += max(0, 2 * n - max(minr, l + 1) + 1)
        print(ans)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided code, we need to focus on edge cases and scenarios that can cause the code to fail. The code seems to have issues with handling specific permutations and boundary conditions. Here are the steps to create such a test input:

1. **Small Permutations with Special Properties**:
   - Permutations where the elements are already sorted or nearly sorted.
   - Permutations with specific patterns that might cause the code to miscalculate the number of valid pairs.

2. **Boundary Conditions**:
   - Test cases with the minimum and maximum values of `n`.
   - Test cases where the array is already sorted or reverse sorted.

3. **Special Patterns**:
   - Permutations where the elements are in a specific order that might cause the code to misinterpret the valid pairs.

Let's create a test input that includes these scenarios:

```plaintext
5
1
1
2
2 1
3
3 1 2
4
4 3 2 1
5
5 1 2 3 4
```

### Explanation:
1. **Test Case 1**: `n = 1`
   - Array: `[1]`
   - This is the simplest case where the array is already sorted. The number of valid pairs should be 1.

2. **Test Case 2**: `n = 2`
   - Array: `[2, 1]`
   - This is a simple case where the array is reversed. The number of valid pairs should be 6.

3. **Test Case 3**: `n = 3`
   - Array: `[3, 1, 2]`
   - This is a more complex permutation. The number of valid pairs should be 11.

4. **Test Case 4**: `n = 4`
   - Array: `[4, 3, 2, 1]`
   - This is a reverse-sorted array. The number of valid pairs should be 23.

5. **Test Case 5**: `n = 5`
   - Array: `[5, 1, 2, 3, 4]`
   - This is a permutation where one element is out of place. The number of valid pairs should be 29.

These test cases cover a range of scenarios, including already sorted arrays, reverse-sorted arrays, and permutations with specific patterns. They should help expose any bugs in the provided code.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import static java.lang.Math.*;
import java.util.*;
import java.io.*;

public class x1973
{
    static final int INF = Integer.MAX_VALUE/2;
    public static void main(String[] args) throws Exception
    {
        FastScanner infile = new FastScanner();
        int T = infile.nextInt();
        while(T-->0)
        {
            int N = infile.nextInt();
            int[] p = new int[N+1];
            for(int i=1; i <= N; i++)
                p[i] = infile.nextInt();
            boolean identity = true;
            for(int i=1; i <= N; i++)
                identity &= p[i] == i;
            if(identity)
            {
                System.out.println((long)N*(2*N+1));
                continue;
            }
            long res = 1;
            int val = -1;
            for(int a=1; a <= N; a++)
            {
                if(a != p[p[a]])
                    res = 0;
                else if(a != p[a])
                {
                    if(val == -1)
                        val = abs(a-p[a]);
                    else if(val != abs(a-p[a]))
                        res = 0;
                }
            }
            int[] pref = new int[N+1];
            for(int i=1; i <= N; i++)
                pref[i] = pref[i-1] + (p[i] != i ? 1 : 0);
            int needFix = pref[N];
            for(int R=3; R <= 2*N; R++)
            {
                int upper = min(N, R-1);
                if(pref[upper] != needFix)
                    continue;
                int l = 1;
                int h = R-1;
                while(l != h)
                {
                    int mid = (l+h+1)>>1;
                    int lower = max(1, mid-N);
                    if(pref[lower-1] == 0)
                        l = mid;
                    else
                        h = mid-1;
                }
                //System.out.println(R+": "+l);
                res += l;
            }
            System.out.println(res);
        }
    }
}
/*
E:
L = N, R = N+1

boolean[] seen = new boolean[N];
            for(int a=0; a < N; a++)
                if(!seen[a])
                {
                    seen[a] = true;
                    int curr = p[a];
                    ArrayList<Integer> path = new ArrayList<>();
                    path.add(a);
                    while(curr != a)
                    {
                        path.add(curr);
                        seen[curr] = true;
                        curr = p[curr];
                    }
                    Collections.sort(path);
                    if(path.size() >= 2)
                    {
                        int diff = path.get(1)-path.get(0);
                        for(int i=2; i < path.size(); i++)
                            if(path.get(i)-path.get(i-1) != diff)
                                res = 0;
                    }
                }
 */
class FastScanner
{
    private int BS = 1 << 16;
    private char NC = (char) 0;
    private byte[] buf = new byte[BS];
    private int bId = 0, size = 0;
    private char c = NC;
    private double cnt = 1;
    private BufferedInputStream in;

    public FastScanner() {
        in = new BufferedInputStream(System.in, BS);
    }

    public FastScanner(String s) {
        try {
            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
        } catch (Exception e) {
            in = new BufferedInputStream(System.in, BS);
        }
    }

    private char getChar() {
        while (bId == size) {
            try {
                size = in.read(buf);
            } catch (Exception e) {
                return NC;
            }
            if (size == -1) return NC;
            bId = 0;
        }
        return (char) buf[bId++];
    }

    public int nextInt() {
        return (int) nextLong();
    }

    public int[] nextInts(int N) {
        int[] res = new int[N];
        for (int i = 0; i < N; i++) {
            res[i] = (int) nextLong();
        }
        return res;
    }

    public long[] nextLongs(int N) {
        long[] res = new long[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextLong();
        }
        return res;
    }

    public long nextLong() {
        cnt = 1;
        boolean neg = false;
        if (c == NC) c = getChar();
        for (; (c < '0' || c > '9'); c = getChar()) {
            if (c == '-') neg = true;
        }
        long res = 0;
        for (; c >= '0' && c <= '9'; c = getChar()) {
            res = (res << 3) + (res << 1) + c - '0';
            cnt *= 10;
        }
        return neg ? -res : res;
    }

    public double nextDouble() {
        double cur = nextLong();
        return c != '.' ? cur : cur + nextLong() / cnt;
    }

    public double[] nextDoubles(int N) {
        double[] res = new double[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextDouble();
        }
        return res;
    }

    public String next() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c > 32) {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public String nextLine() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c != '\n') {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public boolean hasNext() {
        if (c > 32) return true;
        while (true) {
            c = getChar();
            if (c == NC) return false;
            else if (c > 32) return true;
        }
    }
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To create a test input that exposes the bug in the provided buggy code, we need to focus on edge cases and scenarios that are likely to cause issues. The buggy code has a few potential areas of concern:

1. **Identity Permutation Handling**: The code checks if the permutation is already sorted (identity permutation) and immediately prints the result. This is correct, but we need to ensure it handles other cases properly.
2. **Cycle Detection and Validation**: The code checks for cycles and their properties, but it might not handle all cycle structures correctly.
3. **Range Calculation**: The code calculates the number of valid pairs \((l, r)\) based on the number of elements that need to be fixed. This calculation might have issues with edge cases.

Let's consider a permutation that has a specific structure to test these aspects:

### Test Case: Permutation with a Cycle
A permutation that has a cycle where the elements are not in their correct positions but can be sorted with a specific range of \((l, r)\) values.

### Example Input:
```plaintext
1
4
2 3 4 1
```

### Explanation:
- **Permutation**: `[2, 3, 4, 1]`
- **Cycle**: The permutation has a cycle: `1 -> 2 -> 3 -> 4 -> 1`.
- **Valid Pairs**: The pairs \((l, r)\) that allow sorting this permutation are those that cover the sum of the elements in the cycle. Specifically, we need to be able to swap elements such that the sum of the elements being swapped is within the range \([l, r]\).

### Expected Output:
```plaintext
23
```

### Reasoning:
- The permutation can be sorted by swapping elements in the cycle. The valid pairs \((l, r)\) are those that cover the sums of the elements in the cycle. For example, the sums of pairs are:
  - \(2 + 3 = 5\)
  - \(3 + 4 = 7\)
  - \(4 + 1 = 5\)
  - \(1 + 2 = 3\)

- The valid pairs \((l, r)\) are those that cover these sums. The pairs are:
  - \((3, 7)\), \((4, 7)\), \((5, 7)\), \((6, 7)\), \((7, 7)\)
  - \((3, 6)\), \((4, 6)\), \((5, 6)\), \((6, 6)\)
  - \((3, 5)\), \((4, 5)\), \((5, 5)\)
  - \((3, 4)\), \((4, 4)\)
  - \((3, 3)\)

- The total number of valid pairs is 23.

This test case should expose any issues in the buggy code related to cycle detection and valid range calculation.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import static java.lang.Math.*;
import java.util.*;
import java.io.*;

public class x1973
{
    static final int INF = Integer.MAX_VALUE/2;
    public static void main(String[] args) throws Exception
    {
        FastScanner infile = new FastScanner();
        int T = infile.nextInt();
        while(T-->0)
        {
            int N = infile.nextInt();
            int[] p = infile.nextInts(N);
            for(int i=0; i < N; i++)
                p[i]--;
            boolean identity = true;
            for(int i=0; i < N; i++)
                identity &= p[i] == i;
            if(identity)
            {
                System.out.println((long)N*(2*N+1));
                continue;
            }
            long res = 1;
            int val = -1;
            for(int a=0; a < N; a++)
            {
                if(a != p[p[a]])
                    res = 0;
                else if(a != p[a])
                {
                    if(val == -1)
                        val = abs(a-p[a]);
                    else if(val != abs(a-p[a]))
                        res = 0;
                }
            }
            int[] pref = new int[N];
            if(p[0] != 0)
                pref[0]++;
            for(int i=1; i < N; i++)
                pref[i] = pref[i-1] + (p[i] != i ? 1 : 0);
            int needFix = pref[N-1];
            for(int R=2; R <= 2*N; R++)
            {
                int upper = min(N, R-1);
                if(pref[upper-1] != needFix)
                    continue;
                int l = 1;
                int h = R-1;
                while(l != h)
                {
                    int mid = (l+h+1)>>1;
                    int covered = pref[upper-1];
                    int lower = max(1, mid-N);
                    if(lower >= 2)
                        covered -= pref[lower-2];
                    if(covered == needFix)
                        l = mid;
                    else
                        h = mid-1;
                }
                //System.out.println(R+": "+l);
                res += l;
            }
            System.out.println(res);
        }
    }
}
/*
E:
L = N, R = N+1

boolean[] seen = new boolean[N];
            for(int a=0; a < N; a++)
                if(!seen[a])
                {
                    seen[a] = true;
                    int curr = p[a];
                    ArrayList<Integer> path = new ArrayList<>();
                    path.add(a);
                    while(curr != a)
                    {
                        path.add(curr);
                        seen[curr] = true;
                        curr = p[curr];
                    }
                    Collections.sort(path);
                    if(path.size() >= 2)
                    {
                        int diff = path.get(1)-path.get(0);
                        for(int i=2; i < path.size(); i++)
                            if(path.get(i)-path.get(i-1) != diff)
                                res = 0;
                    }
                }
 */
class FastScanner
{
    private int BS = 1 << 16;
    private char NC = (char) 0;
    private byte[] buf = new byte[BS];
    private int bId = 0, size = 0;
    private char c = NC;
    private double cnt = 1;
    private BufferedInputStream in;

    public FastScanner() {
        in = new BufferedInputStream(System.in, BS);
    }

    public FastScanner(String s) {
        try {
            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
        } catch (Exception e) {
            in = new BufferedInputStream(System.in, BS);
        }
    }

    private char getChar() {
        while (bId == size) {
            try {
                size = in.read(buf);
            } catch (Exception e) {
                return NC;
            }
            if (size == -1) return NC;
            bId = 0;
        }
        return (char) buf[bId++];
    }

    public int nextInt() {
        return (int) nextLong();
    }

    public int[] nextInts(int N) {
        int[] res = new int[N];
        for (int i = 0; i < N; i++) {
            res[i] = (int) nextLong();
        }
        return res;
    }

    public long[] nextLongs(int N) {
        long[] res = new long[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextLong();
        }
        return res;
    }

    public long nextLong() {
        cnt = 1;
        boolean neg = false;
        if (c == NC) c = getChar();
        for (; (c < '0' || c > '9'); c = getChar()) {
            if (c == '-') neg = true;
        }
        long res = 0;
        for (; c >= '0' && c <= '9'; c = getChar()) {
            res = (res << 3) + (res << 1) + c - '0';
            cnt *= 10;
        }
        return neg ? -res : res;
    }

    public double nextDouble() {
        double cur = nextLong();
        return c != '.' ? cur : cur + nextLong() / cnt;
    }

    public double[] nextDoubles(int N) {
        double[] res = new double[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextDouble();
        }
        return res;
    }

    public String next() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c > 32) {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public String nextLine() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c != '\n') {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public boolean hasNext() {
        if (c > 32) return true;
        while (true) {
            c = getChar();
            if (c == NC) return false;
            else if (c > 32) return true;
        }
    }
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To create a test input that exposes the bug in the provided code, we need to focus on edge cases or scenarios that might cause the code to fail. The code seems to handle certain conditions, but we need to find a permutation that might break the logic.

### Step-by-Step Analysis:
1. **Identity Permutation:**
   - The code correctly handles the identity permutation (i.e., already sorted array) by outputting the number of valid pairs directly. We don't need to test this as it is straightforward.

2. **Single Cycle Permutations:**
   - The code checks if the permutation can be sorted by cycles and ensures that the differences between elements in the cycle are consistent. We need to test permutations with cycles that might not meet these conditions.

3. **Multiple Cycles:**
   - The code might fail if there are multiple cycles with different properties. We need to test permutations with multiple cycles.

4. **Edge Cases:**
   - Test with small values of `n` (e.g., 1, 2, 3) to ensure the code handles them correctly.
   - Test with large values of `n` (e.g., 100,000) to ensure the code doesn't fail due to performance issues.

### Test Input:
Let's create a permutation with multiple cycles and different properties to see if the code can handle it correctly.

```plaintext
1
6
4 5 6 1 2 3
```

### Explanation:
- **Permutation:** `[4, 5, 6, 1, 2, 3]`
- **Cycles:**
  - One cycle: `(4, 1, 2, 5, 3, 6)`
- **Expected Output:**
  - The permutation can be sorted if we can swap elements such that the sum of the values is within the range `[1, 12]`.
  - The valid pairs `(l, r)` are:
    - `(1, 12), (2, 12), (3, 12), (4, 12), (5, 12), (6, 12), (7, 12), (8, 12), (9, 12), (10, 12), (11, 12)`
  - The total number of valid pairs is 11.

This test input should expose any issues in the code's handling of permutations with multiple cycles and different properties.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <bits/stdc++.h>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)
#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32)(x.size())
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;
using pi = pair<i32, i32>;
using pl = pair<i64, i64>;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;
template <typename T>
using VVVV = V<V<V<V<T>>>>;
template <typename T>
using PQR = priority_queue<T, V<T>, greater<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
i32 lob(const V<T> &arr, const T &v) {
    return (i32)(lower_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
i32 upb(const V<T> &arr, const T &v) {
    return (i32)(upper_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
V<i32> argsort(const V<T> &arr) {
    V<i32> ret(arr.size());
    iota(ALL(ret), 0);
    sort(ALL(ret), [&](i32 i, i32 j) -> bool {
        if (arr[i] == arr[j]) {
            return i < j;
        } else {
            return arr[i] < arr[j];
        }
    });
    return ret;
}
#ifdef INT128
using u128 = __uint128_t;
using i128 = __int128_t;
#endif
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
void scan(char &x) { cin >> x; }
void scan(u32 &x) { cin >> x; }
void scan(u64 &x) { cin >> x; }
void scan(i32 &x) { cin >> x; }
void scan(i64 &x) { cin >> x; }
void scan(string &x) { cin >> x; }
template <typename T>
void scan(V<T> &x) {
    for (T &ele : x) {
        scan(ele);
    }
}
void read() {}
template <typename Head, typename... Tail>
void read(Head &head, Tail &...tail) {
    scan(head);
    read(tail...);
}
#define CHAR(...)     \
    char __VA_ARGS__; \
    read(__VA_ARGS__);
#define U32(...)     \
    u32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define U64(...)     \
    u64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I32(...)     \
    i32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I64(...)     \
    i64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define STR(...)        \
    string __VA_ARGS__; \
    read(__VA_ARGS__);
#define VEC(type, name, size) \
    V<type> name(size);       \
    read(name);
#define VVEC(type, name, size1, size2)    \
    VV<type> name(size1, V<type>(size2)); \
    read(name);
// ============

#ifdef DEBUGF
#else
#define DBG(...) (void)0
#endif

void solve() {
    I32(n);
    VEC(i32, a, n);
    REP(i, n) {
        --a[i];
    }
    // -1 <= l <= r <= 2 * n - 2
    i32 back_match = 0;
    while (back_match < n && a[n - 1 - back_match] == n - 1 - back_match) {
        ++back_match;
    }
    i32 front_match = 0;
    while (front_match < n && a[front_match] == front_match) {
        ++front_match;
    }
    if (front_match == n) {
        cout << (i64)n * (2 * n + 1) << '\n';
        return;
    }
    V<pi> prs;
    REP(i, n) {
        if (a[i] > i && a[a[i]] == i) {
            prs.emplace_back(i, a[i]);
        }
    }
    bool all_same = !prs.empty();
    i32 tgt = (prs.empty() ? -1 : prs[0].first + prs[0].second);
    for (auto [p, q] : prs) {
        if (p + q != tgt) {
            all_same = false;
        }
    }
    DBG(front_match, back_match, all_same, tgt);
    i64 ans = 0;
    REP(r, 1, n - 1) {
        if (n - r - 1 <= back_match) {
            if (all_same && tgt == r) {
                ans += r + 2;
            } else {
                ans += r + 1;
            }
        }
    }
    REP(l, n, 2 * n - 2) {
        if (l - n + 1 <= front_match) {
            if (all_same && tgt == l) {
                ans += 2 * n - 1 - l;
            } else {
                ans += 2 * n - 2 - l;
            }
        }
    }
    if (all_same && tgt == n - 1) {
        ans += (i64)(n + 1) * n;
    } else {
        ans += (i64)(n + 1) * n - 1;
    }
    cout << ans << '\n';
}

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

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided buggy code, we need to focus on edge cases and scenarios that can maximize the failure probability. Here are the steps to think through this:

1. **Understand the Bug**: The buggy code seems to have issues with handling specific edge cases, such as when the permutation is already sorted, when there are specific pairs that need to be swapped, and when the constraints on `l` and `r` are very tight.

2. **Identify Edge Cases**:
   - **Already Sorted Permutation**: The code might not handle this case correctly.
   - **Single Pair Swaps**: If the permutation can be sorted by a single pair swap, the code might not count all valid pairs.
   - **Multiple Pair Swaps**: If the permutation requires multiple pairs to be swapped, the code might miss some valid pairs.
   - **Edge Values for `l` and `r`**: The code might not handle the edge values of `l` and `r` correctly.

3. **Generate Test Input**:
   - **Already Sorted Permutation**: This is a simple case where the permutation is already sorted.
   - **Single Pair Swap**: Create a permutation that can be sorted by a single pair swap.
   - **Multiple Pair Swaps**: Create a permutation that requires multiple pairs to be swapped.
   - **Edge Values for `l` and `r`**: Create a permutation that tests the edge values of `l` and `r`.

Given the constraints and the need for a small-scale test input, let's create a test case that combines these edge cases:

```plaintext
4
1
1
2
2 1
3
3 1 2
4
4 3 2 1
```

### Explanation:
1. **Test Case 1**:
   - `n = 1`
   - `p = [1]`
   - This is the simplest case where the permutation is already sorted. The number of valid pairs `(l, r)` should be 1.

2. **Test Case 2**:
   - `n = 2`
   - `p = [2, 1]`
   - This case requires a single pair swap to sort the permutation. The number of valid pairs `(l, r)` should be 6.

3. **Test Case 3**:
   - `n = 3`
   - `p = [3, 1, 2]`
   - This case requires multiple pair swaps to sort the permutation. The number of valid pairs `(l, r)` should be 11.

4. **Test Case 4**:
   - `n = 4`
   - `p = [4, 3, 2, 1]`
   - This case requires multiple pair swaps to sort the permutation. The number of valid pairs `(l, r)` should be 23.

This test input should help expose any bugs in the provided code by covering a variety of scenarios, including already sorted permutations, single pair swaps, and multiple pair swaps.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 100005;

int n, a[N];

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int lef = n + 1, rig = 0, cnt = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (a[i] != i)
        {
            lef = min(lef, i);
            lef = min(lef, a[i]);
            rig = max(rig, a[i]);
            rig = max(rig, i);
            cnt++;
        }
    }
    if (rig == 0)
    {
        cout << (2 * n + 1) * n << '\n';
        return;
    }
    ll ans = cnt == 2;
    for (int i = 1; i <= lef + n; ++i)
    {
        ans += 2 * n + 1 - max(rig + 1, i + 1);
    }
    cout << ans << '\n';
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
1
1
2
2 1
4
4 3 2 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import sys
import heapq
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
for __ in range(fg()):
    n = fg()
    p = fgh()
    if p == sorted(p):
        print(2 * n * (2 * n + 1) // 2)
    else:
        c = 0
        while p[c] == c + 1:
            c += 1
        d = 0
        while p[n - d - 1] == n - d:
            d += 1
        L = c + 1
        R = n - d
        ans = 0
        fl = 1
        for i in range(L, R + 1):
            if p[i - 1] != R - (i - L) or p[R - (i - L) - 1] != i:
                fl = 0
        for l in range(1, 2 * n + 1):
            minr = R + 1
            k = l - L
            if l <= L + n:
                if fl and l == R + L:
                    ans += max(0, 2 * n - max(minr, l) + 1)
                else:
                    ans += max(0, 2 * n - max(minr, l + 1) + 1)
        print(ans)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
1
1
2
2 1
4
4 3 2 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import sys
import os
from io import BytesIO, IOBase
from _collections import defaultdict

# region fastio
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

if sys.version_info[0] < 3:
    sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

file = sys.stdin
if os.environ.get('USER') == "loic":
    file = open("data.in")

line = lambda: file.readline().split()
ui = lambda: int(line()[0])
mi = lambda: map(int,line())
li = lambda: list(mi())
lsi = lambda: list(map(int,line()[0]))
#######################################################################

def solve():

    mn = 10**9
    mx = 0
    mp = defaultdict(int)
    for i,val in enumerate(A,1):
        if val != i:
            mn = min(mn,val)
            mx = max(mx,val)
            if A[val-1] == i:
                mp[val + i] += 1

    if mn > mx:
        return str(2 * N * (2 * N + 1) // 2)

    res = mx * (2 * N - mx)
    res += (2 * N - mx - 1) * (2 * N - mx) // 2
    rem = 2 * N - (mn + N + 1)
    res -= rem * (rem + 1) // 2
    if len(mp) == 1:
        res += 1

    return str(res)

for test in range(1,ui()+1):
    N = ui()
    A = li()

    if test == 61:
        print("_".join(str(v) for v in A))

    print(solve())

file.close()

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To expose the bug in the provided buggy code, we need to focus on edge cases or scenarios that are likely to cause issues. Here are the steps to think through this:

1. **Identify Potential Issues:**
   - The code uses a dictionary `mp` to track pairs of values that can be swapped.
   - It calculates the minimum and maximum values that are out of place.
   - It computes the number of valid pairs `(l, r)` based on these values.

2. **Edge Cases:**
   - **Already Sorted Array:** If the array is already sorted, the code should return the maximum possible number of pairs.
   - **Single Element Array:** An array with a single element should return the correct number of pairs.
   - **All Elements Out of Place:** An array where all elements are out of place should be handled correctly.
   - **Specific Patterns:** Arrays with specific patterns that might cause issues in the minimum and maximum calculations.

3. **Generate Test Input:**
   - **Already Sorted Array:**
     - Input: `1\n5\n1 2 3 4 5`
   - **Single Element Array:**
     - Input: `1\n1\n1`
   - **All Elements Out of Place:**
     - Input: `1\n5\n5 4 3 2 1`
   - **Specific Pattern:**
     - Input: `1\n4\n3 2 1 4`

4. **Combining Test Cases:**
   - We will combine these test cases into a single test input to ensure the code handles all scenarios.

Here is the test input:

```plaintext
4
1
1
5
1 2 3 4 5
5
5 4 3 2 1
4
3 2 1 4
```

This test input includes:
- A single element array.
- An already sorted array.
- An array where all elements are out of place.
- An array with a specific pattern.

These cases should help expose any bugs in the code.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

void solve(){
    int n;
    cin >> n;
    vector<int> q(n + 1);
    int mi = 1e9, ma = 0;
    for (int i = 1; i <= n; i++){
        cin >> q[i];
        if (q[i] != i) mi = min(mi, q[i]), ma = max(ma, q[i]);
    }
    if (ma < mi){
        cout << 1ll * (n * 2) * (n * 2 + 1) / 2 << endl;
        return;
    }
    int op = mi + ma;
    ll ans = 0;
    if (ma - mi == 1){
        ans = 1ll * op * (n * 2 - op + 1);
        for (int i = 1; i < op; i++){
            if (i - ma >= 1){
                ans += 1ll * (i - 1);
            }
        }
        for (int i = op + 2; i <= n * 2; i++){
            if (i - mi <= n + 1){
                ans += 1ll * (n * 2 - i + 1);
            }
        }
    }
    else {
        ans = 1ll * (op - 1) * (n * 2 - op + 1);
        for (int i = 1; i < op; i++){
            if (i - ma >= 1){
                ans += 1ll * (i - 1);
            }
        }
        for (int i = op + 1; i <= n * 2; i++){
            if (i - mi <= n + 1){
                ans += 1ll * (n * 2 - i + 1);
            }
        }
        int flag = 1;
        for (int i = 1; i <= n; i++){
            if (q[i] != i && q[i] != mi && q[i] != ma) flag = 0;
        }
        ans += flag;
    }
    cout << ans << endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    solve();
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To create a challenging test input that exposes the bug in the provided buggy code, let's consider the following:

1. **Edge Cases**:
   - The array is already sorted.
   - The array is a reverse sorted permutation.
   - The array has a specific pattern that could cause the bug to be evident.

2. **Specific Pattern**:
   - The array has a small number of elements that are out of place, and these elements are not adjacent in the sorted order.

3. **Small-Scale Test Input**:
   - We need a test case that is small enough to be easily validated but large enough to expose the bug.

Let's create a test input where the permutation has a few elements out of place, and these elements are not adjacent in the sorted order. This should help in exposing any issues in the logic for calculating the number of valid pairs.

Here is the test input:

```plaintext
1
5
3 1 5 2 4
```

Explanation:
- The permutation is `[3, 1, 5, 2, 4]`.
- The elements `3` and `1` are out of place, and so are `5` and `2`.
- These elements are not adjacent in the sorted order, which should help in exposing any issues in the logic for calculating the valid pairs.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Cat, Fox and Swaps
time_limit_ms: 2000
memory_limit_mb: 256
Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Examples:
input:
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output:
6
11
23
29
55
46
58

Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

Buggy Code:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int t = scanner.nextInt();
        while (t-- > 0) {
            int n = scanner.nextInt();
            int[] p = new int[n];
            for (int i = 0; i < n; i++) {
                p[i] = scanner.nextInt();
            }

            System.out.println(countValidPairs(n, p));
        }

        scanner.close();
    }

    private static int countValidPairs(int n, int[] p) {
        int totalPairs = 0;

        for (int l = 1; l <= 2 * n; l++) {
            for (int r = l; r <= 2 * n; r++) {
                if (canBeSorted(l, r, p)) {
                    totalPairs++;
                }
            }
        }

        return totalPairs;
    }

    private static boolean canBeSorted(int l, int r, int[] p) {
        int n = p.length;
        UnionFind uf = new UnionFind(n + 1);

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int x = p[i];
                int y = p[j];
                if (l <= x + y && x + y <= r) {
                    uf.union(x, y);
                }
            }
        }

        for (int i = 1; i <= n; i++) {
            if (uf.find(i) != uf.find(p[i - 1])) {
                return false;
            }
        }

        return true;
    }
}

class UnionFind {
    private int[] parent;

    public UnionFind(int size) {
        parent = new int[size];
        for (int i = 0; i < size; i++) {
            parent[i] = i;
        }
    }

    public int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    public void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            parent[rootX] = rootY;
        }
    }
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
1
1
2
2 1
4
4 3 2 1
```