You are an expert at testing code. You will be given a buggy code and the complete description of the problem it intends to solve. You have to find a test-case where the code fails. You can do this by writing a randomised test case generator, along with a correct (possibly slow, brute force) solution. I will then repeatedly compare the buggy code against the correct solution on the generator's outputs until a failing test case is found. This checking cycle runs for upto 1 minute. Keep this in mind so that your generator doesn't print test-cases that your correct solution takes too long to run on.

Provide the correct solution and the test-case generator to me in the exact XML format I show below. Do not include anything else in your responses. Your code must be written in Python 3 or C++ 23.

```
<action>
<name>solve_brute</name>
<code>
[correct code to solve the problem]
</code>
<lang>[Python 3 | C++ 23]</lang>
</action>

<action>
<name>generate_tc</name>
<code>
[code to generate random test-cases]
</code>
<lang>[Python 3 | C++ 23]</lang>
</action>
```

# Example 1

## Statement

An array $$b$$ of length $$m$$ is good if for all $$i$$ the $$i$$-th element is greater than or equal to $$i$$. In other words, $$b$$ is good if and only if $$b_i \geq i$$ for all $$i$$ ($$1 \leq i \leq m$$). You are given an array $$a$$ consisting of $$n$$ positive integers. Find the number of pairs of indices $$(l, r)$$, where $$1 \le l \le r \le n$$, such that the array $$[a_l, a_{l+1}, \ldots, a_r]$$ is good .

Time Limit: 1000ms

Memory Limit: 256 megabytes

## Input Format

Each test contains multiple test cases. The first line contains the number of test cases $$t$$ ($$1 \leq t \leq 2 \cdot 10^5$$). Description of the test cases follows.
The first line of each test case contains an integer $$n$$ ($$1 \leq n \leq 2 \cdot 10^5$$), the length of the array $$a$$.
The second line of each test case contains $$n$$ space-separated integers $$a_1,a_2,\ldots,a_n$$ ($$1 \leq a_i \leq n$$), representing the array $$a$$.
It is guaranteed that the sum of $$n$$ over all test cases does not exceed $$2 \cdot 10^5$$.

## Output Format

For each test case, print the number of suitable pairs of indices.

## Example Input

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

```

## Example Output

```
6
3
7
```

## Note

In the first test case, all subarrays of $$a$$ are good , so all pairs are suitable. In the second test case, the pairs $$(1, 1)$$, $$(2, 2)$$, and $$(3, 3)$$ are suitable. For example, when $$(l, r) = (1, 2)$$, the array $$b=[1,1]$$ is not good because $$b_2 < 2$$.

## Code

```
#include <iostream>

typedef unsigned int uint;

using namespace std;

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

  uint t;
  cin >> t;

  for (uint test_case = 0; test_case < t; test_case++) {
    uint n;
    cin >> n;

    uint total = 0;
    uint start = 1;

    for (uint i = start; i <= n; i++) {
      uint a;
      cin >> a;

      uint l = i - start;

      if (a < l + 1) {
        // cout << "total += " << (l * (l + 1)) / 2 << endl;
        total += (l * (l + 1)) / 2;

        start = i;
      }
    }

    uint l = n - start + 1;
    // cout << "Final l = " << l << endl;
    total += (l * (l + 1)) / 2;

    cout << total << endl;
  }
}

```

## You would respond with:

```
<action>
<name>solve_brute</name>
<code>
for _ in range(int(input())):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(n):
        for j in range(i, n):
            if a[j] < j - i + 1:
                break
            ans += 1
    print(ans)
</code>
<lang>Python 3</lang>
</action>

<action>
<name>generate_tc</name>
<code>
import random

t = 1
print(t)
for _ in range(t):
    n = random.randint(1, 500)
    print(n)
    print(' '.join(str(random.randint(1, n)) for _ in range(n)))
</code>
<lang>Python 3</lang>
</action>
```

# Example 2

## Statement

You have an array $$a$$ consisting of $$n$$ positive integers and you have to handle $$q$$ queries of the following types:
$$1$$ $$i$$ $$x$$: change $$a_{i}$$ to $$x$$,
$$2$$ $$l$$ $$r$$ $$k$$: check if the number of occurrences of every positive integer in the subarray $$a_{l}, a_{l+1}, \ldots a_{r}$$ is a multiple of $$k$$ (check the example for better understanding).

Time Limit: 3000ms

Memory Limit: 256 megabytes

## Input Format

The first line of the input contains two integers $$n$$ and $$q$$ ($$1 \le n , q \le 3 \cdot 10^5$$), the length of $$a$$ and the number of queries.
Next line contains $$n$$ integers $$a_{1}, a_{2}, \ldots a_{n}$$ ($$1 \le a_{i} \le 10^9$$) — the elements of $$a$$.
Each of the next $$q$$ lines describes a query. It has one of the following forms.
$$1$$ $$i$$ $$x$$, ($$1 \le i \le n$$ , $$1 \le x \le 10^9$$), or
$$2$$ $$l$$ $$r$$ $$k$$, ($$1 \le l \le r \le n$$ , $$1 \le k \le n$$).

## Output Format

For each query of the second type, if answer of the query is yes, print " YES ", otherwise print " NO ".

## Example Input

```
10 8
1234 2 3 3 2 1 1 2 3 4
2 1 6 2
1 1 1
2 1 6 2
2 1 9 2
1 10 5
2 1 9 3
1 3 5
2 3 10 2
```

## Example Output

```
NO
YES
NO
YES
YES
```

## Note

In the first query, requested subarray is $$[1234, 2, 3, 3, 2, 1]$$, and it's obvious that the number of occurrence of $$1$$ isn't divisible by $$k = 2$$. So the answer is " NO ". In the third query, requested subarray is $$[1, 2, 3, 3, 2, 1]$$, and it can be seen that the number of occurrence of every integer in this sub array is divisible by $$k = 2$$. So the answer is " YES ". In the sixth query, requested subarray is $$[1, 2, 3, 3, 2, 1, 1, 2, 3]$$, and it can be seen that the number of occurrence of every integer in this sub array is divisible by $$k = 3$$. So the answer is " YES ".

## Code

```
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,Q,q[300003][5],a[300003],ans[300003],totq,B=70;
set<int>R;
int stk[600003],tots;
struct BIT{
	int TreeAr[300003];
	int lowbit(int X){return (X&(-X));}
	void modify(int wz,int val){
		for(int i=wz;i<=n;i+=lowbit(i))TreeAr[i]+=val;
		return;
	}
	int Query(int l,int r){
		int ret=0;
		for(int i=r;i;i-=lowbit(i))ret+=TreeAr[i];
		for(int i=l-1;i>0;i-=lowbit(i))ret-=TreeAr[i];
		return ret;
	}
};
struct rndseq{
	char val[600003];
	BIT T;
}C[73];
int fff(int X){return (int)(lower_bound(stk+1,stk+tots+1,X)-stk);}
void m0dify(int wz,int v,int val){
	for(int i=1;i<=B;i++)C[i].T.modify(wz,val*C[i].val[v]);
	return;
}
ll seed=257825;
int rnd(){
	seed=(seed*4132ll+1ll*seed*seed+98521)%998244353;
	seed=(seed<<13)^seed^(seed>>5);
	seed%=998244353;
	return seed;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>Q;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)R.insert(a[i]);
	for(int i=1;i<=Q;i++){
		cin>>q[i][0]>>q[i][1]>>q[i][2];
		if(q[i][0]==1)R.insert(q[i][2]);
		else{
			cin>>q[i][3];
			ans[++totq]=1;
			q[i][4]=totq;
		}
	}
	for(auto i:R)stk[++tots]=i;
	for(int i=1;i<=n;i++)a[i]=fff(a[i]);
	for(int i=1;i<=n;i++)if(q[i][0]==1)q[i][2]=fff(q[i][2]);
	for(int i=1;i<=B;i++){
		for(int j=1;j<=tots;j++)C[i].val[j]=rnd()%2;
	}
	for(int i=1;i<=n;i++)m0dify(i,a[i],1);
	for(int i=1;i<=Q;i++){
		if(q[i][0]==1){
			m0dify(q[i][1],a[q[i][1]],-1);
			a[q[i][1]]=q[i][2];
			m0dify(q[i][1],a[q[i][1]],1);
		}
		else{
			for(int j=1;j<=B;j++)if(C[j].T.Query(q[i][1],q[i][2])%q[i][3]!=0)ans[q[i][4]]=0;
		}
	}
	for(int i=1;i<=totq;i++){
		if(ans[i])cout<<"YES\n";
		else cout<<"NO\n";
	}
	return 0;
}
```

## You would respond with:

```
<action>
<name>solve_brute</name>
<code>
n, q = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

for _ in range(q):
    qr = [int(i) for i in input().split()]
    if qr[0] == 1:
        i, x = qr[1], qr[2]
        i -= 1
        a[i] = x
    else:
        l, r, k = qr[1], qr[2], qr[3]
        l -= 1
        r -= 1
        freq = {}
        for i in range(l, r + 1):
            if a[i] in freq:
                freq[a[i]] += 1
            else:
                freq[a[i]] = 1

        valid = True
        for key, value in freq.items():
            if value % k != 0:
                valid = False
                break

        print("YES" if valid else "NO")
</code>
<lang>Python 3</lang>
</action>

<action>
<name>generate_tc</name>
<code>
import random
MXN, MXQ, MXV = 500, 500, int(1e9)

n, q = random.randint(1, MXN), random.randint(MXQ, MXQ)
print(n, q)
print(' '.join(str(random.randint(1, n)) for _ in range(n)))
for _ in range(q):
    if random.randint(0, 1):
        i, x = random.randint(1, n), random.randint(1, n)
        print(1, i, x)
    else:
        l, r, k = random.randint(1, n), random.randint(1, n), random.randint(1, n)
        print(2, min(l, r), max(l, r), k)
</code>
<lang>Python 3</lang>
</action>
```

# Example 3

## Statement

You are given an integer $$x$$ and an array of integers $$a_1, a_2, \ldots, a_n$$. You have to determine if the number $$a_1! + a_2! + \ldots + a_n!$$ is divisible by $$x!$$.
Here $$k!$$ is a factorial of $$k$$ — the product of all positive integers less than or equal to $$k$$. For example, $$3! = 1 \cdot 2 \cdot 3 = 6$$, and $$5! = 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120$$.

Time Limit: 2000ms

Memory Limit: 256 megabytes

## Input Format

The first line contains two integers $$n$$ and $$x$$ ($$1 \le n \le 500\,000$$, $$1 \le x \le 500\,000$$).
The second line contains $$n$$ integers $$a_1, a_2, \ldots, a_n$$ ($$1 \le a_i \le x$$) — elements of given array.

## Output Format

In the only line print "
Yes
" (without quotes) if $$a_1! + a_2! + \ldots + a_n!$$ is divisible by $$x!$$, and "
No
" (without quotes) otherwise.

## Example Input

```
6 4
3 2 2 2 3 3
```

## Example Output

```
Yes
```

## Note

In the first example $$3! + 2! + 2! + 2! + 3! + 3! = 6 + 2 + 2 + 2 + 6 + 6 = 24$$. Number $$24$$ is divisible by $$4! = 24$$.
In the second example $$3! + 2! + 2! + 2! + 2! + 2! + 1! + 1! = 18$$, is divisible by $$3! = 6$$.
In the third example $$7! + 7! + 7! + 7! + 7! + 7! + 7! = 7 \cdot 7!$$. It is easy to prove that this number is not divisible by $$8!$$.

## Code

```
def ntor(n, r):
    m = n
    i = 1
    while n - i > r:
        m *= n - i
        i += 1
    return m

n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
count = 1
ans = True

for i in range(1, n):
    if a[i] == a[i - 1]:
        count += 1
    else:
        m = ntor(a[i], a[i - 1])
        # print(i,m,count)
        if count % m == 0:
            count = (count // m) + 1
        else:
            ans = False
            break
if ans:
    if count % ntor(x,a[-1]) != 0 : ans = False

if ans == True:
    print('Yes')
else:
    print('No')
```

## You would respond with:

```
<action>
<name>solve_brute</name>
<code>
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
x_fac = 1
for i in range(2, x + 1):
    x_fac *= i

s = 0
for i in a:
    rem = 1
    for j in range(1, i + 1):
        rem = (rem * j) % x_fac

    s = (s + rem) % x_fac

print("Yes" if s == 0 else "No")
</code>
<lang>Python 3</lang>
</action>

<action>
<name>generate_tc</name>
<code>
import random
MXN, MXV = 500, 500

n, x = random.randint(1, MXN), random.randint(1, MXV)
print(n, x)
print(' '.join(str(random.randint(1, MXV)) for _ in range(n)))
</code>
<lang>Python 3</lang>
</action>
```