After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i, j, k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
Input
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Output
Print one number — the quantity of triples (i, j, k) such that i, j and k are pairwise distinct and ai·aj·ak is minimum possible.
Examples
Input
4
1 1 1 1
Output
4
Input
5
1 3 2 3 4
Output
2
Input
6
1 3 3 1 3 2
Output
1
Note
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.
In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.
In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
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 an array of n positive integers, find the number of triples of indices (i, j, k) with i<j<k such that the product a_i * a_j * a_k is minimized. So we need to find the minimum possible product among all triples, and then count how many triples achieve that product.
Constraints: n up to 1e5, a_i up to 1e9.
We need an efficient solution.
Observing: Since all numbers are positive integers, the product is minimized when we pick the three smallest numbers. However, we must be careful: the three smallest numbers by value might not yield the minimal product if there are zeros? But numbers are positive (>=1), so smallest values yield smallest product. Indeed, with positive numbers, sorting ascending, the product of the three smallest numbers is minimal (since all products are positive). But is it always? Suppose we have numbers like 1, 100, 101 vs 2,3,4. 1100101 = 10100, 234=24, so obviously 1,100,101 product is larger than 2,3,4. Actually, the smallest three numbers by value should give minimal product because product is monotonic in each factor: if we replace a number with a larger number, product increases. However, careful: we are picking three numbers, not necessarily the three smallest by value if there are duplicates? Actually, if we sort ascending: a1 ≤ a2 ≤ a3 ≤ ... ≤ an. For any triple (i,j,k) with values x ≤ y ≤ z. If we compare to (a1,a2,a3), since a1 ≤ x, a2 ≤ y, a3 ≤ z, we have product(a1,a2,a3) ≤ product(x,y,z). So indeed the triple of smallest values (by sorted order) yields the minimal product. But what if there are more than three smallest numbers? Then the minimal product might be achieved by different combinations involving those smallest values. For example, if the three smallest values are 1,1,2 but there are multiple 1's and 2's, then any triple that consists of two 1's and one 2 (since 1,1,2 are smallest three values) would have product 2. However, could a triple of 1,1,1 have product 1? That would be smaller if there are three 1's. So the minimal product depends on the frequencies of the smallest numbers.
Thus, the problem reduces to: find the three smallest distinct values? Not exactly; we need the combination of three numbers (with repetitions allowed if multiple copies) that yields the smallest product. Since we can pick from the array with repetition of indices but distinct values? Actually values can be repeated, we have multiple copies. The minimal product among triples is determined by the sorted list. Let's denote the sorted array values (non-decreasing). The minimal product is a[0]*a[1]*a[2] where a[0] is the smallest, a[1] second smallest, a[2] third smallest (taking into account positions after sorting). However, if there are multiple copies of the smallest numbers, the minimal product might involve combinations like (a[0], a[0], a[1]) if a[0]*a[0]*a[1] < a[0]*a[1]*a[2]? But note: a[0] ≤ a[1] ≤ a[2]. Compare product P1 = a[0]*a[0]*a[1] vs P2 = a[0]*a[1]*a[2]. Since a[0] ≤ a[2], we have a[0]*a[0]*a[1] ≤ a[0]*a[1]*a[2]. Actually if a[0] < a[2] then P1 < P2, making it potentially smaller. But we must consider: can we actually pick two copies of the smallest value? Yes, if there are at least two copies of the smallest number. So the minimal product might not necessarily involve the three smallest distinct values; it could involve two copies of the smallest and the second smallest, or three copies of the smallest. So we need to find the triple (x, y, z) (not necessarily distinct values) from the multiset that yields the smallest product. Because numbers are positive, the product is minimized by using as small numbers as possible, and we can use duplicates if available.
Thus we need to consider the frequencies of the smallest values. Basically, the minimal product will be formed by the three smallest numbers in the sorted multiset. Since sorted multiset is simply the array sorted, the first three elements (sorted order) give the minimal product among all triples if we consider triples of indices? Wait careful: The triple (a[0], a[1], a[2]) after sorting corresponds to some three indices. But there could be a triple (a[0], a[0], a[1]) if a[0] appears at index 0 and at index 1 (since sorted, if there are at least two copies, then a[0] = a[1] maybe). Actually in sorted order, a[0] ≤ a[1] ≤ a[2]. If a[0] = a[1], then the triple (a[0], a[1], a[2]) is same as two copies of the smallest and a[2]? Actually if a[0] = a[1], then the triple (a[0], a[1], a[2]) already uses two copies of the smallest and the third smallest (distinct maybe). That product is a[0]a[0]a[2] (if a[1]=a[0]). If a[0] = a[1] = a[2], then triple (a[0], a[1], a[2]) uses three copies of the smallest. So indeed, the minimal product is always the product of the first three elements in the sorted array (with indices 0,1,2). Because that's the triple formed by taking the smallest possible three values in order. But wait: Could there be a triple that yields a smaller product than a[0]a[1]a[2]? Suppose the sorted array: [1, 2, 100, ...]. Then a[0]a[1]a[2] = 12100=200. Could we pick (1,1,2)? That would be 2, smaller than 200, but we need to have two copies of 1. Does the sorted array have two 1's? In my example, it has only one 1. So no. But if there are two 1's, the sorted array would be [1,1,2,100,...] then a[0]=1, a[1]=1, a[2]=2 -> product = 2. So it matches. So indeed, the product of the first three elements in sorted order is always a candidate triple. Could there be a triple that yields a product smaller than that? For it to be smaller, you would need to replace one of the three smallest values with a smaller value, but you cannot because those are the smallest. However, you might think: what if we take a[0], a[0], a[0]? That product would be a[0]^3. But in sorted order, the first three elements include three copies if there are three copies; if not, you cannot pick three copies because there might not be three copies. But if there are at least three copies, then a[0]=a[1]=a[2]=a[0], so product a[0]^3. If there are only two copies, the first three are a[0], a[0], a[2]; product = a[0]^2 * a[2]. Could a triple (a[0], a[0], a[0]) exist? No, because only two copies exist. So the minimal product is indeed the product of the first three elements in the sorted list. So the problem reduces to: sort the array, find the product P = a[0]a[1]a[2] (taking the first three after sorting). Then count the number of triples (i<j<k) in the original array such that the multiset of values {a_i, a_j, a_k} when sorted yields the same three values as the sorted first three? Actually we need to count triples whose product equals P (which is minimal). But careful: There might be other triples whose product equals P but are not exactly the same values? Could there be a different combination of values that yields the same product but not the same set of three smallest? Since all numbers are positive integers, and we're looking for minimal product, any triple achieving that product must be minimal. Could there be distinct values that also produce the same product as the minimal? For example, suppose the sorted array begins with 1,1,2,3... minimal product = 112 = 2. Could there be a triple 1,2,? 12? =2 => ?=1. So that's same set {1,1,2}. Could there be triple 1,1,1? That would be 1 if there were three 1's, but then product 1 is smaller than 2, so it would be minimal if three 1's existed. So if three 1's existed, the minimal product would be 1, not 2. So no conflict. Could there be a triple like 1,2,? where ?=1 again, so same. Could there be triple 2,? ? 2?? =2 => ?=?=1, again 1's. So all minimal product triples must consist of the smallest values as per sorted order, i.e., they are drawn from the multiset of the smallest values with multiplicities. But could there be a scenario where the product of the three smallest distinct numbers equals the product of two smallest and some other number? Suppose the array: [2,2,3,3,...] sorted: 2,2,3,3... Product of first three = 23=12. Could there be a triple 2,3,? that also gives 12? 2?=12 => ?=2, so again (2,2,3). Or 3,3,?=12/9=1.333 not integer. So no. In general, if the minimal product is P = a[0]a2, we need xz = a0a2. Since x ≥ a0, y ≥ a1 (if x = a0 then possibly y ≥ a1, but careful: because if a0 appears multiple times, the smallest value x can be a0. Actually x ≥ a0 by definition of a0 being minimum of entire array. Similarly, since y is the second smallest in the triple, but we cannot guarantee y ≥ a1? The second smallest in the triple could be also a0 if there are multiple a0's. But a1 is the second smallest in the whole array. However, if there are at least two copies of a0, then a1 = a0. So in that case a1 = a0. In any case, we have x ≥ a0, y ≥ a1? Let's examine carefully: The sorted entire array: a0 ≤ a1 ≤ a2 ≤ ... . For any triple, when we sort its three values, we have x ≤ y ≤ z. Since the entire set contains all numbers, the smallest possible value for x is a0. The smallest possible value for y, given that x may be a0, is the minimum value among the remaining elements after possibly using one a0. That could be a0 again if there are at least two a0's, else a1. So in general, for any triple, we have x ≥ a0, y ≥ a1 (but is it always y ≥ a1? Consider the case where there are at least two copies of a0. Then a1 = a0, so y ≥ a1 is trivially true because y ≥ x and x ≥ a0 = a1? Actually if there are at least two a0's, then a1 = a0, so any y is ≥ a0 = a1. If there is exactly one a0, then a1 > a0. If we pick a triple that does not include a0, then x could be a1 (the smallest we pick). Then x ≥ a1, but y could be something ≥ x. So the condition y ≥ a1 may not hold if the triple does not include a0? For triple that doesn't include a0, x could be a1, then y ≥ x = a1, so still y ≥ a1. If triple includes a0 and some other numbers, then y could be a0 (if we have two copies of a0) or a1 or larger. In all cases, I think it's true that y ≥ a1? Let's test: If there is exactly one a0, and we pick triple (a0, a2, a3). Then sorted: x = a0, y = a2. a2 ≥ a2? But a1 is the second smallest overall (which is the smallest after a0). Since a2 ≥ a1? Actually a2 ≥ a1 because sorted. So y = a2 ≥ a1. If we pick (a1, a2, a3) (no a0), then x = a1 ≥ a1, y = a2 ≥ a1. So indeed y ≥ a1 always? The only possible issue: Could y be less than a1? Only if there is a value less than a1 among the remaining after picking x. But a1 is the second smallest value in the whole array (counting multiplicities). So any value that is less than a1 would be a0, but if we already used a0 as x, and there is only one a0, then we cannot have another a0. So if x = a0 (the only a0), then the next smallest possible is a1. So y ≥ a1. If we didn't use a0, then the smallest we pick is at least a1, so y ≥ that smallest, which is ≥ a1. So indeed for any triple, we have x ≥ a0 and y ≥ a1. Also, for z, it's obviously ≥ a2? Not necessarily: z could be less than a2 if we pick triple that includes two small numbers and a number less than a2? But a2 is the third smallest overall. If we pick triple (a0, a0, a0) with three a0's, then z = a0, which could be less than a2 if a2 > a0. However, does that triple exist? Only if there are at least three a0's. In that case, a2 = a0 as well (since sorted, if there are at least three a0's, then a0 = a1 = a2 = a0). So then z = a0 = a2. If there are exactly two a0's, then a2 > a0. Then triple (a0, a0, something) will have z = something ≥ a2? Actually something could be any value, but if we want product minimal, we might pick the smallest something which is a2. So z ≥ a2? Consider a triple (a0, a0, a0) doesn't exist because only two a0's. So if we have exactly two a0's, then the smallest possible z after using both a0's is a2 (the third smallest overall). So for any triple, can we guarantee z ≥ a2? Not always: Suppose we have array: [1,2,3,4,...] a0=1,a1=2,a2=3. A triple (1,2,4) has z=4 ≥ a2=3. Another triple (1,3,4) has z=4 ≥3. Triple (2,3,4) has z=4 ≥3. Triple (1,1,? ) not possible because only one 1. But if there are three or more copies of the smallest value, then a2 = that value as well, so z = that value = a2. So overall, for any triple, we can say that when sorted, the i-th smallest element of the triple is at least the i-th smallest element of the entire array? That is true if we consider the sorted array as a multiset and we pick three elements without replacement. The "order statistics" have the property: For any sample without replacement, the k-th order statistic of the sample is at least the k-th order statistic of the population? Actually, for a population sorted x_(1) ≤ x_(2) ≤ ... ≤ x_(n), if we take a sample of size m without replacement, then the i-th smallest of the sample is at least x_(i) (the i-th smallest in population) and at most x_(n-m+i). This is a known property: the sample order statistics are bounded by the population order statistics. In particular, the minimum of the sample is at least x_(1) (actually equal to some population value, but it cannot be less than x_(1)). The second smallest of the sample is at least x_(2) because the sample cannot skip the first two smallest values both? Let's think: If we take three elements, the smallest of the three could be x_(1) if we include it; if not, the smallest could be x_(2) or larger. So the smallest of the sample is ≥ x_(1). The second smallest of the sample: Could it be less than x_(2)? Suppose we pick x_(1), x_(3), x_(4). Then sorted sample: x1, x3, x4. The second smallest is x3, which is ≥ x_(3) ≥ x_(2). So yes, it's ≥ x_(2). Could we have second smallest = x_(1)? Only if we have at least two copies of x_(1) in the sample, meaning we picked x_(1) twice, but we cannot because the array has distinct indices but values can repeat. If there are multiple copies of x_(1), then x_(2) might also be x_(1). So the statement "the i-th smallest of the sample is ≥ the i-th smallest of the population" holds when the population is considered as a multiset with multiplicities. If there are duplicates, the population's i-th order statistic is defined accordingly. So for our case, for any triple (sample of size 3), we have:
x1_sample ≥ a0 (population 1st order stat)
x2_sample ≥ a1 (population 2nd order stat)
x3_sample ≥ a2 (population 3rd order stat)
Proof: Since we are selecting without replacement, the sorted sample values are a subsequence of the sorted population values (when considering each occurrence as separate element). Indeed, if we list the population sorted as b1 ≤ b2 ≤ ... ≤ bn. Then any sample of size 3 corresponds to selecting three indices i<j<k. The sorted sample values will be b_i1, b_i2, b_i3 where i1, i2, i3 are the selected indices in sorted order. Since we are selecting three distinct indices, the smallest index we select is at least 1, so b_i1 ≥ b1. The second smallest index is at least 2, because there are at least two population elements with indices ≤ i2? Actually more direct: The sample's sorted values are exactly some three elements from the list. The smallest of them is at least b1, because b1 is the smallest element overall. The second smallest is at least b2? Is it possible that the second smallest is less than b2? Suppose we pick b1, b3, b4. Then second smallest is b3, which is ≥ b2 (since b2 ≤ b3). It could be equal to b2 only if b2=b3? Not necessarily. But it's always ≥ b2 because b2 is the second smallest overall. However, if we don't pick b1, then the smallest is say b2, and second smallest is b3, both ≥ b2. So indeed second smallest ≥ b2. For third smallest, by similar reasoning, it is at least b3 (the third smallest overall). Because among the three selected indices, the largest index is at least 3, so the value at that index is at least b3. More formally, the k-th order statistic of a sample without replacement from a finite ordered set is at least the k-th order statistic of the population. So that holds.
Thus, for any triple, product = x1x2x3 ≥ a0a1a2 = P, with equality if and only if x1 = a0, x2 = a1, x3 = a2. Because if any of the inequalities is strict, product becomes larger (since all numbers positive). However, careful: Could there be equality if, for example, x1 = a0, x2 = a1, x3 = a2 but also other combinations where some values are different but product still equals P? Since product is monotonic, if any factor is larger, to keep product equal, some other factor must be smaller, but cannot be smaller than the population order statistic bound. So the only way to have product = P is to have x1 = a0, x2 = a1, x3 = a2. This is strict because if x1 > a0, then even if x2 = a1 and x3 = a2, product > P. If x1 = a0 but x2 > a1, then product > P unless x3 < a2, but x3 cannot be < a2 because it's bounded below by a2. So indeed equality forces all three to match the three smallest population order statistics.
Thus the problem reduces to: find the counts of the three smallest values in the array (with multiplicities, considering order statistics). Specifically, let the sorted array be s[0..n-1]. Let the three smallest distinct values? Actually we need the values v1 = s[0], v2 = s[1], v3 = s[2]. But note that v1, v2, v3 may have duplicates; e.g., s[0]=s[1]=s[2]=x, then all three are x. In general, we need to count the number of triples (i<j<k) such that the multiset of values is exactly {v1, v2, v3} in terms of order statistics. However, we must be careful: Since indices matter, we need to count distinct index triples. The condition for a triple to have product = P is that when we sort its three values, they equal (v1, v2, v3). But note that v1, v2, v3 are not necessarily distinct; they are the first three values in the sorted list.
Thus the counting problem: given the frequencies of numbers, specifically we care about the frequencies of the smallest values. Let the sorted list of all numbers. Define:
Let f1 = number of occurrences of the smallest value (value = s[0]).
Let f2 = number of occurrences of the second smallest distinct value? Actually we need to determine counts of values that appear in positions 0,1,2. But we can compute frequencies for the three order statistics.
We can compute the counts of the three values that appear at positions 0,1,2 in sorted order. Since sorted order includes duplicates, we can simply take first three elements. There are three cases based on the distinctness:
Case A: All three values are the same: i.e., the smallest value appears at least 3 times. Then v1 = v2 = v3 = x. Then the minimal product is x^3. The valid triples are any three indices whose values are all x (i.e., all three numbers are the smallest value). So the number of triples = C(f1, 3) where f1 is count of x.
Case B: The first two values are the same, but the third is different: i.e., the smallest value appears at least twice, and the second smallest distinct value appears at least once. In sorted order: s[0]=x, s[1]=x, s[2]=y with y > x. Then minimal product = xxy. Valid triples must consist of two x's and one y. So count = C(f_x, 2) * f_y, where f_x = count of x, f_y = count of y (but careful: y is the value at position 2, which might have multiple occurrences. However, we need exactly one y and two x's. But could there be a triple with two x's and one y where y is not exactly the second smallest distinct value? Actually y is the third smallest element overall. But there could be other occurrences of y? If y appears multiple times, all those y's are valid as the third element because they all have value y, which is the third smallest. But we must ensure that the third smallest value is indeed y; that is, any occurrence of value y is acceptable because it's the same value. However, what if there is another value z that is also equal to y? That would mean y appears multiple times. So it's fine. So count = C(f_x,2) * f_y.
But also need to consider the possibility that the first value is x, second is y, third is also y? That is, s[0]=x, s[1]=y, s[2]=y, with y > x. That's a case where the smallest appears once, and the next value appears at least twice. Then minimal product = xyy. Valid triples are one x and two y's. Count = f_x * C(f_y,2). This falls under a different pattern: first two distinct? Actually it's the case where first and second are different but second and third are same. So we need to treat separately.
Case C: All three values are distinct: s[0]=x, s[1]=y, s[2]=z with x<y<z. Then minimal product = xyz. Valid triples must have one x, one y, one z. Count = f_x * f_y * f_z.
But note: There might be scenario where the first and second are same, and third also same? That's case A. Also scenario where first is x, second is y, third is y (as above). So we need to examine all possibilities based on the frequencies of the smallest values.
Essentially, we need to determine the three order statistics from the multiset. Let’s define after sorting: we look at the first three elements. We can count how many of these three are equal to the smallest, second smallest, etc. But more systematically: we can compute frequencies of the distinct values in ascending order. Let the distinct values sorted: v1 < v2 < v3 < ... . Let cnt1, cnt2, cnt3, ... be their frequencies. Then the sorted list begins with cnt1 copies of v1, then cnt2 copies of v2, etc. The first three positions will fall within these groups.
We want the product of the three smallest numbers and count of triples achieving that product.
We can compute:
Let total taken from first group (v1): t1 = number of occurrences of v1 among the first three positions. Similarly, from second group (v2): t2 = number of occurrences of v2 among first three, and possibly from third group if needed.
But careful: The minimal product is the product of the three smallest numbers. The triples that achieve it must consist of exactly the multiset of those three numbers. That means we must select t1 copies of v1, t2 copies of v2, and t3 copies of v3 (where t1+t2+t3=3) exactly matching the counts in the first three positions. However, if the first three positions include values from v1 and v2 only (i.e., cnt1 >=3, then t1=3, t2=0). If cnt1=2 and cnt2>=1, then first three are two v1 and one v2 (t1=2,t2=1). If cnt1=1 and cnt2>=2, then first three are one v1 and two v2 (t1=1,t2=2). If cnt1=1 and cnt2=1, then we need a third value from next distinct value v3: first three are one v1, one v2, one v3 (t1=1,t2=1,t3=1). If cnt1=1 and cnt2=1? That's the distinct case. If cnt1=1 and cnt2>=1 but cnt2=1? Actually if cnt1=1 and cnt2=1, we need to go to v3. So yes.
Thus, the counting formula is:
Let a = frequency of the smallest value (value = v1)
Let b = frequency of the second smallest distinct value (v2) if exists; otherwise 0.
Let c = frequency of the third smallest distinct value (v3) if exists; else 0.
But we need to consider scenarios where we may not have three distinct values (i.e., n may have only one distinct value, but n>=3, so at least one value repeated enough). But we need to handle edge cases: if the smallest value appears at least 3 times, then answer = C(a,3). If the smallest appears exactly 2 times, then we need one more from the next smallest value. So answer = C(a,2) * b. But careful: if a=2 and b might be >=1. Also if a=2 and b could be any number, that's fine.
If the smallest appears exactly 1 time, then we look at the second smallest value. If second smallest appears at least 2 times, then answer = a * C(b,2) (since a=1, so effectively = C(b,2)). If second smallest appears exactly 1 time, then we need the third smallest value: answer = a * b * c ( = 1 * 1 * c = c) but careful: a=1, b=1, c could be any frequency. Actually if a=1, b=1, then we need exactly one from v1, one from v2, one from v3. So answer = a * b * c = 1 * 1 * c = c. But is that correct? Since we need to choose one index from each group. Yes, count = cnt1 * cnt2 * cnt3.
But note: Could there be a scenario where a=1, b>=2? Then the first three positions: after the one v1, the next two are both v2 (since cnt2>=2). So minimal product = v1 * v2 * v2. Valid triples: one v1 and two v2. Count = cnt1 * C(cnt2, 2) = C(cnt2,2) because cnt1=1. But if cnt2 is large, that's fine.
Also scenario where a=2, b=1, but there may be more v2? Actually if a=2 and b>=1, then first three are two v1 and the smallest v2 (the one at position 2). But if there are multiple v2, all v2 are valid as the third element because they all have same value v2. So count = C(a,2) * b. That's correct.
What about a=2, b>=2? Still first three are two v1 and the smallest v2 (position 2). But product is v1v1v2. All triples consisting of two v1 and any v2 will have product v1^2 * v2. But note: If we pick two v1 and a v2 that is not the smallest v2? All v2 are equal value, so product same. So all v2 are valid. So count = C(a,2) * b.
Similarly for a=1, b>=2: any two v2 among the b will yield product v1 * v2 * v2. So count = a * C(b,2) = C(b,2).
But careful: What if a=0? Impossible because n>=3, there is at least one element.
Also scenario where there are more than three distinct values but a>=3? Then answer = C(a,3). That's fine.
Edge: a=2, b=1, but there might be also v3? That's fine; the third smallest is v2, not v3. So we only use v2.
Now, is there any possibility that the minimal product could be achieved by a triple that does not consist of exactly t1 copies of v1, t2 of v2, etc. as derived? According to the order statistics argument, we concluded that for a triple to have product = P, its sorted values must equal (v1, v2, v3) where these are the three smallest order statistics. That triple's values are exactly those three numbers, regardless of whether there are other occurrences of those values. For example, if a=2, b=2 (v1 appears twice, v2 appears twice). The sorted first three: positions: v1, v1, v2. So (v1, v1, v2). So valid triples must have two v1 and one v2. Could there be a triple with one v1 and two v2? Sorted: v1, v2, v2. That product is v1 * v2^2. Since v1 < v2, v1 * v2^2 > v1^2 * v2? Not necessarily, but let's compare: v1^2 * v2 vs v1 * v2^2. Divide both by v1v2 (positive): first becomes v1, second becomes v2. Since v1<v2, v1^2v2 is smaller. So product of (v1, v2, v2) is larger. So not minimal. So indeed only the exact composition matches.
Therefore the counting logic is correct.
But wait: Could there be a scenario where v1, v2, v3 are not the three smallest distinct values? Actually they are the three smallest values with multiplicities. So we've covered.
Thus algorithm:
Read n and array a.
Sort array a (or we could avoid full sort if we only need frequencies of the three smallest values? Since n up to 1e5, sorting is fine O(n log n). But we need frequencies of the smallest values. Could do partial selection (like nth_element) but sorting is simplest.
Determine the values and frequencies of the three smallest order statistics. Since the array is sorted, we can scan the sorted list to extract the first three values and counts.
Simplest: After sorting, let x = a[0], y = a[1], z = a[2]. Then we need counts of these three values in the entire array. But careful: If x, y, z are not all distinct, we need to know counts of each distinct value. For example, if x=y=z, we need count of that value. If x=y<z, we need count of x and count of z. If x<y=z, we need count of x and count of y. If all distinct, we need counts of x, y, z.
But we must ensure that we count correctly: In case x=y<z, the count of y is the same as count of x? Actually y is same value as x, so the frequency of that value is the count of all occurrences of that value, which we can compute from sorted array by counting how many of that value exist. Similarly for z. So we can compute frequencies by iterating over sorted array and grouping.
Implementation plan:
Sort the array.
Let val1 = a[0].
Count cnt1 = number of times val1 appears (by scanning from start until value changes).
If cnt1 >= 3: answer = C(cnt1, 3) (choose 3 from cnt1).
Else if cnt1 == 2:
The next value after the two val1's is val2 = a[2] (since index 2 is third element). Count cnt2 = number of occurrences of val2 (starting from index 2 to as long as equals val2). answer = C(cnt1,2) * cnt2.
Else if cnt1 == 1:
Then we need to find the second smallest distinct value. So after skipping the first element (val1), we look at a[1] = val2. Count cnt2 = number of occurrences of val2 starting from index 1.
If cnt2 >= 2: Then the third smallest is also val2 (since we have at least two val2). Then answer = cnt1 * C(cnt2, 2) = C(cnt2, 2) (since cnt1=1).
Else if cnt2 == 1: Then we need the third smallest distinct value: val3 = a[2]. Count cnt3 = number of occurrences of val3 starting from index 2. answer = cnt1 * cnt2 * cnt3 = 1 * 1 * cnt3 = cnt3.
Return answer.
But is there any scenario where cnt1 == 0? No.
We must also consider when n may be exactly 3? That's fine.
Edge Cases: What if cnt1 = 2 but there are no other values? n must be at least 3, but if n=3 and cnt1=2, then there must be a third element which is val2, so cnt2 at least 1. That's fine.
What if cnt1 = 1 and cnt2 = 1 and there is only two distinct values? But n>=3, so there must be a third element; if only two distinct values, then cnt2 must be at least 2 because total n >=3 and we have one of val1, one of val2? Actually if only two distinct values, and cnt1=1, then the rest are val2, so cnt2 = n-1 >= 2. So cnt2 >=2, so we would have taken the branch cnt2>=2. So the cnt2 == 1 branch only occurs when there are at least three distinct values? Actually cnt2==1 implies that after one val1 and one val2, there is at least one val3 because n>=3. So that branch is valid.
Thus algorithm is straightforward.
But wait, we need to be careful: In case cnt1 == 2, we compute cnt2 as the frequency of the value at a[2]. But what if there are more than one distinct value after the two val1? The third element is val2. The frequency cnt2 includes all occurrences of that value, which might be more than 1. So answer = C(2,2)*cnt2 = 1 * cnt2. That matches.
In case cnt1 == 1 and cnt2 >= 2, we compute cnt2 as the frequency of the second smallest value. But careful: If cnt1 == 1 and there are multiple values equal to val2, then the third smallest value is also val2. So indeed the product = val1 * val2 * val2. And the valid triples are one val1 and two val2. Count = cnt1 * C(cnt2,2) = C(cnt2,2). However, could there be a triple consisting of one val1, one val2, and one val3 (where val3 is larger) that also yields same product? No, because product would be larger.
But is it possible that val1 * val2 * val2 equals val1 * val2 * val3 for some val3 > val2? That would require val2 = val3, which contradicts val3 > val2. So no.
Thus answer is correct.
Now we need to output the number of triples as an integer. Possibly large: n up to 1e5, maximum number of triples could be C(1e5,3) ~ 1.666e14, which fits in 64-bit (less than 2^63-1 ~9.22e18). So we can use 64-bit (long long in C++).
Now we should test with examples.
Example 1: 4 ones: sorted [1,1,1,1]. cnt1 = 4 (since all are 1). cnt1 >=3, answer = C(4,3)=4. matches.
Example 2: [1,3,2,3,4] sorted = [1,2,3,3,4]. cnt1=1 (value 1). Then next val2 = a[1]=2, cnt2? How many 2's? Only one. So cnt2=1. Then val3 = a[2]=3, cnt3 = number of 3's? There are two 3's. So answer = 11cnt3 = 2. matches.
Example 3: [1,3,3,1,3,2] sorted = [1,1,2,3,3,3]. cnt1=2? Actually first element 1, second 1, so cnt1=2 (since there are two 1's). Then val2 = a[2]=2, cnt2 = number of 2's? only one. So answer = C(2,2)cnt2 = 11 =1. matches.
Thus algorithm passes examples.
But wait, there is nuance: In Example 3, the array: 1,3,3,1,3,2 => sorted: 1,1,2,3,3,3. Indeed first three: 1,1,2. So cnt1=2, cnt2=1. Answer=1. Good.
Now confirm some other cases:
Case: [2,2,2,2] => cnt1=4, answer=C(4,3)=4.
Case: [1,2,2,2] => sorted:1,2,2,2. cnt1=1, val2=2, cnt2=3. Since cnt2>=2, answer = C(3,2)=3. Check: triples: indices (1st, any two of the three 2's) => 3 triples. Minimal product=122=4. Could there be triple (2,2,2) product=8 >4, so not minimal. So correct.
Case: [1,1,2,2] => sorted:1,1,2,2. cnt1=2, val2=2, cnt2=2. answer=C(2,2)cnt2=12=2. Check triples: need two 1's and one 2. There are C(2,2)=1 way to choose the two 1's, and 2 ways to choose a 2 => 2 triples. Minimal product=112=2. Triples with one 1 and two 2's product=122=4 >2. So correct.
Case: [1,2,3,3] => sorted:1,2,3,3. cnt1=1, val2=2, cnt2=1, val3=3, cnt3=2 => answer=2. triples: (1,2,3) with two choices for the 3 => 2. product=6. triple (1,3,3) product=9 >6. So correct.
Case: [2,2,3,3] => sorted:2,2,3,3. cnt1=2, val2=3? Wait, first three: 2,2,3. So cnt1=2 (value 2), val2=3 (third element), cnt2 = number of 3's =2. answer = C(2,2)cnt2 = 12 =2. Check triples: need two 2's and one 3. There is exactly one way to pick the two 2's (since there are only two 2's) and two ways to pick a 3 => 2. Product = 223=12. Could there be triple (2,3,3) product=18 >12. Good.
Now what about case: [1,1,1,1,2] => sorted:1,1,1,1,2. cnt1=4 >=3 => answer=C(4,3)=4. Indeed, any three 1's product=1. triple (1,1,2) product=2 >1, so only triples of three 1's count. Good.
Now consider case: [1,1,2,2,2] => sorted:1,1,2,2,2. cnt1=2, val2=2, cnt2=3 => answer = C(2,2)3 = 3. Triples: two 1's (only one combination) and any of three 2's => 3. Minimal product=11*2=2. Check triple (1,2,2) product=4 >2, triple (2,2,2)=8. So correct.
Now case: [1,2,2,2,2] => sorted:1,2,2,2,2. cnt1=1, val2=2, cnt2=4 => cnt2>=2 => answer=C(4,2)=6. Triples: one 1 and any two 2's => 6. product=122=4. triple (2,2,2) product=8 >4. So correct.
All seems consistent.
Thus algorithm is correct.
But wait, the product being minimum possible, we are using the property that product of the three smallest order statistics is minimal. But is there any scenario where using a zero would change this? The problem says positive integers, so no zeros. All >=1.
Now, we need to consider if the product might overflow 64-bit when computing? Actually we don't need to compute product except maybe to verify? But we don't need to compute product at all. We just need counts. So no overflow issue.
Now, what about the case where the first three values are like: a[0] = 1, a[1]=1, a[2]=1? Then answer C(cnt1,3). That's fine.
Now, check for extremely large n up to 1e5, and counts large, computing combinations: C(cnt, 3) = cnt*(cnt-1)(cnt-2)/6. Use 64-bit. C(cnt,2)=cnt(cnt-1)/2. No overflow issues because cnt up to 1e5, multiplication up to ~1e15, fits in 64-bit.
Now, is there any scenario where the minimal product could be achieved by a triple that does not correspond to the three smallest order statistics because of multiplication overflow? No, we are dealing with mathematics, not computer arithmetic.
Thus solution is simple.
But the problem statement says "ai·aj·ak is minimum possible". They ask for number of triples (i,j,k) with i<j<k such that product is minimum. So we need to count index triples, not value combinations. Our counting via frequencies accounts for distinct indices.
Implementation steps:
Read n.
Read array a.
Sort a.
Determine cnt1 = count of a[0] in whole array. Since sorted, we can use while loop or upper_bound.
If cnt1 >= 3: answer = C(cnt1, 3)
Else if cnt1 == 2:
// the third element is a[2]; find its frequency
val2 = a[2];
cnt2 = count of val2 in whole array (or from index 2 onward, but easier to count all occurrences; we can use binary search because sorted).
answer = C(2,2) * cnt2 = cnt2.
Else (cnt1 == 1):
// find second distinct value and its count
// the second element is a[1]
val2 = a[1];
cnt2 = count of val2 in whole array.
if cnt2 >= 2:
answer = C(cnt2, 2) // since cnt1=1
else: // cnt2 == 1
// then third distinct value
val3 = a[2];
cnt3 = count of val3 in whole array.
answer = cnt3 // because cnt1cnt2cnt3 = 11cnt3 = cnt3.
But careful: In the cnt1 == 1 and cnt2 >= 2 case, do we need to ensure that the third smallest is indeed val2? That is automatically true because cnt2 >= 2, so after the first 1, the next two are val2, so a[1] and a[2] are both val2. So yes.
Edge: What if cnt1 == 1 and cnt2 == 1, but there are multiple of val3? That's handled.
One more nuance: In the cnt1 == 2 case, we assumed that the second smallest distinct value appears at least once. That's guaranteed because n >= 3 and cnt1=2, so there is at least one more element, which could be equal to val1? If cnt1=2 but there might be a third element also equal to val1 if there were more than 2? But cnt1 is defined as the total count of val1. If cnt1 == 2, that means exactly 2 occurrences of val1. So the third element must be > val1. So val2 = a[2] is indeed the second smallest distinct value. But we also need to count all occurrences of that value. So that's correct.
Now confirm with some custom case: a = [5,5,6,6,6]. Sorted: 5,5,6,6,6. cnt1=2, val2 = a[2]=6, cnt2 = 3. answer = 13 = 3. Triples: (5,5,6) with 3 choices for 6 => 3. product=55*6=150. Check triple (5,6,6) product=180 >150, (6,6,6)=216 >150. So correct.
Now case: a = [10,10,10,20]? cnt1=3, answer = C(3,3)=1. product=1000. triple (10,10,20)=2000 >1000. So correct.
Now case: a = [1,1,1,1,2,2]? cnt1=4 >=3, answer=C(4,3)=4. triple of three 1's product=1; any triple with a 2 product=2 or more. So only triples of three 1's. There are 4 choose 3 =4. Good.
Now, one more: a = [1,1,2,2,2,3]? sorted:1,1,2,2,2,3. cnt1=2, val2=a[2]=2, cnt2=3, answer = 13 =3. Triples: two 1's and one 2 => exactly 3. Product=4? Actually 11*2=2. (1,2,2) product=4, (1,2,3)=6, (2,2,2)=8. So minimal=2, count=3. Good.
Now, what about a = [1,2,2,3,3]? sorted:1,2,2,3,3. cnt1=1, val2=2, cnt2=2, answer = C(2,2)=1? Actually C(2,2)=1? Wait C(2,2)=1 but we need count of triples: one 1 and two 2's => 11 =1? But there are two 2's, choose both =1, times one 1 =1. Indeed only one triple (1, first 2, second 2). product=12*2=4. Check if there is triple (1,2,3) product=6, (2,2,3)=12, (2,3,3)=18. So minimal=4, count=1. Good.
But what about triple (1,2,3) gives product 6, not minimal. So correct.
Now, consider a = [1,2,3,3,3] sorted:1,2,3,3,3. cnt1=1, val2=2, cnt2=1, val3=3, cnt3=3, answer = 113 = 3. Triples: (1,2,3) with three choices for 3 => 3. product=6. Could there be triple (1,3,3) product=9 >6. So correct.
All good.
Now, is there any scenario where the three smallest order statistics are not simply the first three elements? Actually, by definition they are. So it's fine.
Thus algorithm works.
But wait: Could there be a scenario where the product is minimized by a triple with values (a0, a0, a0) even if a0 appears only twice? No, impossible because we cannot pick three copies if only two exist.
Thus we have solved.
But the problem may have a trick: The array can be large, but we only need to count triples, not compute product. Our method uses sorting O(n log n). Acceptable.
But we need to be careful about integer types: Use long long for answer.
Now we can produce final solution.
But before finalizing, let's think if there is any hidden nuance: The condition i<j<k is about indices, not values. Our counting using combinations from frequencies counts the number of ways to choose indices with given values. Since indices are distinct, and we don't care about order (i<j<k), the combinations count unordered triples. However, our counting formulas (C(cnt,3), C(cnt1,2)*cnt2, etc.) count the number of unordered triples of distinct indices with those values, because we are selecting a set of indices from the positions of each value group. And since we treat each occurrence as distinct, and we are selecting combinations of indices, and then we consider triples as sets of indices, and the ordering i<j<k is automatically imposed by the indices. However, does C(cnt,3) count the number of unordered triples? Yes, because we choose any 3 indices from the set of cnt indices where the value is x, and then sorting those indices gives i<j<k. So the count is correct.
For mixed groups: cnt1 * cnt2 * cnt3 counts the number of ways to pick one index from group1, one from group2, one from group3. Since the groups are disjoint sets of indices, the product counts the number of unordered selections of one from each group. However, does this product overcount ordering? Because we are selecting three distinct indices, but we haven't imposed i<j<k ordering. When we count by product of counts, each triple of indices (i, j, k) is counted exactly once, regardless of the order of selection from groups? Actually, if we choose index i from group1, j from group2, k from group3, that yields a specific set of three indices. Since groups are disjoint, each such triple is unique, and there is exactly one way to assign which index comes from which group because values are distinct. But the triple {i,j,k} will have a natural order (i<j<k) if we consider indices sorted. However, does the product cnt1cnt2cnt3 count each unordered triple exactly once? Yes, because for each choice of an index from group1, one from group2, one from group3, we get a specific unordered triple. Since the groups are defined by value, and we require exactly one from each, there's no duplication (like if groups had overlapping values, but they are distinct values, so groups are disjoint). So it's fine.
For cnt1=2, cnt2 * C(2,2) = cnt2: we pick two indices from group1 (only one way if cnt1=2) and one from group2. That yields a unique triple. Similarly for cnt1=1, C(cnt2,2): pick one index from group1 (only one choice) and two indices from group2. That yields unique triples.
Thus counting formulas are correct.
Potential pitfalls: When cnt1=2, but there might be also additional copies of val1? No, because cnt1 is total count; if cnt1=2, there are exactly 2 copies of val1. So C(2,2)=1.
When cnt1=1 and cnt2>=2, we computed answer = C(cnt2,2). However, what if there are also other values equal to val2? cnt2 already counts all. So fine.
Now, we should ensure that we correctly compute counts. The straightforward way: after sorting, we can just iterate to get counts of the first three values. But careful: In case where cnt1=1 and cnt2>=2, we need count of second value (val2). However, if cnt2 >= 2, the third smallest value is also val2. So product = val1 * val2 * val2. But does the count of triples involve any restrictions like that the two val2 must be distinct indices, which they are, and we choose any two from the cnt2 occurrences. That's C(cnt2,2). That's fine.
But what about scenario where cnt1=1 and cnt2 is exactly 1? Then we need count of third value (val3). That's product = val1 * val2 * val3. Count = cnt1cnt2cnt3 = cnt3.
But wait: Could there be scenario where cnt1=1 and cnt2=1 and cnt3>=1, but there might be more than one distinct value after that? But we only need val3 frequency. It's correct.
Now, the only nuance: In cnt1=1, cnt2=1 case, we assumed that the third smallest value is the value at index 2. But if cnt2=1, then index 2 is the first occurrence of the third distinct value. So val3 = a[2]. Its frequency cnt3 is the number of occurrences of that value. But is it possible that after index 2 there are other values that are also equal to val3? That's included in cnt3. So fine.
Now, check if there is a scenario where cnt1=1, cnt2=1, but there is also a fourth distinct value that is equal to val2? No, cnt2=1 means exactly one occurrence of val2, so no duplicates.
Now, let's think: Could there be a scenario where the minimal product is not unique? Actually we count all triples achieving minimal product. So our counting should include all such triples.
Now, one more subtlety: What about negative numbers? The problem says positive integers, so no negatives.
Thus, solution is final.
But wait: Could there be an edge case where the three smallest values are such that the product might overflow 64-bit? Not relevant for counting.
Implementation details:
We can sort the array. Then we can compute frequencies by using loops.
Pseudo-code:
function solve():
read n
read array a of size n
sort a
let cnt1 = 1
i = 1
while i < n and a[i] == a[0]:
cnt1++, i++
if cnt1 >= 3:
ans = cnt1*(cnt1-1)(cnt1-2)//6
elif cnt1 == 2:
// third element is at index 2
val2 = a[2]
// count occurrences of val2
// we can do binary search for upper bound - lower bound using sorted array
cnt2 = upper_bound(a, val2) - lower_bound(a, val2)
// but since we have index i already at 2? Actually after counting cnt1, i = 2? If cnt1=2, then we scanned indices 0 and 1, so i = 2. So we can start from i=2 to count cnt2.
// simpler: count from index 2 while a[j] == val2; but there could be more after that. So we can binary search.
// or we can just loop from index 2 to n-1, but worst-case O(n) which is fine? n up to 1e5, looping from 2 to n-1 is O(n). That's acceptable. But we could also use binary search for elegance.
// However, note that we may already have i=2, we can count cnt2 by while loop from i while a[i]==val2.
// But after we counted cnt1, i is at first index where a[i] != a[0]. Since cnt1=2, i=2. So we can set j=i and count cnt2.
j = i
while j < n and a[j] == val2:
j++
cnt2 = j - i
ans = cnt2 // because C(2,2)=1
else: // cnt1 == 1
// next value at index 1
val2 = a[1]
// count occurrences of val2
// we already have i=1? Actually after counting cnt1, we did while loop: starting i=1, while a[i]==a[0]; but cnt1=1 so we didn't increment i, i remains 1.
// So we can count cnt2 from index 1.
j = 1
while j < n and a[j] == val2:
j++
cnt2 = j - 1 // because starting at 1
if cnt2 >= 2:
ans = cnt2(cnt2-1)//2
else: // cnt2 == 1
// third value at index 2
val3 = a[2] // note index 2 is after the first two distinct values
// count occurrences of val3
// start from index 2
k = 2
while k < n and a[k] == val3:
k++
cnt3 = k - 2
ans = cnt3
Return ans.
But careful: In cnt1 == 2 case, when we counted cnt1, we set i = 2 (since we incremented i until a[i] != a[0]). So at i=2, we have the first element not equal to a[0]. That is val2. So we can count cnt2 from i. However, what if there are multiple values after i? Our while loop will count only consecutive identical to a[i]. Since the array is sorted, all occurrences of val2 are contiguous. So we can count cnt2 = count from i while a[index] == val2. That yields total count of val2. Good.
In cnt1 == 1 case: after counting cnt1, we have i=1 (since while loop condition fails immediately because a[1] != a[0] presumably, unless there were more ones but cnt1=1 means only one a[0], so a[1] is different). So i=1. Then we count cnt2: from index i to as long as equal to a[i] which is val2. Since array sorted, that counts all val2 occurrences. Good.
In cnt1 == 1 and cnt2 == 1 case: after counting cnt2, j will be index 2 (since only one val2). So we set k = 2 to count cnt3. That's fine.
Thus this approach is O(n) after sort.
But we must ensure that when counting cnt2 or cnt3, we don't exceed array bounds, but it's fine.
Edge: if cnt1==1 and cnt2==1, and there is only one val3 (cnt3=1), then n=3, ans = 1. Works.
Now confirm with large counts: all fine.
But note: In cnt1 == 1 and cnt2 >= 2, we compute ans = C(cnt2,2). However, we must ensure that we are not missing that the two val2 can be any two among cnt2. That's correct.
Now, what about case: cnt1 == 1 and cnt2 == 2? Then ans = C(2,2)=1. That's correct as earlier example.
Now, what about case: cnt1 == 1 and cnt2 > 2, ans = C(cnt2,2). Example earlier with cnt2=4 gave 6. Good.
Now, the answer may be up to about 1.6e14, fits in 64-bit.
Thus the solution is done.
But hold on: I want to double-check the fundamental claim: product of the three smallest order statistics is the minimal product among all triples. This is true for positive numbers. However, is it always true if numbers are as low as 1? Yes, product increases with any increase in any factor. So picking the three smallest numbers (with multiplicities) indeed yields minimal product.
Proof: For any triple (i,j,k), sort values: x≤y≤z. Compare to the sorted list b1≤b2≤b3... For any such triple, we have x≥b1, y≥b2, z≥b3. So product ≥ b1b2b3. Equality iff x=b1, y=b2, z=b3.
Thus problem solved.
Now, potential pitfalls: The problem statement says "pairwise distinct" indices i, j, k (i < j < k). That we have accounted.
Now, is there any scenario where there is a tie for minimal product with different compositions? Could it be that b1b2b3 = b1b1b4 if b2=b1? But if b1 appears at least twice, then b2 = b1, and b3 is the next distinct. b1b1b4 vs b1b1b3. Since b3 ≤ b4, product b1b1b3 ≤ b1b1b4. So minimal is b1b1b3. No tie unless b3 = b4 which would mean b3 and b4 are same value, so it's the same composition. Could there be a scenario where b1, b2, b3 are distinct and b1b2b3 = b1b1b4? That would imply b2b3 = b1b4. Since b1 ≤ b2 ≤ b3, and b4 ≥ b3, it's unlikely but possible if numbers are not distinct? Example: b1=1, b2=2, b3=3, b4=6? 123=6, 116=6. So product equal! So indeed, it's possible that the minimal product (which is 6) could be achieved by two different compositions: (1,2,3) and (1,1,6). But is (1,1,6) a valid triple? It would use two ones and a six. But if the array contains 1,1,2,3,6... The sorted first three are 1,1,2 (since 1,1,2,3,6). The product of first three is 112=2, which is smaller than 6. So minimal product would be 2, not 6. So for (1,1,6) to be a candidate for minimal product, we need that there are at least two 1's and at least one 6, but also that there is no smaller combination. For (1,1,6) to be minimal, we need that there is no 2 or any number smaller than 6 that could replace the 6 to produce a smaller product? Actually the minimal product would be determined by the three smallest numbers. If the three smallest numbers are 1,1,2 (if there is a 2), then product=2 <6. So for (1,1,6) to be minimal, the third smallest must be 6, i.e., there must be no number between 1 and 6 besides possibly more 1's. That means after the two 1's, the next smallest number is 6. So the array would have at least two 1's, then 6, maybe other numbers >=6. So sorted: 1,1,6,... Then the product of the first three is 116=6. Now consider triple (1,2,3) doesn't exist because there is no 2 or 3. So the only triples that could yield product 6 are those using two 1's and a 6. But could there be another triple with same product? For instance, (1,2,3) not available. (1,1,6) is the only. So no tie.
But could there be a scenario where the three smallest numbers are 1,2,3 and also there is a 6 and another 1? That is, array: 1,1,2,3,6,... The three smallest numbers are 1,1,2 (since two 1's then 2). So product=2, not 6. So the triple (1,2,3) yields product 6 but that's not minimal because there is smaller product 2. So not tie.
For a tie to occur, the product of the three smallest numbers must equal the product of some other combination also using three numbers that are not the three smallest. That would require that replacing one of the three smallest numbers with a larger number and simultaneously replacing another with a smaller number (but we cannot get smaller than smallest) to keep product same. Since we cannot get smaller than the smallest values, the only way for product to stay same is if some of the smallest numbers are repeated in such a way that the set of three smallest numbers is ambiguous? For example, if the sorted list begins with 1,1,1,... then the three smallest numbers are all 1. Any triple of three 1's yields product 1. Could there be a triple that yields product 1 but includes a 2? No, because 2 >1, product would be at least 112=2 >1. So no.
What about sorted: 1,2,2,4? The three smallest: 1,2,2 product=4. Could a triple (1,1,4) also yield 4? But there is no second 1. So no.
Consider sorted: 1,2,3,6? Three smallest product = 6. Could (1,1,6) yield 6? Need second 1 not present. Could (2,2,? ) need ?=1.5 not integer.
Thus, it seems unlikely that different compositions yield the same minimal product unless there is some coincidence with duplicate values and ordering of values that produce same product but with different values. Let's try to construct: We need three smallest numbers (call them x ≤ y ≤ z) such that there exists another triple (x', y', z') with sorted values not equal to (x,y,z) but product equal to xyz. Since product is minimal, any other triple must have at least one element larger than z. To keep product equal, at least one element must be smaller than something, but the smallest possible is x. So maybe x' = x, y' = something larger than y, z' = something smaller than z? But z' cannot be smaller than z because z is the third smallest overall. The only way is if there are duplicates of x and y such that the three smallest are not uniquely defined? For instance, if the sorted list has multiple values equal to the third smallest? Actually the three smallest are defined as the values at positions 0,1,2. If there are multiple values equal to the third smallest, then any triple that includes two of those third smallest and something else? Let's think.
Suppose array: [1,2,2,2]. Sorted: 1,2,2,2. Three smallest: 1,2,2 product=4. Another triple: (2,2,2) product=8 >4. So no tie.
Suppose array: [1,1,2,2]. Three smallest: 1,1,2 product=2. Another triple: (1,2,2) product=4 >2. No tie.
Suppose array: [1,2,4,8]? Three smallest: 1,2,4 product=8. Another triple: (1,1,8) but no second 1. (2,2,2) no.
Suppose array: [2,2,2,4]? Three smallest: 2,2,2 product=8. Another triple: (2,2,4)=16 >8.
What about [2,3,6,?] 2,3,6 product=36. Another triple: (3,3,4) no.
It appears that due to ordering, the minimal product is uniquely determined by the three smallest order statistics. Could there be a scenario where the three smallest are not unique because of ties? For example, if there are three values that are the smallest but there are multiple copies and the exact set of three indices can be chosen in many ways, but all those triples have the same multiset of values (e.g., all three are the smallest value). That's not a different composition; it's the same composition but different indices. That's exactly what we count.
Thus, there is no scenario where two different multisets of values yield the same minimal product, because if such existed, then one of them would have a value larger than the third smallest, and to compensate, another would have to be smaller than the corresponding order statistic, which is impossible. So the counting based on the three smallest values is correct.
Hence solution stands.
Now let's consider if the array can have values up to 1e9, product may be huge but we don't compute it.
Now, one more nuance: The problem says "pairwise distinct" indices, not values. So fine.
Thus we can implement.
However, the problem originally may have intended a different interpretation: maybe they consider triples (i, j, k) such that i<j<k and aiajak is minimum. That's exactly as we did.
Let's generate a random array and compute brute force to ensure correctness. Not needed here but we can mentally test few.
Case: n=5, a=[2,2,1,1,3] sorted:1,1,2,2,3. cnt1=2, val2=a[2]=2, cnt2? There are two 2's. So answer= C(2,2)cnt2 = 12 =2. Brute force: indices: (1,3,? ) Actually original indices: let's assign indices: i1=1:2, i2=2:2, i3=3:1, i4=4:1, i5=5:3. Triples (i<j<k) with minimal product? Compute all triples: list products:
(1,2,3):221=4
(1,2,4):221=4
(1,2,5):223=12
(1,3,4):211=2
(1,3,5):213=6
(1,4,5):213=6
(2,3,4):211=2
(2,3,5):213=6
(2,4,5):213=6
(3,4,5):113=3
Minimal product =2. Triples achieving 2: (1,3,4) and (2,3,4) => 2 triples. matches answer=2.
Case: n=6, a=[5,1,4,1,3,1] sorted:1,1,1,3,4,5. cnt1=3 >=3 => answer=C(3,3)=1. Brute: minimal product? three 1's product=1. Are there three 1's? Yes at indices 2,4,6? Actually original: index1:5, 2:1, 3:4, 4:1, 5:3, 6:1. Triples of three 1's: indices (2,4,6) only one triple. Check any triple with product 1? Only that. Good.
Case: n=5, a=[2,2,2,2,3] sorted:2,2,2,2,3. cnt1=4 >=3 => answer=C(4,3)=4. Minimal product=8? Actually 222=8. Triples: any three of the four 2's => 4. Check triple with a 3: 223=12 >8. So correct.
Case: n=6, a=[2,2,2,3,3,3] sorted:2,2,2,3,3,3. cnt1=3 => answer= C(3,3)=1. Minimal product=8. triple of three 2's only one. Could triple (2,2,3) product=12 >8. So correct.
Case: n=6, a=[2,2,3,3,3,3] sorted:2,2,3,3,3,3. cnt1=2, val2=a[2]=3, cnt2=4 => answer=14=4. Minimal product=223=12? Actually 22*3=12. Triples: pick two 2's (only one combination) and any one of four 3's => 4. Check triple (2,3,3) product=18 >12, (3,3,3)=27 >12. So correct.
Now, interesting scenario: n=4, a=[1000000000, 1000000000, 1000000000, 1000000000] => cnt1=4, ans=4. Product huge but we don't compute.
Thus algorithm stands.
Now, implement.
But we need to be careful about the count when cnt1 == 1 and cnt2 >= 2: ans = cnt2*(cnt2-1)//2. If cnt2 is large, it's fine.
Now, is there any scenario where cnt1 == 1 and cnt2 == 1 and cnt3 is large, ans = cnt3. That's fine.
Now, we also should consider the possibility that after counting cnt2 we might have cnt2 == 0? That cannot happen because n>=3 and cnt1=1, so there must be at least 2 more elements. So cnt2 >=1.
Now, what if cnt1 == 1 and cnt2 is 1 and then cnt3 is also 1? That's fine.
Now, what if cnt1 == 1 and cnt2 is 1 and cnt3 is also 1? That's n=3, answer=1.
Now, edge: When cnt1 == 2, we count cnt2 from index 2. But what if after index 2 there are also other values? We count only the block of val2, which is correct because all occurrences of val2 are contiguous.
But what if cnt1 == 2 and val2 appears multiple times? That's fine.
Now, I want to think about if there is any scenario where the three smallest values are not exactly a[0], a[1], a[2] due to sorting of indices? No, sorting gives non-decreasing order, so the first three are indeed the three smallest values (with multiplicities). So product = a[0]*a[1]*a[2] is minimal.
But wait: What about the case where there are negative numbers? Not here.
All good.
Thus final answer.
Now, we need to write code.
Implementation in C++:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i=0; i<n; ++i) cin >> a[i];
sort(a.begin(), a.end());
ll cnt1 = 1;
int i = 1;
while (i < n && a[i] == a[0]) {
++cnt1;
++i;
}
ll ans = 0;
if (cnt1 >= 3) {
ans = cnt1 * (cnt1-1) * (cnt1-2) / 6;
} else if (cnt1 == 2) {
// the third element is at index 2
int val2 = a[2];
ll cnt2 = 0;
int j = i; // i is already 2
while (j < n && a[j] == val2) {
++cnt2;
++j;
}
ans = cnt2; // C(2,2)=1
} else { // cnt1 == 1
// second value at index 1
int val2 = a[1];
ll cnt2 = 0;
int j = 1;
while (j < n && a[j] == val2) {
++cnt2;
++j;
}
if (cnt2 >= 2) {
ans = cnt2 * (cnt2-1) / 2;
} else { // cnt2 == 1
int val3 = a[2];
ll cnt3 = 0;
int k = 2;
while (k < n && a[k] == val3) {
++cnt3;
++k;
}
ans = cnt3;
}
}
cout << ans << endl;
return 0;
}
But we need to ensure we handle the case when cnt1 == 2 but the third element may not exist? n>=3, so it exists.
Potential bug: In cnt1 == 2, after counting cnt1, i is at index 2 (since we incremented i until a[i] != a[0], and we had exactly 2 copies, so i=2). So we can set j=i and count cnt2. However, it's possible that there are no elements? Actually n>=3, so i=2 is valid.
In cnt1 == 1, j starts at 1, counts cnt2. That's fine.
But there is an alternative approach: use standard library's upper_bound/lower_bound to count frequencies without loops. But loops are fine because they only scan the contiguous block, which in worst-case might scan many elements? The while loops only scan the block of equal elements. In worst-case, if the array has all same value, then cnt1 = n, we stop after scanning all n elements in the first while loop. That's O(n). That's acceptable (n=1e5). And we don't do additional loops. In other cases, scanning is limited to the blocks we care about. So total O(n) after sort. So fine.
But we need to be careful: In cnt1 >=3 case, we already scanned the first block to compute cnt1. That's O(cnt1). Good.
In cnt1 == 2 case, we scan the block of val2. That's O(cnt2). Could be up to n-2, but still O(n). That's fine.
In cnt1 ==1 case, we scan block of val2, and possibly block of val3. In worst-case, if cnt1=1 and cnt2=1 and cnt3 = n-2, we scan O(n). So overall O(n) after sort, which is fine.
Thus we are done.
But before final, we might consider if there's a more elegant way: using map to count frequencies and then selecting the smallest three distinct values and using combinations accordingly. That would also be O(n log n) due to sorting or map. But sorting is simpler.
Now, could there be a scenario where the three smallest values are not from three distinct groups? Actually we handle all.
Now, double-check the case cnt1 == 2: What if there are more than 2 of the smallest value? Then cnt1 would be >2, not equal 2. So fine.
Now, is there any scenario where cnt1 == 2 but the second smallest distinct value appears also at index 2, but there might be also other values equal to val2 later, which we count. That's fine.
Now, confirm with a tricky case: a = [1,1,1,2] but cnt1=3, we go to first branch. That's correct. But what about a = [1,1,1,1]? cnt1=4, first branch.
Now, test a case: a = [1,2,2,2] we handled earlier. cnt1=1, val2=2, cnt2=3, ans = C(3,2)=3. Good.
Now, test a case: a = [1,2,3,3] cnt1=1, val2=2, cnt2=1, val3=3, cnt3=2, ans=2. Good.
Now, test a case: a = [2,3,3,4] cnt1=1 (2), val2=3, cnt2=2 => ans = C(2,2)=1. Good.
Now, test a case: a = [3,3,4,4] cnt1=2? Actually sorted:3,3,4,4. cnt1=2, val2 = a[2]=4, cnt2=2, ans=2. Good.
Now, test a case: a = [5,5,6,7,7] sorted:5,5,6,7,7. cnt1=2, val2=6, cnt2=1, ans=1. Check triples: two 5's and one 6 => product=150. (5,5,7)=175, (5,6,7)=210, (5,6,7) other combos etc. Minimal is 150, only one triple. Good.
Now, test a case: a = [5,5,6,6,7] sorted:5,5,6,6,7. cnt1=2, val2=6, cnt2=2, ans=2. Triples: two 5's (only one combination) and any of two 6's =>2. Good.
Now, test a case: a = [5,6,6,6,6] sorted:5,6,6,6,6. cnt1=1, val2=6, cnt2=4, ans=C(4,2)=6. Triples: one 5 and any two 6's =>6. Good.
Now, test a case: a = [5,5,5,6] cnt1=3, ans=C(3,3)=1. Good.
Now, test a case: a = [5,5,5,5] cnt1=4, ans=C(4,3)=4. Good.
Now, test a case: a = [5,5,5,6,6] sorted:5,5,5,6,6. cnt1=3, ans=1. Good.
Now, test a case: a = [5,5,6,6,6] sorted:5,5,6,6,6. cnt1=2, val2=6, cnt2=3, ans=3. Good.
Now, think about the possibility that the minimal product might be achieved by a triple that does not consist of the three smallest values due to the product being the same? But we argued impossible. Let's formalize proof:
Let sorted array b[0..n-1]. For any triple indices, after sorting the values we have x ≤ y ≤ z. Since we pick without replacement, the multiset of chosen values is a subset of size 3. By the pigeonhole principle, x ≥ b[0]. Moreover, if x = b[0], then the remaining two values are from the set of remaining elements, which is a subset of b[1..n-1]. The smallest possible y in that case is b[1] if there is at least one copy of b[0] left? Actually careful: If b[0] occurs multiple times, it's possible that y could also be b[0] if we pick two copies of b[0]. But then y = b[0] which is ≥ b[1] if b[1] = b[0]? Actually if there are multiple copies of the smallest value, then b[0] = b[1] = b[0]. So b[1] equals b[0]. So y ≥ b[1] holds as equality. If there is only one copy of b[0], then b[1] > b[0]. If we pick that b[0], then the next smallest among the remaining is at least b[1] because the next smallest overall is b[1]. So y ≥ b[1]. If we don't pick b[0], then x ≥ b[1] and so y ≥ x ≥ b[1]. So in all cases, y ≥ b[1]. Similarly, z ≥ b[2]. Proof: Consider the three chosen indices. The largest index among them is at least 2 (0-indexed), because we need three distinct indices. So the value at that index is at least b[2] because b[2] is the third smallest element overall. More formally, by the same reasoning: after picking two elements, the third smallest of the sample is at least the third smallest of the population. So z ≥ b[2]. Therefore, product xyz ≥ b[0]*b[1]*b[2]. Equality holds iff x = b[0], y = b[1], z = b[2]. Thus the minimal product is uniquely the product of the three smallest values (with multiplicities). So any triple achieving it must have its sorted values exactly equal to b[0], b[1], b[2]. This triple's multiset is exactly {b[0], b[1], b[2]}. So indeed the counting is as described.
Thus correct.
Now, think about potential pitfalls: Since we use sorting, for large n sorting is fine. But if we wanted to optimize, we could find the three smallest values without sorting the entire array, by using nth_element or manual scanning. But not necessary.
Now, we should consider the note in the problem: "In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2." That's consistent.
Now, we must output a single number.
Now, double-check the handling of cnt1 == 2: In that branch, we computed cnt2 by scanning from index i. However, what if there are other values that are also equal to val2 but not contiguous because array is sorted? They will be contiguous. So fine.
Now, consider if cnt1 == 2 and the third element at index 2 is val2, but what if there is also a value equal to val2 before index 2? That's impossible because index 2 is the first element not equal to val1, and since sorted, all val1 are before val2. So fine.
Now, one more subtlety: In cnt1 == 2, we use a[2] as val2. But what if cnt1 == 2 and n=3 exactly, then a[2] is the only val2, cnt2 count from i=2 to <n will count 1. So ans = 1. Good.
Now, in cnt1 == 1 and cnt2 >= 2, we compute ans = C(cnt2,2). However, what about the scenario where cnt1=1 and cnt2 >=2 but there also exists another value equal to val2? cnt2 already includes all. That's fine. But we must ensure that the triple we count must consist of one val1 and two val2. Are there any triples that consist of one val1, one val2, and one val2 (i.e., two val2) — that's exactly what we count. However, could there be a triple consisting of one val1, one val2, and one val2' where val2' is also val2 but from same group? That's the same. So C(cnt2,2) counts unordered pairs of indices from the cnt2 occurrences. Since we pick one val1 (only one choice if cnt1=1), the triple is uniquely determined by which two val2 indices we choose. So good.
Now, what about scenario where cnt1=1 and cnt2=1 and cnt3 is large: we count ans = cnt3. That's number of triples: one val1, one val2 (only one choice), and one val3 (any of cnt3). That's cnt3. Good.
Now, check scenario where cnt1=1 and cnt2=1 and cnt3=1: ans=1.
Now, what if cnt1=1 and cnt2=1 and cnt3 also 1? That's n=3, ans=1.
Now, what if cnt1=1 and cnt2=1 and cnt3 >1: e.g., a=[1,2,3,3,3]? We already did.
Now, all good.
Thus final answer.
Now we can produce final solution.
But wait, there is a nuance: The problem statement says "pairwise distinct and i<j<k". Our counting with combinations counts unordered triples of distinct indices. Since we consider sets of indices, and the condition i<j<k is just a way to denote ordered triple but with increasing indices, each unordered set corresponds to exactly one ordered triple (i,j,k) with sorted indices. So counting sets is correct.
Now, we should also consider that the array indices are 1-indexed in the problem? Not relevant.
Now, let's test with an example that might cause confusion: array = [1,1,2,2,2] gave answer 3 earlier. That matches our reasoning.
Now, consider array = [1,1,1,2,2] sorted:1,1,1,2,2. cnt1=3 -> ans=1. But is there a triple (1,1,2) that gives product 2? That is larger than product 1. So only triples of three 1's give minimal product. Indeed, there is only one way to choose three 1's? Actually there are three 1's, so C(3,3)=1. Good.
Now, check array = [1,1,2,2,2,2] sorted:1,1,2,2,2,2. cnt1=2, val2=2, cnt2=4 -> ans=4. Triples: two 1's and any 2 => 4. Minimal product=2. Indeed, (1,1,2) product=2, (1,2,2) product=4, (2,2,2)=8. So answer 4. Good.
Now, check array = [1,2,2,2,2] sorted:1,2,2,2,2. cnt1=1, val2=2, cnt2=4 -> ans=C(4,2)=6. Minimal product=4. Triples: one 1 and any two 2's => 6. Good.
Now, consider array = [2,2,2,2,2] sorted: all 2. cnt1=5 -> ans=C(5,3)=10. Minimal product=8. All triples produce 8. So answer 10. Good.
Thus algorithm is correct.
Now, note: The problem may also be solved without sorting by simply counting frequencies of the three smallest values. We can find the three smallest values by scanning the array and keeping track of the smallest, second, third with duplicates considered? But careful: We need the three smallest order statistics, not the three smallest distinct values. For example, if we have values 1,1,2,3, the three smallest are 1,1,2. To determine that we need to know that the smallest value appears at least twice. So we can find the three smallest by scanning and maintaining a multiset of the three smallest? We could do a linear scan and keep a sorted list of up to 3 smallest values (with multiplicity). But sorting is simpler and within constraints.
Thus final answer.
Now, I'd like to ensure the answer fits within 64-bit. max answer: C(100000,3) = 1000009999999998/6 ≈ (1e5)^3/6 ≈ 1.666e14, well within 9e18. So long long is safe.
Now, code must handle large numbers and avoid integer overflow during multiplication. For C(cnt,3) we compute cnt*(cnt-1)*(cnt-2)/6. Since cnt up to 1e5, product up to ~1e15, fits in 64-bit. But if we used int (32-bit), it would overflow. So use long long.
Now, final code.
But we can also write using combinations functions.
I think it's good.
Now, I'll produce final solution description.
But before final, double-check the logic for cnt1 == 2: we used ans = cnt2. However, what if cnt1 == 2 and there are multiple distinct values after that? For example, array: [1,1,2,3,3] sorted:1,1,2,3,3. cnt1=2, val2=a[2]=2, cnt2=1 (since only one 2). So ans=1. Triples: two 1's and one 2 => product=2. Check other triples: (1,1,3) product=3 >2, (1,2,3)=6, (2,3,3)=18. So only one triple. Good.
Now, what about array: [1,1,2,2,3]? sorted:1,1,2,2,3. cnt1=2, val2=a[2]=2, cnt2=2, ans=2. Triples: two 1's and one 2 (two choices) => 2. Minimal product=2. Good.
Now, what about array: [1,1,2,2,2]? Already did, ans=3.
Now, what about array: [1,1,2,2,2,3]? sorted:1,1,2,2,2,3. cnt1=2, val2=a[2]=2, cnt2=3, ans=3. Triples: two 1's and one 2 => 3. Minimal product=2. Good.
Now, what about array where smallest appears once, second appears once, third appears many: [1,2,3,3,3] sorted:1,2,3,3,3. cnt1=1, cnt2=1, cnt3=3, ans=3. Triples: one 1, one 2, one 3 => 3. Good.
Now, what about array where smallest appears once, second appears once, third appears once but there are other larger numbers: [1,2,3,4,5] sorted:1,2,3,4,5. cnt1=1, cnt2=1, cnt3=1, ans=1. Good.
Now, what about array where smallest appears once, second appears many, third appears many? But second appears many means cnt2 >=2, so we go to cnt1=1, cnt2>=2 branch. But in that case the third smallest is also val2. So the triple composition is one val1 and two val2. So we don't need val3 at all. For example: [1,2,2,3,3] sorted:1,2,2,3,3. cnt1=1, cnt2=2, so ans=C(2,2)=1. Indeed minimal product=122=4. Check if triple (1,2,3) product=6 >4, (2,2,3)=12, (2,3,3)=18. So only one triple: indices (1,2,3?) Actually original indices: let's assign: [1,2,2,3,3] (positions 0..4). The triple: val1 at index0, val2 at index1, val2 at index2 -> that's one. Also could we pick val2 at index1 and index2? That's the only pair. So answer=1.
But wait: Could we also pick val1, val2 (index1), and val2 (index3)? But index3 is value 3, not val2. So no.
Thus correct.
Now, what about array: [1,2,2,2,2] sorted:1,2,2,2,2. cnt1=1, cnt2=4, ans=C(4,2)=6. Triples: one 1 and any two 2's => 6. Good.
Now, what about array: [1,1,2,3,4]? sorted:1,1,2,3,4. cnt1=2, val2=2, cnt2=1, ans=1. Triples: two 1's and one 2 => 1. Good.
Now, what about array: [1,1,2,2,4]? sorted:1,1,2,2,4. cnt1=2, val2=2, cnt2=2, ans=2. Good.
Now, what about array: [1,1,2,2,2,4]? sorted:1,1,2,2,2,4. cnt1=2, val2=2, cnt2=3, ans=3. Good.
Now, all consistent.
Thus final answer.
Now, I'll craft final explanation.
But hold on: The problem statement says "the quantity of triples (i, j, k) such that i, j and k are pairwise distinct and ai·aj·ak is minimum possible." It says pairwise distinct indices, which we've handled.
Now, I'd like to consider if there is any scenario where the three smallest values are not the first three in sorted order because of the way sorting works? No.
Thus we are done.
Now, implement code.
One more thing: In cnt1 == 2 branch, we set val2 = a[2]. But after counting cnt1, i is 2, so a[i] = a[2] = val2. So we can just use that. But what if cnt1 == 2 but the array size is exactly 2? Not possible, n>=3. So fine.
Now, about integer types: Use long long for answer and for counts because cnt1 may be up to 1e5, but we store in int? It's fine as int up to 1e5, but when we compute combinations we need to multiply, so cast to long long. So we can store counts as int but then convert. But to be safe, use long long for counts.
Simpler: use long long for cnt1, cnt2, cnt3.
But note that the array values themselves are up to 1e9, but we don't use them for product, only for comparison, so int is fine (32-bit). But we need to store them, so vector is fine.
Now, let's finalize code.
Potential improvements: use binary search to count frequencies: lower_bound/upper_bound. But loops are fine.
Edge Cases: All elements same, then cnt1 = n. The first while loop will go through all n elements, O(n). Then compute ans = n*(n-1)*(n-2)/6. Works.
If n is large (1e5), ans about 1.666e14, fits.
Now, think about the possibility of integer overflow in multiplication for ans: we compute cnt1*(cnt1-1)*(cnt1-2)/6. cnt1 up to 1e5, product up to ~1e15, which is less than 2^63-1 ~9.22e18. So safe.
Now, about division: integer division after multiplication yields integer. Since it's a combination, it's exact. So fine.
Now, produce final solution.
But wait: what about scenario where cnt1==2 and cnt2 is very large? ans = cnt2, up to 1e5, safe.
cnt1==1 and cnt2>=2: ans = cnt2*(cnt2-1)/2, up to ~5e9? Actually cnt2 up to 1e5, C(1e5,2)=~5e9, fits in 64-bit.
cnt1==1 and cnt2==1 and cnt3 large: ans = cnt3, up to 1e5.
So all safe.
Now, final answer.
But before final, I recall a similar problem from Codeforces: "A. Minimum Product" maybe? Actually there is a problem "A. Minimum Product" from CF Round? Not sure. But the solution is similar.
Now, I'll produce final answer.
But I want to test with a random case where cnt1=2 and the block for val2 is not contiguous? Since array sorted, it's contiguous. So fine.
Now, maybe there is a scenario where cnt1=2 and val2 appears, but there might be also a value equal to val2 before index 2? No.
Now, also check when cnt1=2 but the value at index 2 is not the second distinct value? Actually if cnt1=2, then index 2 is the first element not equal to the smallest. So it is the second distinct value (since sorted). So correct.
Now, we should also consider if cnt1=2 and there are multiple distinct values, but val2 could have frequency 1, and then there is val3 etc. That's fine; we only care about val2 frequency because triple must include two smallest (both of val1) and one val2, not val3. So ans = cnt2, not cnt2 + cnt3? Because if we pick a val3 instead of val2, the product becomes larger (since val3 > val2). So not counted.
Thus correct.
Now, finalize.
But wait: There is an alternative scenario: if cnt1=2 but there is also the possibility of picking triple (val1, val1, val1)? Not possible because only two val1.
Thus done.
Now, final answer.
But before final, I'll consider if there is any scenario where the minimal product might be achieved by a triple not including the smallest value? Could that happen? Suppose the smallest value is very large? But all values are positive, so smallest is the minimum. If we don't include it, then all three numbers are at least the second smallest. So product would be at least (second smallest)^3, whereas including smallest might give smaller product. For example, if smallest=1, second=100, third=100, product with smallest: 1100100=10000; without smallest: 100100100=1e6, larger. So indeed including smallest yields smaller product. So minimal product must include the smallest value, unless there are not enough copies? But we have three indices; if smallest appears only once, we may be forced to not include it in some triples, but the minimal product triples will include it because including it reduces product. So minimal product triples always include the smallest value? Actually if the smallest value appears only once, then any triple that includes it uses that one copy. Could there be a triple that doesn't include the smallest value but still has product equal to the minimal? That would require that the product of three numbers none of which is the smallest equals the product of the three smallest numbers. But the three smallest numbers include the smallest, so product with smallest is smaller than any product without it (since all numbers are positive). Because if we replace the smallest with the next larger number, product increases. So equality impossible. So minimal product triples always include the smallest value. More generally, they include as many copies of the smallest as possible, up to three.
Thus our counting aligns.
Now, I'll write final solution.
Now, final answer includes explanation and code.
Now, let's produce final response. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end());
code
// count how many times the smallest value appears
ll cnt1 = 1;
int i = 1;
while (i < n && a[i] == a[0]) {
++cnt1;
++i;
}
ll ans = 0;
if (cnt1 >= 3) {
// three copies of the smallest value
ans = cnt1 * (cnt1 - 1) * (cnt1 - 2) / 6;
} else if (cnt1 == 2) {
// two smallest are equal, third is different
int val2 = a[2]; // third element in sorted order
ll cnt2 = 0;
int j = i; // i is already 2
while (j < n && a[j] == val2) {
++cnt2;
++j;
}
ans = cnt2; // C(2,2) = 1, multiply by cnt2
} else { // cnt1 == 1
// smallest appears only once
int val2 = a[1];
ll cnt2 = 0;
int j = 1;
while (j < n && a[j] == val2) {
++cnt2;
++j;
}
if (cnt2 >= 2) {
// second value appears at least twice -> triple: one smallest, two val2
ans = cnt2 * (cnt2 - 1) / 2;
} else {
// second value appears exactly once -> need a third distinct value
int val3 = a[2];
ll cnt3 = 0;
int k = 2;
while (k < n && a[k] == val3) {
++cnt3;
++k;
}
ans = cnt3; // 1 * 1 * cnt3
}
}
cout << ans << "\n";
return 0;
a[1]a[2], then any other triple must have at least one value > a[2] (since the three smallest values are a[0] ≤ a[1] ≤ a[2], any triple that does not include exactly these three smallest values (with multiplicities) must contain some element > a[2]). If it contains an element > a[2], then to still have product = P, we would need the other two elements to be less than the corresponding smallest ones to compensate, but they cannot be less than a[0] and a[1] because those are the minimums. More formally: Suppose we have a triple (x,y,z) sorted non-decreasing: x ≤ y ≤ z. For it to have product P = a0a1