Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence .
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem: Given n, we need to find a permutation a1..an of numbers 1..n such that the sequence of prefix products modulo n (I assume?) Let's read: "Define its prefix product sequence ." The problem statement likely includes an image of something like: P_i = (a1 * a2 * ... * a_i) mod n? Or maybe just the prefix product, not mod? However, they want the prefix product sequence to be a permutation of [0, 1, ..., n-1]. So the prefix product values must be exactly each integer from 0 to n-1 exactly once. But note that prefix product without modulo can quickly become huge, exceeding n-1. So they must be taken modulo n. Also the example: n=7, output sequence: 1,4,3,6,5,2,7. Let's compute prefix products mod 7:
a1=1 => P1=1 mod7=1
a2=4 => P2=14=4 mod7=4
a3=3 => P3=43=12 mod7=5? Wait 12 mod7=5. But the requirement: prefix product sequence should be a permutation of 0..n-1 = 0..6. So far we have {1,4,5}. Continue:
a4=6 => P4=56=30 mod7=2 (since 28 mod7=0, 30 mod7=2). So P4=2.
a5=5 => P5=25=10 mod7=3. So P5=3.
a6=2 => P6=32=6 mod7=6. So P6=6.
a7=7 => P7=67=42 mod7=0. So P7=0.
Thus we have prefix products: [1,4,5,2,3,6,0] which is a permutation of 0..6. So indeed the definition is prefix product modulo n.
Thus we need to find a permutation of 1..n such that the sequence of prefix products (mod n) yields all residues 0..n-1 exactly once.
Observation: The prefix product sequence is defined as p_i = (a1 * ... * a_i) mod n. Since we want all residues from 0 to n-1, in particular, the last p_n must be 0 (since it's the only way to have 0 appear). Also note that p_n = product of all numbers 1..n mod n = n! mod n. For n>1, n! mod n = 0 because n is a factor. For n=1, 1! mod1 = 0? Actually modulo 1 everything is 0. But we need a permutation of [0]? For n=1, the set is [0]? Actually [0, ..., n-1] for n=1 is just [0]. So we need prefix product sequence to be [0]. Our permutation must be [1]. Check: a1=1, p1=1 mod1 =0? Because any number mod1 is 0. Yes p1=0, okay. So n=1 works.
Now for n>1, p_n must be 0. Also p_1 = a1 mod n. So a1 mod n must be some nonzero residue? Actually it could be 0 if a1 = n? But a1 is from 1..n, so a1 = n gives a1 mod n = 0. But if p1=0 then we already have 0 appearing at the first position, and then later we cannot have another 0 (since must be permutation), but p_n also must be 0? Actually we need all residues exactly once, so exactly one index i where p_i = 0. Could be p_n = 0 and p1 != 0. Could p1 be 0? Then p_n would be also 0 if n>1? But then 0 appears twice unless the product after some steps becomes 0 and stays 0? But modulo n, once we multiply by a number that is not coprime to n, we could get 0 and thereafter stay 0 if we multiply by numbers? Actually if p_k = 0 mod n, then for any subsequent multiplication, p_{k+1} = 0 * a_{k+1} mod n = 0. So it would remain 0. So if p1=0, then all subsequent p_i = 0, causing multiple zeros. So cannot have p1=0. Therefore a1 cannot be n. So a1 ≠ n. So a1 must be between 1 and n-1.
Similarly, p_{n-1} must be some nonzero residue that when multiplied by a_n yields 0 mod n. That implies that p_{n-1} * a_n ≡ 0 (mod n) and p_{n-1} ≠ 0. Since we multiply by a_n at the last step to get 0, we need that a_n is a multiple of some factor that combined with p_{n-1} yields a multiple of n. More precisely, for product of all numbers from 1 to n, it's n! which is divisible by n, so overall we get 0. But we want that the zero appears exactly at the last step, meaning that up to n-1, the product is not 0 mod n. So the cumulative product after n-1 steps must be not divisible by n. That product is (product of all numbers except a_n) mod n. So we need that product is not 0 mod n, and then multiplying by a_n gives 0 mod n.
Thus we need to choose a permutation such that the prefix products produce all residues exactly once.
This is reminiscent of constructing a sequence where a_i = p_i * (p_{i-1})^{-1} mod n? Actually given p_i, a_i = (p_i * inv(p_{i-1})) mod n, where inv exists only if p_{i-1} is invertible mod n. But p_{i-1} could be non-invertible if not coprime to n. However, we need to generate a permutation of numbers 1..n. This is like a Eulerian walk on the group of units? But note that some numbers are not coprime to n, so multiplication by them can cause zero divisors.
We need to find conditions on n for which such a permutation exists. For example, n=7 (prime) works. n=6: sample says NO. So prime? Check n=2: can we? n=2, permutation of [1,2]. Need prefix products mod2: [a1 mod2, a1a2 mod2] = permutation of [0,1]. Since a1 ∈ {1,2}. If a1=1 => p1=1; then a2 must be 2 => p2=12=2 mod2=0 => sequence [1,0] which is a permutation of [0,1]. So works. Output? But sample only for 6 and 7. Let's test n=3: numbers 1,2,3. Need prefix products mod3 to be [0,1,2] in some order, with last =0. Let's try to find permutation. a1 cannot be 3 else p1=0. So a1 ∈ {1,2}. Try a1=1 => p1=1. Need to eventually get residues 0,2. a2 could be 2 or 3. If a2=2: p2=2 mod3=2. Then a3=3 => p3=23=6 mod3=0 => sequence [1,2,0] which is a permutation of 0,1,2. So works. If a1=2 => p1=2. a2=1 => p2=2 mod3=2 (same as p1? Actually 21=2 mod3=2, not allowed because duplicates? So would get p2=2 which is duplicate; invalid). a2=3 => p2=23=6 mod3=0, duplicate of eventual 0? Actually p2=0, then p3 would be 0remaining=0, duplicate. So only a1=1 works. So n=3 works. n=4? Let's test manually maybe. n=4: numbers 1,2,3,4. Need prefix products mod4 to be [0,1,2,3] in some order. p_n=0 at last. Let's see if possible. We need product of all four numbers = 4! =24 mod4=0. So last step must produce 0. So p_{3} must be something not 0 that times a_4 gives 0 mod4. That implies that a_4 is even (since to get factor 2 twice? Actually mod4, to get zero, the product must be divisible by 4. Since p_3 is product of three numbers among 1,2,3,4 (excluding a4). p_3 might be 0 mod4? If any of the three numbers includes 2 and another even? Actually among 1,2,3,4, only 4 is multiple of 4, but we could have 2 and something else to get 0? 22=4, but we have only one 2? There is only one even 2 and one 4. So to get 0 mod4, need factor 4 or at least two factors 2. Since the only numbers that provide factor 2 are 2 (one 2) and 4 (2^2). So to get 0 mod4, p_3 must be even (i.e., contains factor 2) and a_4 must be even as well to get at least 2^2? Actually if p_3 has factor 2 (i.e., is even) and a_4 is even, then product has at least 2^2, which is divisible by 4 only if both are even? Let's analyze: 22=4 => divisible by 4, but if p_3 is 2 mod4 (i.e., exactly one factor 2) and a_4 is 2, product=4 => 0 mod4. If p_3 is 2 mod4 and a_4 is 4, product = 8 => 0 mod4. If p_3 is 0 mod4 already, then earlier zero. So we need p_3 to be exactly 2 mod4? Actually could be 2 or maybe 6 mod4 =2, etc. But the condition is that p_3 * a_4 ≡ 0 mod4. For p_3 odd (1 or 3 mod4), a_4 would need to be 0 mod4, i.e., a_4=4 gives 4odd=4 mod4=0. So that also works: if p_3 is odd, a_4=4 yields 0. So a_4 could be 4 regardless. So we can set a_4=4, and p_3 any odd or even? It must be nonzero mod4, and then 4anything ≡0 mod4. So that's fine. But we also need that p_1, p_2, p_3 are distinct and nonzero, and together with p_4=0 cover all residues 0,1,2,3.
Let's try to find a permutation. We'll try a_4=4. Then remaining numbers 1,2,3 for a1,a2,a3. We need p_3 = product of a1a2a3 mod4 to be some nonzero residue, and p_1 and p_2 also nonzero and distinct.
Compute possible orders of 1,2,3.
Case: a1=1 => p1=1.
Then a2 could be 2 or 3.
Subcase a2=2 => p2=12=2 mod4 => p2=2.
Then a3=3 => p3=23=6 mod4=2 => p3=2 duplicate with p2. Not allowed.
Subcase a2=3 => p2=13=3 mod4 => p3 = 32=6 mod4=2 => p3=2, distinct from p1=1 and p2=3. So p1=1, p2=3, p3=2. Then a4=4 => p4=2*4=8 mod4=0. So sequence [1,3,2,0] which is a permutation of 0,1,2,3. That works! So n=4 works.
Check n=5 (prime) likely works. n=6 sample says NO. n=8? Let's test maybe. We need to find general condition.
Let's analyze necessary conditions.
We need p_i to be a permutation of Z_n. This is a bijection from indices to residues. In particular, the mapping i -> p_i is a permutation. Also p_i = p_{i-1} * a_i mod n. So the sequence of residues is a walk in the multiplicative monoid (Z_n) that visits each residue exactly once, starting from p_1 = a_1 mod n, and ending at p_n = 0. Additionally, the a_i are a permutation of 1..n, meaning each integer 1..n appears exactly once.
This reminds me of constructing a sequence where a_i = (p_i * inv(p_{i-1})) mod n, but this only works if p_{i-1} is invertible mod n. When p_{i-1} is not invertible (i.e., shares a factor with n), then the "inverse" doesn't exist. However, we can still define a_i as the factor such that p_i = p_{i-1} * a_i mod n.
Since we require a_i to be exactly the numbers from 1 to n, we need to assign each integer to a position such that its multiplication yields the next residue.
Alternative viewpoint: The set of prefix products p_0=1? Actually we could define p_0=1 (empty product) and then a_i = p_i * (p_{i-1})^{-1} if invertible? But we could also think of the sequence of a_i as representing the differences between successive residues. Not exactly.
Maybe we can characterize the existence in terms of n's prime factorization. The sample: n=6 fails. 6=2*3. n=4=2^2 works. n=8=2^3? Might work? Let's test quickly with a programmatic mind? We can try to reason.
Let's try to construct for n=8 manually or see pattern.
We need a permutation of 1..8 such that prefix products mod8 are 0..7. p_n=0. p1 cannot be 0. So a1 !=8. But 8 is allowed later.
Observation: Since we need all residues, and p_n=0, we must have that at some step we encounter zero and then stay zero? Actually if we ever get zero before the last step, subsequent steps remain zero, causing duplicate zeros. So we must get zero exactly at the last step. That means for all i=1..n-1, p_i must be nonzero mod n. So the product of the first i numbers is not divisible by n. In particular, the product of the first n-1 numbers is not divisible by n, but the product of all n is divisible by n.
Thus the cumulative product after n-1 steps must be a number that is not a multiple of n, but when multiplied by the final number (which must be the only number among 1..n that provides the missing prime factors to make n) we get a multiple of n.
Let's denote P = product of all numbers from 1 to n mod n = 0. But note that product of all numbers from 1 to n is n! which is divisible by n, but often by higher powers. But we only care mod n.
For p_{n-1} to be non-zero mod n, the set of first n-1 numbers must not collectively contain all prime factors of n with sufficient multiplicities. However, the last number a_n must contain the missing factors so that total product is divisible by n.
Because n can have multiple prime factors, the condition might be that n is a prime power? Or maybe n is not 6? Let's test n=9 (3^2). Does it work? We can try to construct? Let's attempt to find a permutation for n=9 quickly.
We need numbers 1..9. The last should be such that p_{9}=0. We need p_{i} nonzero for i<9.
We can try to build like we did for 7 and 4. Perhaps we can design a generic construction for prime powers and maybe for n=2? But 6 fails, maybe because 6 is composite with two distinct primes? Let's test n=10: maybe fails? Or maybe some composites work? 4 works (2^2). 8 likely works? Let's test n=8 manually with an approach.
We need to assign a_1..a_8 such that p_i mod 8 are all distinct and cover 0..7.
Let's attempt constructive: Since we need zero at the end, we can set a_8 = 8 (since 8 mod8 =0? Actually 8 mod8=0, but if we set a_8=8, then p_7 must be something; p_8 = p_78 mod8 = 0. So that's okay. But careful: a_8=8, p_8 = 0 regardless of p_7. But p_7 must be nonzero and not 0 (obviously). So a_8 can be 8. Also could a_8 be some other number that yields zero? For zero mod8, we need the product to be even multiple of 8? Actually to get 0 mod8, we need factor 8. But 8 itself gives 0 when multiplied. But also, if we have factor 2 and 4? For instance, 24=8 mod8=0. So a_n could be 4 and p_{n-1}=2, or a_n=2 and p_{n-1}=4, etc. But it's simpler to set a_n = n if possible. Check if that always works? For n=6, a_n =6 would give p_56 mod6 = 0 regardless of p_5? Because 6 mod6=0? Actually 6 mod6 =0, so p_6 = p_50 mod6 =0. So if we set a_6=6, p_6=0. But we need p_5 not zero. However, is it possible? If we set a_6=6, then we need to have p_5 such that p_5 * 6 ≡ 0 mod6, which is always true because 6≡0 mod6? Wait careful: a_6 = 6, but when we compute p_6 = (p_5 * 6) mod 6. Since 6 mod6 = 0, indeed p_6 = 0 regardless of p_5. So a_n = n always yields p_n = 0. So we can set a_n = n. That seems plausible as part of a construction. But is it always allowed? For n=6, we could try a_6=6. Then we need to arrange a1..a5 such that p_1..p_5 are distinct nonzero residues mod6 and also p_i ≠ 0. Also p_5 must not be 0 mod6. That seems to be the challenge.
So the existence reduces to: can we order numbers 1..n-1 (except n) such that their prefix products mod n are all distinct and nonzero? Because then we can append n at the end to get p_n=0. Also we need the prefix products to be a permutation of 1..n-1? Wait, they must be a permutation of 0..n-1, and we have p_n=0. So the set of p_1..p_{n-1} must be exactly the nonzero residues {1,2,..., n-1} (since 0 is taken at the end). So we need a sequence of length n-1 consisting of numbers 1..n-1 (each exactly once) such that the prefix products mod n of the first n-1 numbers (all except n) yield exactly all the nonzero residues (each exactly once). And the order of the numbers themselves is a permutation of 1..n-1.
Thus we need a permutation of {1,...,n-1} with the property that the cumulative products (mod n) give a permutation of the set of units? Not exactly because residues might include nonunits as well. But they must be all residues 1..n-1 exactly once.
So we need to find a permutation π of [1,n-1] such that the sequence p_i = (π_1 * π_2 * ... * π_i) mod n is a permutation of {1,2,...,n-1}.
This is like a complete directed Hamiltonian path in the Cayley graph of the multiplicative monoid of Z_n generated by the set {1,...,n-1} where we must use each generator exactly once, and the path must visit all vertices (non-zero residues) exactly once, ending at some residue that when multiplied by n yields 0. But we already fixed the last step as multiplication by n giving 0, but the last prefix product before n (p_{n-1}) can be any nonzero residue. So we just need the path covering all nonzero residues.
This reminds me of "sequencing" the group of units? For prime n, Z_n is a field, all nonzero elements are units. Then the multiplicative group is cyclic of order n-1. There is known construction: take a primitive root g, then order the numbers as g^0, g^{n-2}, ...? Actually we need a permutation of the numbers 1..n-1 (the elements themselves) such that the prefix products give all residues. For prime n, there is a known solution: a_i = i * (i-1)^{-1} mod p? Let's see: If we set p_i = i mod p? Then a_i = p_i * (p_{i-1})^{-1} mod p = i * (i-1)^{-1} mod p. Since i and i-1 are nonzero mod p (except i=1, i-1=0 but p_0? Actually we need p_0 =1? Not given). But we can define p_0 = 1 (empty product) and then p_i = i mod p for i=1..p-1. Then a_i = i * (i-1)^{-1} mod p. This yields numbers from 1..p-1? For prime p, as i runs from 1 to p-1, a_i are all distinct? Let's check p=7: p_1=1, a_1 = 1 * (1)^{-1}? Wait we need p_0 = 1, then a_1 = p_1 * (p_0)^{-1} = 1 * 1^{-1}=1. So a_1=1. p_2=2, then a_2 = 2 * (1)^{-1}? Actually p_1=1, so a_2 = p_2 * (p_1)^{-1} = 2 * 1^{-1}=2. That gives a_2=2, but in the example for n=7 they had a_1=1, a_2=4, not 2. So this is not the only solution. Indeed, we can set p_i to be any permutation of residues and then compute a_i. But the a_i must be exactly the numbers 1..n-1 (excluding n). So we need a bijection between positions i and a_i such that the a_i are a permutation of 1..n-1. This is equivalent to constructing a sequence (p_0=1, p_1,..., p_{n-1}) of distinct nonzero residues with p_{n-1} arbitrary, and then define a_i = p_i * (p_{i-1})^{-1} mod n (if p_{i-1} is invertible). However, p_{i-1} must be invertible mod n for the inverse to exist. In prime case, all nonzero are invertible, so that's fine. But for composite n, some residues are not invertible, so we cannot define a_i as such because the equation p_i = p_{i-1} * a_i mod n may not be uniquely solvable for a_i if p_{i-1} is not invertible (there could be multiple a_i giving same p_i). However, we still need a_i to be a specific number from 1..n-1. So we can still choose them.
So the problem reduces to existence of a Hamiltonian path that visits each nonzero residue exactly once, where each step multiplies by a distinct number from 1..n-1. Also the path must use each number exactly once as the multiplier.
Alternatively, we can think of it as we need to order the numbers 1..n-1 such that the partial products are all distinct modulo n. Since the cumulative product after using all n-1 numbers is something, but note that product of all numbers 1..n-1 mod n is (n-1)! mod n. That might be something. For prime n, Wilson's theorem says (n-1)! ≡ -1 mod n. So p_{n-1} = -1 mod n = n-1. So if we set a_n = n, then p_n = (-1)n ≡ 0 mod n, okay. And indeed the example: product of first 6 numbers (1,4,3,6,5,2) is 143652 = 720? Actually 720 mod7 = 6? 720/7=102*7=714, remainder 6 = -1 mod7. Yes. So indeed p_6 = 6. So that matches Wilson.
Now for composite n that are not prime, can we have such a sequence? Let's test n=4: (n-1)! = 3! = 6 mod4 = 2. In our constructed sequence (1,3,2) product = 132=6 mod4=2, and p_3=2, then a_4=4 yields 0. So works.
n=8: (7)! = 5040 mod8 = ? 5040/8=630 exactly? 8630=5040, so 5040 mod8 = 0. But wait, 7! includes 2,4,6,8? Actually 7! = 5040, which is divisible by 8 because 8=2^3, and 7! contains many factors of 2: exponent of 2 in 7! is floor(7/2)+floor(7/4)+floor(7/8)=3+1+0=4, so 2^4=16, so yes divisible by 8. So (n-1)! mod n = 0 for n=8. That would mean that if we take any permutation of 1..7, the product of all 7 numbers mod8 might be 0? Not necessarily; product of all numbers 1..7 mod8 might be 0 because there is an even number with enough 2's? Actually we need to check: the numbers 1..7 contain 2,4,6. Among them, 2 provides one 2, 4 provides two 2's (2^2), 6 provides one 2. Total exponent of 2 in product of 1..7 is: floor(7/2)=3, floor(7/4)=1, floor(7/8)=0 -> total 4? Wait careful: The exponent of prime p in n! is sum_{k>=1} floor(n/p^k). For 7! the exponent of 2 is floor(7/2)=3, floor(7/4)=1, floor(7/8)=0 => total 4. So product contains 2^4 =16, which is divisible by 8. So yes, 7! is divisible by 8. So any permutation of the set {1,2,...,7} will have the same product (commutative) which is 7! So product of all numbers from 1 to 7 is 5040, mod8 = 0. Therefore p_{7} = 0 if we multiply all 7 numbers. But we need p_{7} (the prefix product after 7 numbers, before adding n) to be nonzero because we need to have zero only at step 8 after multiplying by n. However, if the product of the first 7 numbers is already 0 mod8, then we would have p_7 = 0, violating the condition that p_i must be nonzero for i < n. But wait: p_7 is the product of the 7 numbers we placed in positions 1..7. Since multiplication is commutative, the product of any ordering of the numbers 1..7 is the same: 7! mod8 = 0. So p_7 will always be 0. That means we cannot avoid hitting zero before the last step, because after using all numbers 1..7, the product is divisible by 8, so p_7 = 0. However, could it be that we hit zero earlier? But if p_7 is 0, then at i=7 we have zero, which is okay only if we want zero at i=7 and then at i=8 we also get zero? But we need zero exactly once, at i=8. If p_7=0, then we have zero at position 7 and also at position 8 (since 0a_8=0). So duplicate zeros => invalid. So for n=8, it seems impossible because the product of any 7 numbers from 1..7 (i.e., all except 8) is always 7! which is divisible by 8. Thus p_{n-1} is forced to be 0. So no solution.
But is it always that (n-1)! mod n = 0 for composite n? Not always. For n=4, (3)! = 6 mod4 = 2 ≠ 0. For n=6, (5)! = 120 mod6 = 0? 120/6=20, remainder 0, yes 5! is divisible by 6. For n=9, (8)! = 40320 mod9 = ? 94480=40320? 94480=40320, exactly? Let's check: 9*4480 = 40320, yes 40320. So 8! is divisible by 9? 9=3^2, exponent of 3 in 8! is floor(8/3)=2, floor(8/9)=0 => total 2, so exactly 3^2 divides 8!. So 8! is divisible by 9. So (n-1)! mod n = 0 for n=9. That would suggest impossible for n=9. But wait, we might not need to use all numbers 1..n-1? Actually we must use exactly numbers 1..n-1 in the first n-1 positions. So product of those n-1 numbers is (n-1)! regardless of order. So p_{n-1} = (n-1)! mod n. So if (n-1)! ≡ 0 (mod n), then p_{n-1}=0, which forces zero to appear at step n-1 (or earlier), causing duplicate zeros when we multiply by a_n (which will also give zero). That would be invalid because we need zero only at the last step. However, could it be that zero appears earlier but then we somehow skip duplicate? No, because once zero appears, it stays zero. So if p_{n-1}=0, then we have zero at step n-1 and also at step n. So we would have at least two zeros. But the requirement is that the prefix product sequence is a permutation of 0..n-1, each exactly once. So two zeros would violate. So necessary condition: (n-1)! mod n ≠ 0. That is, (n-1)! is not divisible by n.
But is this condition also sufficient? For n where (n-1)! mod n ≠ 0, can we always construct a permutation? Let's test n=1: trivial. n=2: (1)! =1 mod2=1 ≠0, works. n=3: (2)! =2 mod3=2 ≠0, works. n=4: (3)! =6 mod4=2 ≠0, works. n=5: (4)! =24 mod5=4 ≠0, works. n=6: (5)! =120 mod6=0, fails. n=7: (6)! =720 mod7=6 ≠0, works. n=8: (7)! =5040 mod8=0, fails. n=9: (8)! =40320 mod9=0, fails. n=10: (9)! =362880 mod10= ? 10 divides 9! because 9! has factors 2,5? Actually 9! includes 2 and 5, so 10 divides 9! -> remainder 0. So fails. n=11 prime, works. n=12: (11)! mod12? 11! is divisible by 12 because 12=2^2*3, and 11! has at least 2^? exponent of 2 in 11! is floor(11/2)=5, floor(11/4)=2, floor(11/8)=1 => total 8, exponent of 3 is floor(11/3)=3, floor(11/9)=1 => total 4. So definitely divisible by 12. So fails.
It seems that for composite n, (n-1)! is divisible by n except for n=4? And maybe n=1? Let's examine: A well-known theorem: (n-1)! ≡ -1 mod n if and only if n is prime (Wilson's theorem). But (n-1)! ≡ 0 mod n for composite n > 4? Actually there is a known result: For composite n ≠ 4, (n-1)! ≡ 0 mod n. Check n=4: 3! =6 ≡ 2 mod4, not 0. So the exception is n=4. So the condition (n-1)! mod n ≠ 0 holds exactly for primes and n=4. But also n=1 trivial.
Let's verify n=6 composite, (5)! mod6 = 0. n=8 composite, (7)! mod8 = 0. n=9 composite, (8)! mod9 = 0. n=10 composite, (9)! mod10 = 0. n=12 composite, (11)! mod12 = 0. n=14? 13! divisible by 14? Since 14=27, 13! contains both, so yes. So only exception is n=4. Why? Because for composite n with at least two distinct prime factors or a prime power with exponent > 1 and >4? Actually 4=2^2, but (3)! = 6, which is not divisible by 4 because 6 lacks two factors of 2? Actually 3! = 6 = 23, only one factor of 2, so not divisible by 4. For n = p^2 where p is an odd prime, say p=3, n=9: (8)! has exponent of 3 floor(8/3)=2, floor(8/9)=0, so exactly 3^2, so divisible by 9. For p=2, n=4: (3)! exponent of 2 floor(3/2)=1, so only one factor of 2, not enough for 4. So 4 is the only composite where (n-1)! is not divisible by n.
Thus the necessary condition for existence appears to be: n is prime or n=4 (or n=1). Check n=2,3,5,7 etc are primes. So sample n=7 gives YES, n=6 gives NO. That matches.
But wait, is n=4 the only composite that works? Could n=1? Works. So answer: YES if n is prime or n=1 or n=4; otherwise NO. But we should confirm if n=2 and n=3 are allowed, they should be YES. Also n=4 works, as we constructed. Check n=8? We argued impossible due to product of first 7 numbers being 0. So NO.
But is there any other composite that could work? Maybe n=2p? For n=2p where p is an odd prime? (n-1)! = (2p-1)! . Does it contain factors 2 and p? It will, because p ≤ 2p-1 and 2 ≤ 2p-1 (for p≥2), so both present, so divisible by 2p, hence zero. So fails. How about n = p^k for k>1 and p odd? As argued, (p^k - 1)! contains p^k? Actually the exponent of p in (p^k - 1)! is sum_{i=1}^{∞} floor((p^k-1)/p^i). For p odd, we can check if it's at least k. For p^k, need exponent >= k. Usually floor((p^k-1)/p) = p^{k-1} - 1/p? Actually floor((p^k-1)/p) = p^{k-1} - 1? Because p^k/p = p^{k-1}, but minus 1/p? More precisely: (p^k - 1)/p = p^{k-1} - 1/p, floor = p^{k-1} - 1 if p^{k-1} is integer? Wait compute: p^k - 1 = p * p^{k-1} - 1. Dividing by p gives p^{k-1} - 1/p, floor is p^{k-1} - 1? Actually if p^{k-1} is integer, then p^{k-1} - 1/p is not an integer; its floor is p^{k-1} - 1 because it's less than p^{k-1} by 1/p. For example p=3, k=2: n=9, (8)/3 = 2.666..., floor=2 = 3^{1} - 1 = 2. So floor((p^k-1)/p) = p^{k-1} - 1. Similarly floor((p^k-1)/p^2) = p^{k-2} - 1? Actually (p^k-1)/p^2 = p^{k-2} - 1/p^2, floor = p^{k-2} - 1 if k≥2? For p=3,k=2: /9 = 0.888..., floor=0. That is p^{0} - 1? p^{0}=1, 1-1=0. So yes pattern: floor((p^k-1)/p^i) = p^{k-i} - 1 for i ≤ k? Let's test i=k: p^{0} - 1 = 0. So sum_{i=1}^{k} (p^{k-i} - 1) = (p^{k-1}+p^{k-2}+...+1) - k = (p^k -1)/(p-1) - k. For p≥3, (p^k -1)/(p-1) - k ≥? For k=2: (p^2 -1)/(p-1) -2 = (p+1) -2 = p-1. For p=3, that's 2, which is exactly k=2? Actually we need exponent ≥ k=2. p-1 for p=3 is 2, so equals k. So exponent exactly = k? For p=3, k=2, exponent is 2, so divisible by 9? Since exponent exactly 2, divisible by 9. For p=5, k=2: exponent = (25-1)/(4) -2 = (24/4)-2=6-2=4, which is >2. So divisible. For k=3, p=3: exponent sum = (27-1)/2 -3 = 13 -3 =10, which is >3. So exponent >= k for p odd k>=2. Actually we need to verify if exponent can be less than k for some p,k? Let's compute lower bound: (p^k -1)/(p-1) - k. Since (p^k -1)/(p-1) = 1 + p + p^2 + ... + p^{k-1}. That sum is at least 1 + p + ... . For p≥3, the sum is > k (since each term at least 3 except 1). Actually for k=2: 1+p = 1+3=4, minus 2 =2. So exponent =2 exactly = k. So still equals k. So divisible by p^k. For p=2, k=3: n=8, (7)! exponent of 2: sum floor(7/2)=3, floor(7/4)=1, floor(7/8)=0 total =4, which is >3, so divisible by 8. So indeed for composite n>4, (n-1)! is divisible by n. The only exception is n=4 because p=2,k=2 gives exponent of 2 in 3! is floor(3/2)=1, floor(3/4)=0 total=1 <2, so not divisible by 4. Also maybe n=2? But 2 is prime. So yes.
Thus the necessary condition that (n-1)! mod n ≠ 0 is equivalent to n being prime or n=4 (or n=1). So the answer: YES if n is 1, 4, or prime; else NO.
But is this condition sufficient? For primes, we need to construct a permutation. For n=4, we already have a construction. For n=1, trivial.
Now we need to also provide a valid permutation when answer is YES. So we need an algorithm to construct such a permutation for primes and for n=4 (and maybe n=2,3, etc). For n=4 we can output the known sequence: 1,3,2,4 (or 1,3,2,4? Actually earlier we had 1,3,2,4? Let's check: Our construction: a1=1, a2=3, a3=2, a4=4 gave prefix products: 1,3,2,0. That's a permutation of 0..3. Yes. Or the example for 7 gave something else. So for prime n, we need a general construction.
One known construction for prime p: Let a1 = 1. For i from 2 to p-1, set a_i = i * (i-1)^{-1} mod p, where inverse is taken modulo p. This yields a_i ∈ {1,..., p-1} distinct? Let's check. p_0 = 1 (imaginary). Then define p_i = i mod p for i=1..p-1. Since p is prime, i mod p are distinct nonzero. Then a_i = p_i * (p_{i-1})^{-1} mod p = i * (i-1)^{-1} mod p. This is well-defined because i-1 is nonzero for i=2..p-1. For i=1, we set a1 = p1 = 1. Then the a_i for i=1..p-1 are: a1=1; a2 = 2 * 1^{-1} = 2; a3 = 3 * 2^{-1} mod p; a4 = 4 * 3^{-1} mod p; ... a_{p-1} = (p-1) * (p-2)^{-1} mod p. Are these all distinct? Likely yes, because they are the ratios of consecutive numbers. Let's test for p=7: compute a's:
a1=1.
a2 = 2 * 1^{-1} = 2.
a3 = 3 * 2^{-1} mod7. 2^{-1} mod7 = 4 (since 24=8≡1). So 34=12 mod7=5. So a3=5.
a4 = 4 * 3^{-1} mod7. 3^{-1}=5 (35=15≡1). So 45=20 mod7=6. a4=6.
a5 = 5 * 4^{-1} mod7. 4^{-1}=2 (42=8≡1). 52=10 mod7=3. a5=3.
a6 = 6 * 5^{-1} mod7. 5^{-1}=3 (53=15≡1). 63=18 mod7=4. a6=4.
So a sequence: 1,2,5,6,3,4 and then a7 = 7? But we also need a_n = n at the end? Actually we need a permutation of 1..n, with n at the end? Not necessarily at the end; we could place n somewhere else? Wait, we earlier argued that we can set a_n = n to ensure p_n=0. But is it required that a_n = n? Possibly not; we could have a_n be another number that when multiplied yields 0, and then n appears earlier. But for simplicity, we can choose to place n at the end. But we need to check if the construction above yields a permutation of 1..p? It gives a1..a_{p-1} as some ordering of numbers 1..p-1 (check distinctness: 1,2,5,6,3,4 - that's all numbers 1..6). And then we can set a_p = p. So the full permutation is: 1,2,5,6,3,4,7. But the example for 7 gave: 1,4,3,6,5,2,7. That is a different permutation. So any construction works.
Now we need to ensure that the a_i computed by this method are indeed within 1..p-1 and distinct. Let's prove: For prime p, the map i -> i * (i-1)^{-1} (with i from 2 to p-1) yields a permutation of {2,...,p-1}? Actually also includes 1? a1=1. So all numbers appear. This is because the mapping from i to a_i is essentially the function f(i) = i/(i-1) in the field F_p. As i runs over 2..p-1, f(i) takes values in F_p \ {1}? Let's see: f(2)=2; f(3)=3/2; etc. We can show that f is a bijection from {2,...,p-1} to {2,...,p-1} maybe except some value? Actually we need to show that the set {a_i | i=2..p-1} = {2,3,...,p-1} and all distinct. Consider the equation a = i/(i-1) mod p => a(i-1) ≡ i mod p => a i - a ≡ i => a i - i ≡ a => i(a-1) ≡ a => i ≡ a * (a-1)^{-1} mod p (since a≠1). So for each a ≠ 1, there is a unique i = a*(a-1)^{-1} mod p. That i will be in {2,...,p-1} as long as a ≠ 0? But a cannot be 0 because a ∈ {1..p-1}. Also a=1 gives division by 0. So for each a in {2..p-1}, there is a unique i. So indeed the mapping i -> a_i is a bijection from {2..p-1} to {2..p-1}. And a1=1 covers the remaining number. So it's a permutation. So this construction works.
Now we need to output the permutation. For n prime, we can compute a1=1, then for i=2 to n-1, compute a_i = i * inv(i-1) mod n. Since i and i-1 are in [1, n-1], we compute modular inverse modulo n (which exists because n is prime). Then a_n = n.
But careful: The a_i computed by this formula may be numbers from 1 to n-1. However, we need to output them as integers (not modulo). Since we are computing modulo n, the result will be in range 1..n-1. Good.
Now for n=4 (composite but works), we can provide a fixed sequence: 1,3,2,4. For n=1: output "1"? Actually the problem says n more lines, each containing an integer a_i. For n=1, we need one line with the number? It should be a permutation of [1], so just 1. And the prefix product sequence would be [0] (since 1 mod1 =0). That is a permutation of [0]. So YES, output 1.
But wait, check condition: For n=1, the set is [0] so prefix product should be permutation of [0]. Yes.
Thus overall solution:
Read n.
If n == 1: print YES, then 1.
Else if n == 4: print YES, then "1\n3\n2\n4".
Else if is_prime(n): print YES, then construct permutation as described, with a_n = n.
Else: print NO.
Now, we need to ensure correctness for n=2. 2 is prime, so we apply prime construction. Compute: a1=1 (since p1=1). For i=2 to n-1? For n=2, n-1 =1, so loop from 2 to 1 empty. So we only have a1=1, then a2 = n =2. So permutation: 1,2. Prefix products mod2: 1, 0 => permutation of [0,1]. Works.
For n=3: a1=1; loop i=2 to 2: compute inv(1)=1, a2=21=2; a3=3. Permutation: 1,2,3. Check: p1=1, p2=12=2 mod3=2, p3=2*3=6 mod3=0 => [1,2,0] permutation of 0,1,2. Works.
Thus the prime construction yields p_i = i mod n for i=1..n-1, and p_n=0.
Now we need to consider n=4 separately. Could we also use the prime construction for n=4? No, because 4 is not prime and modular inverses may not exist for all i-1. For i=3, i-1=2, but 2 is not invertible mod4. So the construction fails.
Thus our solution is complete.
But we must also consider n=0? Not allowed, n>=1.
Now, we need to output n lines of the sequence. For prime n up to 1e5, we need to compute modular inverses quickly. Since n is prime, we can compute inverse using fast exponentiation (modular exponentiation) because n up to 1e5, it's fine O(n log n) total, which is acceptable. Or we can precompute inverses using Fermat's little theorem: inv(x) = x^{n-2} mod n. Doing this for each i (n up to 1e5) yields O(n log n) which is fine.
But we can also compute using extended Euclidean algorithm per value; but pow mod is simpler.
Alternatively, we could generate a different construction that doesn't require computing inverses, but it's okay.
But note: For large prime near 1e5, n-2 exponent may be large, but we can compute pow(x, n-2, n) efficiently.
Time complexity: O(n log MOD) per test, which is fine.
Now, there is a nuance: The problem may have multiple test cases? No, single input n.
Now we must be careful: The construction gives p_i = i mod n for i=1..n-1. That yields p_i values from 1 to n-1, which are distinct. However, we must also ensure that the a_i themselves are all distinct and cover 1..n. As argued, they do. But we need to verify that a_i computed as i * inv(i-1) mod n yields numbers in {1,..., n-1} and are all distinct. Let's quickly prove distinctness: Suppose for i≠j, a_i = a_j. Then i/(i-1) ≡ j/(j-1) mod p => cross-multiplying: i(j-1) ≡ j(i-1) => ij - i ≡ ij - j => -i ≡ -j => i≡j mod p. Since i,j in {2..p-1} and distinct, this is impossible. So distinct.
Now for i=1, a1=1, which is different from others because others are never 1? Check a_i for i≥2: can a_i be 1? That would require i ≡ i-1 mod p => 1 ≡ 0 mod p => impossible. So all good.
Thus for prime n, the construction works.
Now for n=4 we output specific sequence. Could there be other composites? Possibly n=1 but that's trivial. Also maybe n=2? but it's prime.
Thus final answer: YES for prime n or n=4 or n=1; else NO.
But let's double-check if n=4 is indeed the only composite >1 with (n-1)! not divisible by n. Known result: If n is composite and n ≠ 4, then n | (n-1)!. Because if n = ab with 1 < a, b < n, then both a and b appear in the product 1..n-1, unless one factor equals n? Actually careful: For n = p^2, with p prime, if p^2 > 2p? For n=4, p=2, the two factors a=b=2? But 2 appears only once in 1..3? Actually product (n-1)! contains each integer from 1 to n-1. If n = a*b with a,b > 1, and a ≠ b, then both a and b are ≤ n-1 (since max(a,b) ≤ n/2 < n for n>4? Actually if n is composite and not prime square? Let's examine. Known theorem: (n-1)! ≡ 0 mod n for all composite n except n=4. That's a known fact from Wilson's theorem extension. So correct.
Thus we can confidently answer NO for all other composites.
But we must also ensure that for n=1, we output YES and a sequence. The problem says n lines. So output 1 line with 1.
Now, what about n=0? Not in range.
Thus solution.
But wait, the problem statement says "The elements of the sequence should be different positive integers no larger than n." That's satisfied.
Now, let's think: Is there any possibility for n=4 to be derived from prime-like construction? Not needed.
Now, we need to implement prime checking for n up to 1e5. Simple trial division up to sqrt(n) is fine (sqrt(1e5)=316). We'll check if n is prime or 4 or 1.
Now, for prime n, we need to output the sequence. We'll compute inv[i-1] for i from 2 to n-1. Could compute inverses using Fermat: inv(x) = pow(x, n-2, n). Since n is prime, we can compute pow quickly.
Alternatively, we can compute all inverses in O(n) using extended Euclid or using the fact that we have a loop; but O(n log n) is fine for 1e5.
But we can also compute a_i as i * inv(i-1) % n, and ensure that if the result is 0? It won't be 0 because i and inv are nonzero mod n. The result is in 1..n-1.
Now, we also need to output a_n = n.
Edge case: n=2: loop from 2 to 1, so no intermediate a_i computed. So output a1=1, a2=2.
Edge case: n=3: loop i=2: compute inv(1)=1, a2=2*1%3=2, then a3=3.
Now, confirm that the prefix product sequence is indeed permutation: p1=1, p2= (12)%3=2, p3= (23)%3=0. Works.
Now, for n=4, we can output: 1,3,2,4. Check: p1=1%4=1, p2=13=3, p3=32=6%4=2, p4=2*4=8%4=0 -> [1,3,2,0] permutation.
Thus done.
But wait, we should consider if there is any other composite n like n=1? Already covered. What about n=2? It's prime.
Thus solution is correct.
Now, we might consider n=0? Not.
Now, we should write the solution accordingly.
But before finalizing, we must also consider if there is any nuance regarding the prefix product sequence definition: It might be the actual product without modulo? But they said "prefix product sequence" and then "is a permutation of [0,1,...,n-1]". If product without modulo, values would be huge and can't be limited to 0..n-1. So definitely modulo.
Also the note: "For the second sample, there are no valid sequences." So they confirm.
Now, we can produce an answer.
But what about n=4? They didn't provide sample. But it's fine.
Now, check for n=9? According to our condition, it's NO. Let's verify quickly if it's indeed impossible. Suppose we try to construct. The product of first 8 numbers is 8! = 40320, divisible by 9, so p_8 would be 0. To avoid p_8=0, we would need to have p_8 !=0, but product of all 8 numbers is fixed, so impossible. So NO. Good.
Now, think about n=2p for p prime? Already covered: (n-1)! includes both 2 and p, so divisible.
Thus answer stands.
Now, implement:
Algorithm:
Input n.
If n == 1: print YES and 1.
Else if n == 4: print YES and "1\n3\n2\n4".
Else if is_prime(n):
Compute array a of size n+1 (1-indexed)
a[1] = 1
For i from 2 to n-1:
inv = mod_pow(i-1, n-2, n) // since n prime
a[i] = (i * inv) % n
a[n] = n
Print a[1..n] each on new line.
Else: print NO.
Now, careful: For n=2, loop from 2 to 1 does nothing. So a[1]=1, a[2]=2.
Now, we must ensure that for n=3, inv(1) computed via pow(1, n-2, n) gives 1 anyway.
Now, we might need to handle n=1 separately to avoid prime check. For n=1, is 1 prime? Usually not, but we handle separately.
Now, we also need to consider if there's any off-by-one: p_i for i from 1 to n-1 should be i mod n. For i=n-1, p_{n-1}=n-1. Then multiply by a_n=n gives 0.
Now, check for n=2: p1=1, p2=0. Works.
Now, ensure that for n=4 prime? It's not prime, so we use separate case. But what if we try to apply prime construction for n=4? It would require inv(2) mod4, which doesn't exist. So separate.
Now, is there any other composite where (n-1)! not divisible by n? Let's double-check n=2? prime. n=3 prime. n=4 composite works. n=8 fails. n=9 fails. n=25? 24! exponent of 5: floor(24/5)=4, floor(24/25)=0 total 4, which is less than k=2? Actually need exponent >=2 for 25, and 4 >=2 so divisible. So fails. n=2*2=4 is the only exception because 2^2 requires exponent 2, but (3)! exponent of 2 is only 1. For 2^k with k>=3: (2^k -1)! exponent of 2 is sum floor((2^k-1)/2^i). For k=3: sum floor(7/2)=3, floor(7/4)=1, floor(7/8)=0 =4 >=3, so divisible. So only 4.
Now, what about n=1? trivial.
Thus correct.
Now, we should also think about whether there exists solution for n=4 that does not end with 4? But we output one anyway.
Now, note: The problem may require to print "YES" and then the sequence only if solution exists. For NO, just "NO". Ensure no extra spaces.
Now, check sample: Input 7, output YES and 7 numbers. The example output for 7 is: YES then lines: 1,4,3,6,5,2,7. Our prime construction gives: 1,2,5,6,3,4,7. That's different but acceptable. So fine.
Now, we can implement.
Potential pitfalls: For n=1, we must output exactly "YES\n1". Verify prefix product: a1=1, p1=1 mod 1 = 0, which is the only element, so permutation of [0]. Yes.
But the problem says the prefix product sequence should be a permutation of [0,1,...,n-1]. For n=1, that set is [0], so okay.
Now, let's consider if n=1 is prime? Not typically, but we treat separately.
Now, we should confirm that for n=4, the sequence we output yields prefix products covering 0..3. Already did.
Now, ensure that for n=4, we output a1=1, a2=3, a3=2, a4=4. That's distinct and within 1..4.
Now, let's implement prime check:
bool is_prime(int n) {
if (n < 2) return false;
for (int i=2; i*i<=n; i++) if (n%i==0) return false;
return true;
}
Since n up to 1e5, sqrt is 316, fine.
Now, we should also treat n=2 as prime, fine.
Now, implement modular exponentiation:
long long mod_pow(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b&1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
Now, when n is prime and n <= 1e5, we can compute inv quickly.
But note: For n=2, n-2 =0, pow(x,0,2) =1 for x%2=1? For x=1, inv(1) should be 1. That's fine. For x=1, pow(1,0,2)=1. For x=2? Actually i-1 will be 1 only. So fine.
Now, handle potential overflow: use long long.
Now, we can output directly.
Now, let's double-check for n=7 with our construction: a1=1, a2=2, a3=5, a4=6, a5=3, a6=4, a7=7. p1=1, p2=2, p3=25=10 mod7=3, p4=36=18 mod7=4, p5=43=12 mod7=5, p6=54=20 mod7=6, p7=6*7=42 mod7=0. So [1,2,3,4,5,6,0] - permutation of 0..6. So it's valid.
Now, is it always true that p_i = i? For i=3 we got 3, yes. For i=2 we got 2. So p_i = i for i=1..6. Good.
Thus our construction works.
Now, let's check for n=5: p1=1, p2=2, p3=3? Actually we computed p3 as 24=8 mod5=3, yes. p4=33=9 mod5=4. So p_i = i.
Thus general: p_i = i mod n.
Now, note: For i = n-1, p_{n-1} = n-1, which is valid because n-1 is nonzero mod n.
Now, we must ensure that a_i computed as i * inv(i-1) mod n yields numbers 1..n-1. But note: For i = n-1, compute inv(n-2) mod n. Since n is prime, n-2 is nonzero and invertible. So a_{n-1} = (n-1)*inv(n-2) mod n, which is some number between 1 and n-1. Good.
Now, what about i=2? a2 = 2 * inv(1) = 2.
Now, all good.
Now, we need to output each a_i as an integer. Since we computed modulo n, it's already in range [0,n-1]. But for a_i = 0? Could it be 0? Let's check: i * inv(i-1) mod n = 0 only if i ≡ 0 mod n, but i ≤ n-1, so i not 0. So no zero. So output numbers are 1..n-1.
Now, we must also output a_n = n.
Now, check n=2: a1=1, a2=2. Works.
Now, let's think about memory: we just output sequentially without storing all? But we need to compute a_i for i from 2 to n-1, each depending on modular inverse. We can compute on the fly and store in array or just output as we compute. However, we need to output a1 first. So plan: Print "YES". Then print a1=1. Then for i=2 to n-1, compute a_i and print. Then print n. Done.
Now, for n=1: Print YES, then 1.
Now, for n=4: Print YES, then 1,3,2,4.
Now, for NO: just print "NO".
Now, edge case: n=0 not possible.
Now, we must also think about n=1 and n=4 in prime detection? is_prime(4) returns false, so we handle separately.
Now, what about n=1? Could be considered as prime? But we treat separately to avoid complications.
But wait: There's a nuance: The problem statement says "Define its prefix product sequence ." The image might show product without mod? But the example clearly uses mod. So we are correct.
Now, we should ensure that the prefix product sequence is exactly the list of prefix products, and they should be a permutation of 0..n-1. For prime n, our construction yields p_i = i for i=1..n-1, and p_n=0. That's a permutation.
Now, verify n=4 with our construction: p_i: 1,3,2,0 -> permutation.
Now, could there be a solution for n=8 that somehow avoids p_7=0? As argued, product of first 7 numbers is 7! = 5040, which is divisible by 8. So p_7 must be 0. Unless we don't use all numbers 1..7 in the first 7 positions? But we must use each number exactly once, and the last number is from 1..8. If we place number 8 somewhere in the first 7 positions, then the set of first 7 numbers includes 8 and excludes some other number. In that case, the product of the first 7 numbers is not 7! but product of 7 distinct numbers from 1..8, not including the 8th number which is placed at the last. So p_{n-1} is product of n-1 numbers that are a subset of 1..n missing exactly one number (the one placed at the end). So p_{n-1} is not necessarily (n-1)!; it's (n! / a_n) mod n, where a_n is the number placed last. Since n! mod n = 0, we have (n! / a_n) mod n might not be 0 if division is not defined mod n? But careful: We're working modulo n, and we cannot simply divide because mod n is not a field. But we can consider the actual integer product of the first n-1 numbers = (n!)/a_n. Since a_n is one of 1..n. So p_{n-1} = ((n!)/a_n) mod n. Since n! is divisible by n (for n>1, n! always contains n as a factor? Actually n! always contains n as a factor, but if n is composite, n! may contain multiple factors of n). So n! is divisible by n. Then (n!)/a_n might or might not be divisible by n. For p_{n-1} to be nonzero mod n, we need that (n!)/a_n is not divisible by n.
So the condition is: There exists a number a_n ∈ {1,...,n} (the last element) such that when we remove it from the full set, the product of the remaining n-1 numbers is not divisible by n, but the full product (including a_n) is divisible by n. And we also need to be able to order the remaining n-1 numbers such that all prefix products are distinct and nonzero.
Thus our earlier simplification that we always place n at the end might be too restrictive. For n=8, maybe we can choose a_n = something else (like 4) such that (8!)/4 is not divisible by 8, and we can order the rest to get prefix products all distinct nonzero. But we also need the condition that the cumulative product never becomes 0 before step n. Is it possible for n=8? Let's examine.
We need to check if there exists a_n ∈ {1..8} such that (8!)/a_n is not divisible by 8. 8! = 40320. Compute Q = 40320 / a_n. We need Q mod 8 ≠ 0. Since 40320 mod 8 = 0 (as earlier). For Q to be not divisible by 8, we need that a_n contains enough factors of 2 to reduce the exponent of 2 in Q below 3 (since 8=2^3). The exponent of 2 in 8! is: floor(8/2)=4, floor(8/4)=2, floor(8/8)=1 total =7. So 8! = 2^7 * (odd). So Q = 2^{7 - v2(a_n)} * (odd) where v2(a_n) is exponent of 2 in a_n. For Q to be divisible by 8=2^3, we need 7 - v2(a_n) ≥ 3, i.e., v2(a_n) ≤ 4. For Q to be NOT divisible by 8, we need 7 - v2(a_n) < 3 => v2(a_n) > 4. Since max v2 among numbers 1..8: let's compute v2 of each: 1:0, 2:1, 3:0, 4:2, 5:0, 6:1 (6=2*3), 7:0, 8:3 (2^3). Maximum v2 is 3 (for 8). 3 is not >4. So v2(a_n) ≤ 3, thus 7 - v2(a_n) ≥ 4, so Q is divisible by 2^4 at least, definitely divisible by 8. So for all a_n, Q is divisible by 8. Therefore p_{n-1} = Q mod 8 = 0. So no matter which number we put last, the product of the other 7 numbers is always divisible by 8. So p_{n-1}=0 inevitable. Thus impossible.
Thus the necessary condition is: There exists some a ∈ {1..n} such that (n!)/a is not divisible by n. That is equivalent to saying that n does not divide (n-1)!? Not exactly, because (n-1)! is product of all numbers except n. But here we could exclude some other number. So we need that the total product of all numbers except some a is not divisible by n. That means that the prime factor exponents of n are not all met in the product of the other numbers. In other words, there is some number a that contains a "critical" factor that is needed to reach the exponent for n. If we remove that number, the product of the rest may lack sufficient exponent for some prime factor of n.
Thus the condition is: n is not a "prime power" with high exponent? Actually let's analyze generally.
Let n have prime factorization: n = ∏ p_i^{k_i}. In n! (product of all numbers 1..n), the exponent of p_i is e_i = sum_{j=1}∞ floor(n / p_i^j). The exponent in the product of all numbers except a (where a is some integer between 1 and n) is e_i - v_{p_i}(a). So we need there to exist a such that for at least one i, e_i - v_{p_i}(a) < k_i, i.e., v_{p_i}(a) > e_i - k_i. Because for the product to be divisible by p_i^{k_i}, we need exponent >= k_i. If for some i, the exponent falls below k_i, then the product is not divisible by that prime power, and hence not divisible by n (since n requires all prime powers). So we need existence of a such that for each i, e_i - v_{p_i}(a) >= k_i? Actually to be NOT divisible by n, it suffices that for at least one i, e_i - v_{p_i}(a) < k_i. Because if any prime power missing, product not divisible by n. So we want there to exist a such that for at least one i, v_{p_i}(a) > e_i - k_i. Since v_{p_i}(a) is bounded by v_{p_i}(a) ≤ floor(log_{p_i} n). So we need e_i - k_i < max possible v_{p_i}(a) maybe.
But is it always possible for composite n > 4? For n=4, we had 4! = 24, e_2 = floor(4/2)+floor(4/4)=2+1=3? Actually 4! exponent of 2: floor(4/2)=2, floor(4/4)=1 total 3. k=2. Then e_2 - k = 3-2=1. So we need a number a with v2(a) > 1, i.e., v2(a) ≥2. Numbers with v2≥2 in {1..4}: 4 has v2=2, so choose a=4. Then e_2 - v2(a) = 3-2=1 < k=2, so product of others not divisible by 4. So that works. So for n=4, such a exists (a=4). For n=6: 6=2*3. Compute exponents: for p=2, e_2 = floor(6/2)+floor(6/4)+floor(6/8)=3+1+0=4? Actually 6! exponent of 2: floor(6/2)=3, floor(6/4)=1, floor(6/8)=0 => total 4. k_2=1. So e_2 - k_2 = 4-1=3. To break divisibility by 2, need v2(a) > 3? max v2(a) among 1..6? 4 has v2=2, 6 has v2=1, 2 has v2=1, so max=2 < 4? Actually need > e_2 - k_2 = 3, so need v2(a) ≥4, impossible. So we cannot break the factor 2. For p=3, e_3 = floor(6/3)+floor(6/9)=2+0=2, k_3=1, e_3 - k_3 =1. To break 3, need v3(a) > 1, i.e., ≥2. Numbers with v3≥2? The only multiples of 9 are >6, so none. So impossible. Thus no a exists. So n=6 fails.
For n=8: as computed e_2 =7, k=3, e_2 - k =4. Need v2(a) >4, max v2 among 1..8 is 3 (for 8). So impossible.
For n=9: p=3, e_3 = floor(9/3)+floor(9/9)=3+1=4, k=2, e_3 - k =2. Need v3(a) >2, i.e., ≥3. Numbers 1..9: 9 has v3=2, 27 not present, so max=2, not >2. So impossible.
For n=10: 10=2*5. e_2 for 10! = floor(10/2)+floor(10/4)+floor(10/8)=5+2+1=8, k2=1, e2 - k2 =7; need v2>7 impossible. e_5 = floor(10/5)=2, k5=1, e5 - k5 =1; need v5>1 => v5≥2; numbers with factor 25? none. So impossible.
Thus the condition for existence of a such that (n!)/a is not divisible by n is that for at least one prime power p_i^{k_i}, we have max_{a∈[1,n]} v_{p_i}(a) > e_i - k_i. Since max v_{p_i}(a) is floor(log_{p_i} n). For n=4, p=2, floor(log2 4)=2, e_i - k_i =1, so 2>1 true. For n=2p? For p odd, max v2(a) is floor(log2(2p)) which is maybe 1 if p>2? Actually 2p contains factor 2^1 only unless p has factor 2? p odd, so max v2 is 1 (from number 2, or 2p? Actually 2p may be ≤ n? Since n=2p, max number is 2p. The numbers with high power of 2 are powers of 2 ≤ 2p. The largest power of 2 ≤ 2p is at most 2p but could be 2^? For p=3, n=6, max v2=1 (from 2,6 has v2=1). So need > e2 - k2 =7? no. So fails.
Thus the condition seems to be that there exists a prime power dividing n such that the maximum power of that prime among numbers ≤ n is greater than e_i - k_i. But we can also express this as: n is either prime or 4. But is there any other composite satisfying this? Let's test n=2? prime. n=3 prime. n=4 works. n=12? Already fails. n=16? Compute: n=16=2^4. e_2 for 16! = sum floor(16/2)=8, /4=4, /8=2, /16=1 total=15. k=4, e - k =11. max v2(a) ≤ floor(log2 16)=4. 4 > 11? No. So fails. n=2^k for k>=3 fails. n= p^2 for odd p: e = floor(p^2/p)+floor(p^2/p^2) = p+1. k=2. e - k = p+1-2 = p-1. max v_p(a) = 2 (since p^2 is present, v_p(p^2)=2). Need 2 > p-1. For p>=3, p-1 >=2, so 2 > p-1 is false (for p=3, 2 >2 false, for p>3, false). So fails. For n = p^3? e approx? For p=2, we already covered. For p odd, e would be large, max v_p=3, need > e - k which is big. So fails.
What about n = 2 * 2? Already 4 works. So seems only n=4 is special.
But what about n=2? prime, works. n=1 trivial.
Thus our earlier condition stands.
Now, we must also verify that when such a exists, we can actually order the remaining n-1 numbers to get all prefix products nonzero and distinct, ending with p_{n-1} = Q mod n (nonzero) and then final step yields 0. Is it always possible for prime n and n=4? For prime, we gave construction. For n=4, we gave explicit. For n=1, trivial.
But is there any other composite n where (n!)/a can be non-zero mod n? According to above, only n=4. So fine.
Thus final answer: YES if n is prime or n == 4 or n == 1; else NO.
Now, we need to produce the sequence for primes.
But wait: Could n=2 be considered as prime? Yes. So it's covered.
Now, double-check for n=2 with our construction: a1=1, a2=2 -> p1=1, p2=0. Sequence of prefix products: [1,0] which is permutation of [0,1]. So okay.
Now, we must consider the output format: The first line is YES/NO. If YES, then n lines follow, each with an integer. No extra spaces.
Now, implement accordingly.
Now, potential pitfalls: The prime detection for n up to 1e5. However, there may be a case n=1: we output YES and 1. That's valid.
Now, confirm for n=4, we output:
YES
1
3
2
4
Works.
Now, check for n=1:
YES
1
Works.
Now, think about n=0? Not in constraints.
Now, I think solution is complete.
But before finalizing, I want to check if there is any possibility for n=4 that the sequence must end with 4? In our construction we ended with 4. Could we also end with something else? Possibly, but our output is fine.
Now, check n=4 with prime construction? Would fail.
Now, implement.
We'll implement is_prime:
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true; // 2,3
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
But for n=4, we handle separately, so even if isPrime returns false for 4, we still output YES.
Now, for n=2, isPrime true, good.
Now, one more nuance: In the prime construction, we set a1 = 1. But we need to ensure that p1 = 1 (mod n). That's fine. Now, note that for prime n, the set {1,...,n-1} are all units mod n, so multiplication by any of them is invertible. That means the sequence of p_i will never hit 0 before the final step. Since p_{n-1} = n-1 (nonzero), then p_n = 0 after multiplying by n.
Now, what about n=4? In our sequence, a1=1, a2=3 (unit mod4? 3 is invertible mod4? 33=9≡1 mod4, so yes invertible), a3=2 (not invertible mod4). p2 = 13=3 (nonzero), p3 = 32=6 mod4=2 (nonzero), p4 = 24=0. So the non-invertible step appears at the end where it causes zero.
Now, for prime n, the only non-invertible element is n itself, which is used last.
Now, for n=1, all numbers mod1 are 0, but that's fine.
Thus solution is consistent.
Now, we could also consider a more general construction for primes using the "inverse permutation" approach, but we already have.
Now, one more edge: n=2 is prime, we can use construction: a1=1, a2=2.
Now, what about n=10^5 if prime? We need to compute inverses up to n-2 maybe. O(n log n) fine.
Now, but we must be careful about time: n up to 100000, computing pow for each i (i from 2 to n-1) could be 100000 * log(100000) ~ 100000 * 17 = 1.7M operations, fine.
Alternatively, we could compute all inverses in O(n) using extended Euclid or by using the fact that inv(i) = - (n/i) * inv(n % i) mod n? Actually for prime n, we can compute inverses in O(n) using formula inv[i] = (n - n/i) * inv[n % i] % n. But we only need inv of numbers 1..n-2, which we can compute iteratively. But O(n log n) is simpler and safe.
But we need to compute modular exponentiation each time, which might be a bit heavy but still fine in 1e5. However, powmod in C++ using binary exponentiation per number is okay.
But to be safe and efficient, we can precompute all inverses using Fermat's little theorem with pow for each number. 1e5 pow operations with exponent up to 1e5-2, each pow is O(log n) ~ 17 multiplications, so about 1.7 million multiplications, trivial.
Now, implement in C++:
#include
#include
#include
using namespace std;
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
long long modpow(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int main() {
int n;
cin >> n;
if (n == 1) {
cout << "YES\n1\n";
return 0;
}
if (n == 4) {
cout << "YES\n1\n3\n2\n4\n";
return 0;
}
if (isPrime(n)) {
cout << "YES\n";
cout << "1\n";
for (int i = 2; i <= n-1; ++i) {
// compute inv(i-1) mod n
long long inv = modpow(i-1, n-2, n);
long long ai = (i * inv) % n;
// ai will be in [1, n-1], but ensure it's not 0; if 0 then set to n? Actually shouldn't happen.
// But for safety if ai == 0, we could set ai = n, but that's already used later. But it won't.
cout << ai << "\n";
}
cout << n << "\n";
} else {
cout << "NO\n";
}
return 0;
}
Now, test with n=7: compute:
i=2: inv(1)=1 -> 2
i=3: inv(2)=? 2^{5} mod7 = 32 mod7 = 4? Actually 2^5=32, 32 mod7=4. Or use modpow(2,5,7)=4? Check: 2^1=2,2^2=4,2^3=8 mod7=1, 2^4=2, 2^5=4. So inv=4 -> a3=34=12 mod7=5.
i=4: inv(3)=3^5 mod7=243 mod7? 734=238, remainder 5, inv=5? Actually 3^{-1} mod7 =5 because 35=15≡1. So a4=45=20 mod7=6.
i=5: inv(4)=4^5 mod7? 4^2=16≡2, 4^4= (4^2)^2 = 2^2=4, 4^5=4^44=44=16≡2? Actually compute: 4^{-1} mod7 = 2 (since 42=8≡1). So inv=2 -> a5=52=10 mod7=3.
i=6: inv(5)=5^5 mod7? 5^2=25≡4, 5^4=4^2=16≡2, 5^5=5^45=25=10≡3 -> inv=3 (since 53=15≡1). So a6=63=18≡4. So output: 1,2,5,6,3,4,7. That is a valid permutation.
Now, check n=2: loop from 2 to 1 does nothing, so output 1 then 2.
Now, for n large prime like 100003 (but note n ≤ 1e5, so max prime is 99991 or 99989). O(n log n) fine.
Now, confirm no integer overflow: modpow uses long long, multiplication may exceed 64-bit? (1e5 * 1e5 = 1e10 < 2^63, safe). But when we compute a*a mod n, a could be up to n-1 ~ 1e5, square ~1e10 < 9e18, safe. So long long (64-bit) fine.
Now, we also need to ensure that a_i output is positive and ≤ n. Since we computed mod n, result could be 0 if i*inv % n = 0. Could that happen? i mod n is not 0 (since i < n), inv is not 0 (since (i-1) and n are coprime, its inverse mod n is nonzero). So product mod n is nonzero. So it's in [1, n-1]. Good.
Now, one more nuance: The modular inverse computed via modpow(i-1, n-2, n) is correct only if n is prime. So we only call this when isPrime returns true.
Now, also note that for n=2, modpow(1, 0, 2) returns 1 (since any number ^0 =1). So fine.
Now, we might want to handle n=2 in prime case: loop from 2 to 1, so fine.
Now, double-check for n=2: output "YES\n1\n2\n". That's correct.
Now, sample: Input 7 -> our output differs from example but still valid.
Now, we might test for n=4: our output is 1,3,2,4. Let's compute prefix products: 1 mod4=1, 13=3 mod4=3, 32=6 mod4=2, 2*4=8 mod4=0 -> [1,3,2,0] permutation of [0,1,2,3]. Works.
Now, we could also output 1,4,3,2? Not sure. But fine.
Now, check for n=6: isPrime false and n≠4 => NO. Sample says NO. Good.
Now, I think the solution is correct.
But wait, let's double-check the condition for n=1: The prefix product sequence should be a permutation of [0,..., n-1] = [0]. Our a1=1, product mod1 =0, so yes.
Now, consider n=4: (n-1)! = 6, not divisible by 4, so p_3 can be nonzero. So it works.
Now, there is also n=2? (1)! =1 not divisible by 2, so okay.
Now, is there any composite n where (n-1)! mod n ≠ 0 but n is not 4? Let's test n=1? trivial. I recall the known result: (n-1)! ≡ 0 (mod n) for all composite n except n=4. That is a known theorem: For composite n>4, n divides (n-1)!. So that matches. So the condition (n-1)! mod n != 0 is exactly n=1, prime, or 4. So yes.
Thus we are safe.
Now, just to be thorough, let's test n=22: 21! divisible by 22? 22=2*11, 21! contains 2 and 11, so divisible.
n=25: 24! contains 5 and 10,15,20,24? Actually exponent of 5 in 24! is floor(24/5)=4, floor(24/25)=0 -> 4, >=2, so divisible.
n=27: exponent of 3 in 26! is floor(26/3)=8, floor(26/9)=2, floor(26/27)=0 total=10 >=3? k=3, so divisible.
Thus no other.
Now, implement accordingly.
Now, complexity: O(sqrt(n)) for prime check, O(n log n) for prime construction.
Now, we can produce final answer.
But before finalizing, let's also consider n=0? Not.
Thus final answer.
Now, let's consider whether we need to output the numbers in separate lines exactly as required.
Now, I'll produce final code.
Potential improvement: Use fast I/O.
Now, include bits/stdc++.h? But we can keep standard.
Now, for n=4, we output "1\n3\n2\n4\n". For n=1, output "1\n".
Now, test:
Input: 1 -> YES 1
Input: 2 -> YES 1 2
Input: 3 -> YES 1 2 3
Input: 4 -> YES 1 3 2 4
Input: 5 -> YES 1 2 4 3 5
Input: 6 -> NO
Input: 7 -> YES 1 2 5 6 3 4 7 (or other)
Seems fine.
Now, we must consider n=100000? Not prime? Actually 100000 is composite, not 4, so NO. That's okay.
Now, we are ready.
But I want to check if there is any nuance about the prefix product sequence being a permutation of 0..n-1. In our prime construction, p_i = i for i=1..n-1, so they are exactly 1,2,...,n-1, which are all distinct and nonzero. Then p_n=0. So that is a permutation. However, the problem statement says "its prefix product sequence is a permutation of [0,1,...,n-1]". The prefix product sequence includes p_1,...,p_n. In our construction p_i = i for i<n, p_n=0. So it's a permutation. Good.
Now, check for n=4: p=[1,3,2,0] -> permutation.
Thus all good.
Now, answer.
But wait, what about n=2? p=[1,0] -> permutation of [0,1]. Good.
Now, implement.
Now, we might want to handle large n prime, and output the sequence. That's it.
Now, final code.
But before final, let's consider if we can simplify the prime construction using a different pattern: a_i = (i) * inv(i-1) mod n.
We can compute inv using fast pow.
Now, careful: When n is prime, the inverse of 1 is 1, etc.
Now, for i=2: (i-1)=1, inv=1.
Now, for i=n-1: (i-1)=n-2, compute inv.
Now, we could also compute a_i = (i * pow(i-1, n-2, n)) % n.
Now, we must be careful: i and inv are int, multiply may overflow int but we use long long.
Now, that's it.
Now, final solution.
But the problem might be from some contest, and they might expect more reasoning. But our solution is correct.
Now, I'll produce final answer with explanation.
But let me think if there's any other edge: n=1 and n=4 produce YES, but also n=2? Already prime.
Now, could n=0? not.
Now, go ahead.
Now, I'll produce the final response. ```cpp
#include
using namespace std;
typedef long long ll;
// fast exponentiation modulo mod
ll mod_pow(ll a, ll b, ll mod) {
ll res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
// primality test for n <= 1e5
bool is_prime(int n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
code
// special small cases
if (n == 1) {
cout << "YES\n1\n";
return 0;
}
if (n == 4) {
cout << "YES\n1\n3\n2\n4\n";
return 0;
}
// for primes we can construct the sequence
if (is_prime(n)) {
cout << "YES\n";
// a1 = 1
cout << "1\n";
// a2 .. a_{n-1} = i * inv(i-1) (mod n)
for (int i = 2; i <= n - 1; ++i) {
ll inv = mod_pow(i - 1, n - 2, n); // inverse modulo prime n
ll ai = (i * inv) % n;
cout << ai << '\n';
}
// last element is n
cout << n << '\n';
} else {
cout << "NO\n";
}
return 0;