We need to compute number of unordered triples (i,j,k) distinct such that the relation of overwhelming is cyclic: i overwhelms j, j overwhelms k, k overwhelms i.
Define transformed values: r_i' = r_i - c, w_i' = w_i - d.
Condition: i overwhelms j iff r_i' * w_j' > r_j' * w_i'.
If we consider each student as a point (x,y) = (r_i', w_i')? Actually careful: The condition is r_i' * w_j' > r_j' * w_i'. Rearranging: r_i'/w_i' > r_j'/w_j'? But careful: That only holds if w_i' and w_j' are positive. However they could be negative? Since ri, wi are positive, but c,d are positive constants up to 1e9, and ri, wi >0, so r_i' and w_i' could be negative. The statement: "Blenda have decided to subtract some constant value c from all the values of reading speed and some value d from all the writing speed. Therefore she considers ri' = ri - c and wi' = wi - d." There is no guarantee they are positive. Also condition: |ri - c| + |wi - d| > 0 for each student (so not both are zero). So they can be negative. So we cannot simply consider ratios. We need to handle possibly negative numbers.
But note that the overwhelming condition is a product inequality. So it's comparing the cross products. This resembles orientation test for vectors? Actually, condition r_i' * w_j' > r_j' * w_i' is equivalent to the sign of the cross product in 2D if we treat points as (r_i', w_i')? Actually the cross product (x1,y1) x (x2,y2) = x1y2 - x2y1. So i overwhelms j iff (r_i', w_i') and (r_j', w_j') have cross product positive: r_i' w_j' - r_j' w_i' > 0.
So overwhelming is exactly that the vector from the origin to point i is oriented counterclockwise relative to vector to point j. In other words, the angle of (r_i', w_i') measured from positive x-axis (if we treat x = r', y = w') is greater than that of j? Actually cross product > 0 means that the first vector is to the left of second, i.e., the angle from first to second is positive (counterclockwise). But careful: For vectors v_i = (x_i, y_i) and v_j = (x_j, y_j), v_i x v_j = x_i y_j - x_j y_i. If >0, then v_j is to the left of v_i? Let's define orientation: The sign of cross(v_i, v_j) tells whether v_j is clockwise or ccw relative to v_i. Typically, cross(a,b) > 0 means b is ccw from a (or a is cw from b). However we have cross(v_i, v_j) > 0? Actually condition is r_i' w_j' > r_j' w_i' -> cross(v_i, v_j) = r_i' w_j' - r_j' w_i' > 0, so cross(v_i, v_j) > 0. This means that v_j is ccw from v_i (i.e., rotating v_i counterclockwise to align with v_j yields positive angle). So i overwhelms j if v_j is ccw from v_i.
Thus overwhelming is not symmetric; it's a directed relation based on angular ordering. So the condition that i overwhelms j, j overwhelms k, k overwhelms i means that v_j is ccw from v_i, v_k is ccw from v_j, and v_i is ccw from v_k. In terms of angles, that would imply that the angles of v_i, v_j, v_k around the origin are strictly increasing in ccw order, but then the last condition would require that after going from v_j to v_k to v_i, it wraps around the full circle? Actually if we order points by their angle measured from positive x-axis (say in [0,2π)), then if we have three distinct angles a_i < a_j < a_k, then v_i overwhelms v_j? Let's test: For vectors with positive angles, cross product >0 if angle(v_i) < angle(v_j) (since v_j is ccw from v_i). So i overwhelms j if angle_i < angle_j (in ccw order). Check: v_i = (1,0), v_j = (0,1): cross = 11 - 00 = 1 > 0, so i overwhelms j? Yes, and angle_i=0, angle_j=π/2, so angle_i < angle_j. So indeed, if we consider angles in [0,2π) increasing ccw, then i overwhelms j iff angle_i < angle_j (provided we restrict to positive cross product). However, if points can be in different quadrants, the ordering by angle modulo 2π still holds: The cross product between two vectors (not collinear) is positive if the smallest rotation from first to second is counterclockwise (i.e., the angle difference modulo 2π is between 0 and π? Wait, actually cross product sign indicates orientation relative to each other; for two vectors from origin, cross(v_i, v_j) > 0 means that v_j is on the left side of v_i, i.e., the angle from v_i to v_j measured ccw is less than π. More precisely, if we measure the angle from v_i to v_j in the range (-π, π], cross>0 if that directed angle is positive (0<θ<π). If the angle difference is >π (i.e., going ccw from v_i to v_j requires more than π, then the smaller rotation would be clockwise, and cross would be negative). Because cross(v_i, v_j) = |v_i||v_j| sin(θ), where θ is the angle from v_i to v_j. So sign = sign(sin θ). So cross > 0 iff 0 < θ mod 2π < π. But if we order points by their angle in [0,2π), then for any two points, the ccw difference from a to b is (b - a) mod 2π. If that difference is less than π, then cross(a,b) > 0; else cross(a,b) < 0. So the overwhelming relation is not simply based on the cyclic order of angles; it also depends on whether the angular separation is less than π. So it's like a tournament directed graph: For each pair of vectors, we direct the edge from the one that is "earlier" in circular order to the later if the shorter arc (counterclockwise) is less than π, else reverse.
But we have additional twist: vectors can have negative components, but still the cross product is defined.
The condition for a triple to be "good" is that it forms a directed cycle of length 3, i.e., each overwhelms the next cyclically. This is reminiscent of "3-cycles" in tournaments defined by angular ordering with half-plane condition.
Given points on the plane (not including origin), and for each unordered pair, we orient the edge based on sign of cross product (positive direction from i to j if cross(v_i, v_j)>0). Since cross(v_i, v_j) = -cross(v_j, v_i), exactly one direction is positive, so the directed graph is a tournament (complete directed graph with no ties, because no two points are collinear? Actually condition: no two students have both reading and writing speeds coincide. Could they be collinear? That is, v_i and v_j could be multiples? If one is scalar multiple of another? That would imply (r_i', w_i') = λ (r_j', w_j') for λ>0. Then cross=0 because cross(λ v, v)=0. But does the problem guarantee that cross ≠ 0? They guarantee |ri - rj| + |wi - wj| > 0, but that's not enough to guarantee they are not collinear with origin. For example, v_i=(1,2), v_j=(2,4) are collinear and both not equal but cross=0. The condition only says they don't have both coordinates equal; they could be multiples. However the problem statement: "Blenda doesn’t like fights in teams, so she thinks that a team consisting of three distinct students i, j and k is good if i overwhelms j, j overwhelms k, and k overwhelms i. Yes, the relation of overwhelming is not transitive as it often happens in real life." The definition of i overwhelms j is strict inequality (>). So if cross=0, then neither overwhelms the other? Actually condition is r_i'·w_j' > r_j'·w_i'. If cross=0, it's equality, so neither overwhelms the other. Then the relation is not defined; they are incomparable. But the problem's description might implicitly assume that for any two distinct students, exactly one overwhelms the other? They didn't explicitly state that. However the note says: "There are no two students, such that both their reading and writing speeds coincide, i.e. for every i ≠ j condition |ri - rj| + |wi - wj| > 0 holds." That ensures they are not identical, but does not guarantee they are not collinear with origin. In many geometry problems, such condition would be that no two points are collinear with the origin? But not here. However we need to consider if collinear cases can exist. Let's examine constraints: ri, wi > 0, c,d are positive integers. r_i' and w_i' can be negative, but they are integers. Could there be collinearity? For example, take c=2,d=2, student A: (1,1) -> (-1,-1). Student B: (3,3) -> (1,1). Cross = (-1)1 - (1)(-1) = -1+1=0. So yes collinear exists. In the sample, we have students: (1,1) -> (-1,-1); (4,1) -> (2,-1); (2,3) -> (0,1); (3,2) -> (1,0); (3,4) -> (1,2). Among these, (-1,-1) and (1,1) are collinear? Actually (1,1) is not in sample? Wait (1,1) transformed is (-1,-1), (3,4) transformed is (1,2). (-1,-1) and (1,1) would be collinear but there is no (1,1) in sample? Actually check: (1,1) given: r=1,w=1, c=2,d=2 => (-1,-1). (3,4): r=3,w=4 => (1,2). Not collinear. (3,2): (1,0). (2,3): (0,1). (4,1): (2,-1). So no collinearity. Could collinearity occur? Yes. But we need to know if we have to consider them. Typically in these problems they assume general position: no two vectors are collinear (i.e., cross product nonzero). However the problem statement didn't explicitly state that. But maybe it's implied by the condition that all students are distinct and there is no mention of collinearity but the condition "|ri - c| + |wi - d| > 0" ensures not both zero. So collinearity can happen.
However note that for a triple to be good, we need each pair to have a direction. If any pair has cross = 0, then they cannot satisfy the cycle because one direction doesn't hold. So such triples would never be counted. So collinear pairs essentially are not considered as potential edges for the tournament, but the tournament is not complete if cross=0. But the definition of good team requires all three mutual overwhelming relations, which would be impossible if any pair is tied (cross=0). So those triples are automatically excluded.
Now the problem: Given up to 345,678 points (n up to 3.45e5), count number of directed 3-cycles (i→j, j→k, k→i) in the tournament defined by cross product sign.
We need an efficient O(n log n) solution.
Observation: The relation is based on angular ordering. Let's consider mapping each point to an angle θ_i = atan2(w_i', r_i')? Actually careful: The cross product sign sin(θ_j - θ_i) > 0 if 0 < (θ_j - θ_i) mod 2π < π. So the direction from i to j is positive if the angular separation when going ccw from i to j is less than π.
This is exactly the orientation of a tournament defined by points on a circle (with respect to a fixed point), known as "circular tournament" or "round-robin tournament". There's known result: For points on a circle, the number of directed 3-cycles (cyclically oriented triples) equals number of triples that are not contained in any half-circle? Actually need to think.
Alternate viewpoint: Represent each point as a vector from origin. For any two vectors, we define an orientation: we can imagine projecting onto a circle. The direction i→j if the angle from i to j (counterclockwise) is less than 180°. This is the standard "geometric tournament" known to be transitive if points are in an open half-plane? Actually if all points lie in some open half-plane (i.e., there exists a line through origin such that all points are on one side), then the ordering by angle yields a linear order that is consistent with the direction: i→j iff i is earlier than j in that order. That tournament is transitive. But if points are spread around, cycles can appear.
Specifically, the tournament is known as "the tournament of points on a circle", and the number of 3-cycles equals something like total triples minus number of triples that are in a half-circle? Let's examine.
Consider a set of points on a circle (excluding antipodal?). For each triple of points, if they lie in some open semicircle (i.e., there exists a half-plane containing all three, with the half-plane defined by a line through the origin), then the tournament restricted to those three is transitive. Conversely, if they do not lie in any open semicircle (i.e., they span the full circle with angular spread >= π), then the tournament restricted to those three is cyclic (directed cycle). Actually is that true? Let's test with three points. Suppose we have three vectors with angles θ1 < θ2 < θ3. The arcs between them: d12 = θ2-θ1, d23 = θ3-θ2, d31 = 2π - (θ3-θ1). Actually the complement. The condition for the tournament: For each pair, the direction is based on the smaller angular difference being less than π. For three points, if all pairwise angular differences are less than π? Actually for any two points, the smaller angle between them is always ≤ π. It could be exactly π? That would be collinear opposite; cross=0. We ignore that case. So if all three points lie within a semicircle of length < π (open semicircle) then the maximum angular difference between any two is less than π. In that case, the ordering by angle yields that the direction from earlier to later is consistent: if θ1 < θ2 < θ3 and θ3 - θ1 < π, then for any i<j, the ccw difference (θ_j - θ_i) is less than π, so cross(v_i, v_j) > 0, meaning i→j. So the tournament is a total order: 1→2, 1→3, 2→3. That's transitive (acyclic). No 3-cycle.
If the three points are not contained in any open semicircle, then the angular spread is > π. In that case, the points wrap around the circle. Suppose angles: θ1 < θ2 < θ3. Then if θ3 - θ1 > π, that means the points are spread such that the complement arc (2π - (θ3-θ1)) is less than π. Then the direction between the extreme points: For 1 and 3, the ccw difference from 1 to 3 is (θ3 - θ1) > π, so the smaller angle is 2π - (θ3-θ1) < π, which would be clockwise from 1 to 3? Actually careful: The direction is based on cross sign, which corresponds to the directed angle from first to second being between 0 and π (ccw) for positive. So for i→j, we need that the ccw angle from v_i to v_j is less than π. So if we consider 1→3, we check ccw from θ1 to θ3: that is θ3 - θ1 (if we assume 0≤θ1<θ3<2π). If θ3-θ1 > π, then ccw from θ1 to θ3 is >π, so cross(v1,v3) < 0, meaning 3→1 (since cross(v3,v1) would be positive). So the direction between 1 and 3 is from 3 to 1. Meanwhile, for 1→2, since θ2-θ1 < π? Actually if spread > π, it's possible that θ2-θ1 might be less than π; similarly θ3-θ2 might be less than π, because the large gap is between θ3 and θ1 crossing 2π. So typical configuration: angles: 10°, 120°, 250° (difference 10->120 =110°<π, 120->250=130°<π, 250->10 via ccw: from 250 to 10 is 120° (since 360-250+10=120) <π). So then directions: 10→120, 120→250, and 250→10? Wait, from 250 to 10 via ccw: 250 to 10 is 120° <π, so cross(v250,v10)>0 meaning 250→10. Also check 10→250: ccw from 10 to 250 is 240° >π, so cross(v10,v250) <0. So indeed direction is 250→10. So we have a cycle: 10→120, 120→250, 250→10. That's a directed 3-cycle. So the triple forms a cycle iff the three points are not contained in any open semicircle (i.e., they span more than π). Also if they exactly lie on a semicircle boundary? If maximum arc length = π exactly, then some cross product might be zero? Actually if three points exactly lie on a semicircle with endpoints inclusive, then some pair might be opposite (difference exactly π) if points are antipodal? But given they are vectors, collinearity through origin with opposite direction gives cross=0. That would not be a cycle anyway. But if they are exactly spanning π but not collinear? For three points, if the maximum angular separation between two points equals π, and the third is somewhere in between, then for the extremes, the ccw difference equals π, so cross=0 (since sinπ=0). So that triple is not counted. So we can safely treat condition as strict inequality. So the good teams correspond exactly to triples of points such that the angular span (the minimum arc covering all three points) is greater than π. More precisely, for three points with angles sorted, the condition is that the maximum gap between consecutive angles (cyclic) is less than π? Wait, what condition yields cycle? Let's formalize.
Given three angles sorted θ1 < θ2 < θ3. The arcs (cyclic) are: a = θ2 - θ1, b = θ3 - θ2, c = 2π - (θ3 - θ1) = 2π - (a+b). The three points lie in some open semicircle iff there exists a semicircle covering them, which is equivalent to max(a,b,c) ≥ π? Actually if there is a semicircle covering them, then the complement arc (the largest gap) is at least π? Let's think: The points are contained in an open semicircle if and only if there is a rotation such that all points fall within an interval of length π. This is equivalent to the minimal arc covering all points having length ≤ π. The minimal covering arc length is 2π - max_gap, where max_gap is the largest angular gap between consecutive points (cyclic). Because if we take the complement of the largest gap, we get the smallest arc covering all points. So they are contained in a semicircle iff 2π - max_gap ≤ π => max_gap ≥ π. So the condition for being contained in a semicircle is that the maximum gap between consecutive points (in cyclic order) is at least π. Conversely, they are NOT contained in any semicircle (i.e., they span > π) iff max_gap < π. In that case the covering arc length > π. So the triple forms a cycle iff max_gap < π.
But earlier we saw the triple with angles 10,120,250: gaps: 110°,130°,120°, all <180°, so max_gap=130° < π -> cycle. Yes.
If max_gap ≥ π, then they lie in a semicircle, and the tournament is transitive. Also if any gap exactly equals π, then two points are opposite, cross=0, so no cycle. So for cycle, we need all gaps < π.
Thus, the number of good teams equals the number of unordered triples of distinct points such that when sorted by angle, the maximum gap between consecutive angles (including the wrap-around) is less than π.
So the problem reduces to: Given n points (vectors) from the origin (excluding the origin itself? Actually no point is at origin because |ri-c|+|wi-d|>0, so vectors are non-zero). Compute the number of triples with max gap < π.
Now we need to compute this efficiently for n up to 345,678 (3.45e5). O(n^2) is impossible.
Observation: It might be easier to compute total triples minus triples that are "bad" (i.e., contained in some semicircle). But careful: The condition "contained in a semicircle" for a triple includes those that are exactly on a semicircle (including boundaries). Since boundaries (gap exactly π) yield no cycle anyway, they should be considered bad. But we must ensure that for triples with gap exactly π, they are not counted as good, and also they are not counted as bad in the sense of transitive? They are not good, so they should be subtracted from total. So total good = total triples - triples that are not good. But careful: Are there any triples that are not good but also not contained in a semicircle? If three points are not contained in a semicircle, we argued they form a cycle, so they are good. The only possible exception is when some cross product is zero (i.e., collinear points or opposite). In that case, the triple is not good but may also not be contained in any semicircle? Let's examine: If two points are collinear with origin and opposite directions? Actually collinear but not opposite (both same direction) would have angle difference 0? But they cannot have same direction because that would be multiples with positive scalar; then they are in the same ray, angle difference 0, so they are contained in any semicircle? They are definitely in a semicircle. But if they are collinear but opposite (i.e., one is negative of the other), then angle difference = π. Then the triple may or may not be contained in a semicircle. Suppose we have points at angles 0°, 180°, and 90°. The gaps: between 0 and 90: 90°, 90 to 180: 90°, 180 to 0: 180°. max_gap=180° = π, so they are exactly on a semicircle boundary? Actually the minimal covering arc length is 180° (since the points are spread across 180°? Wait: The smallest arc covering all three: from 90° to 0° passing through 180? Actually we need to check: The points are at 0, 90, 180. The largest gap is from 180 to 0 via 360: that's 180°. The complement is the arc from 0 to 180 length 180°, which is a semicircle. So they are contained in a closed semicircle. So yes. If the third point is exactly at 0°, and the opposite at 180°, plus another at 200°, then angles: 0,180,200. Gaps: 180-0=180°, 200-180=20°, 360-200+0=160°. max_gap=180°, so contained in a semicircle? The complement arc from 200 to 0 (through 360) is 160°, which is less than 180, but the points are not all within a semicircle of length <180? The minimal covering arc length is 360 - max_gap = 180°, which is exactly π. So they are contained in a closed semicircle of length π. So any triple with a gap exactly π is contained in a closed semicircle (maybe with boundary). So all non-good triples are exactly those where the three points are contained in some closed semicircle (including boundary) and also those where cross product zero? But the latter is also contained in a closed semicircle because if two points are opposite, they force a gap of π. If two points are collinear same direction, then they are on same ray; the triple can be contained in a semicircle. So indeed, the condition for a triple not to be good is exactly that the three points can be placed in a semicircle (including boundary). But we need to verify if there exist triples that are contained in a semicircle but still form a cycle? No, as argued, if they are contained in a semicircle, the tournament is transitive, so not a cycle. So the equivalence holds: good team iff triple not contained in any semicircle of angle ≤ π (i.e., covering arc > π).
Thus, good triples = total triples - triples that are contained in some semicircle.
Now counting triples contained in a semicircle is a known problem: Given n points on a circle (no two coinciding, but possibly collinear with origin? That would correspond to multiple points with same angle. However, if two points have same angle (collinear same direction), then they are coincident in angle. But is that possible? The points could be multiples: v_i = λ v_j with λ>0, then they have the same angle (if we ignore sign). But wait, if both coordinates are scaled positively, then r_i' and w_i' are proportional, so they lie on same ray from origin, thus same angle (mod 2π). However, the problem statement: "There are no two students, such that both their reading and writing speeds coincide, i.e. for every i ≠ j condition |ri - rj| + |wi - wj| > 0 holds." This does not prevent one being a scalar multiple of the other. So yes, points can have the same angle. But careful: if they have same direction (both coordinates scaled), then their transformed vectors (r_i', w_i') are multiples. That means they lie on the same ray from origin. Their angle is identical. Then the cross product between them is zero. Since cross=0, neither overwhelms the other. In such a case, a triple containing these two cannot be good because the relation between them is not defined (no strict inequality). So such triples should not be counted as good. They also may or may not be counted as "contained in a semicircle". They definitely are (since same angle). So they will be counted as bad. That is fine. However, our reduction to angular condition assumed that cross product is defined and nonzero for each pair in the triple, because otherwise triple is not good. But in the angular condition, if two points have same angle, they are at same location on the circle. Then the "max gap" might still be less than π? For three points with two identical angles, the sorted angles have duplicates. The concept of gaps becomes ambiguous. But since cross=0, the triple is not good. So we need to exclude all triples where any pair is collinear (i.e., cross=0). Those are automatically bad. Our count of "triples contained in a semicircle" would count them if they are indeed contained in a semicircle (which they are). But does our method for counting triples in a semicircle based on angles assume distinct angles? We'll need to handle duplicate angles specially.
Also, points can be collinear but opposite directions: angles differ by π. That's also cross=0. They have distinct angles but difference exactly π. That also yields cross=0. In the angular condition, that corresponds to a gap of exactly π. As argued, triple with any gap = π is not good because some cross product is zero. So we must exclude triples with any gap = π. So the condition for good triple is: all pairwise cross products >0 in the cycle direction? Actually the triple is defined by three distinct students; the definition requires i overwhelms j, j overwhelms k, k overwhelms i. That implies all three pairwise relations must be defined and satisfy strict inequality. So any zero cross product invalidates the triple. So the condition for a triple to be good is: For all three pairs, cross product ≠ 0 (i.e., strict inequality one way), and the cyclic condition holds. But from the angular perspective, if all three pairwise cross products are nonzero, then the directions are determined, and the cyclic condition is equivalent to the points not being contained in a semicircle. However, when cross products are nonzero, can there be a case where they are not contained in a semicircle but the cycle condition fails? Let's examine: Suppose three points with angles such that max_gap < π, then we get a cycle. If max_gap > π, then they lie in a semicircle and the tournament is transitive. If max_gap = π exactly, then at least one pair has angular difference exactly π -> cross=0. So for nonzero cross, max_gap is either <π or >π but never =π. So the condition "nonzero cross for all pairs" is automatically satisfied if max_gap ≠ π. But could there be a triple with max_gap >π (i.e., points in semicircle) but some cross product zero? Possibly if two points are collinear same direction (angle identical) then cross=0 and they are in semicircle. But max_gap might be >π? Actually if two points share same angle, then the sorted angles have duplicates; the gaps: the duplicate points are at same angle, so the gap between them is 0. The max_gap could be >π or <π. For triple with two points same angle, cross between them is zero, so triple not good regardless of max_gap. So such triples must be excluded. Also, three points could have one pair opposite (gap π) and others not; cross zero. So these also must be excluded.
Thus, the problem reduces to counting triples (i,j,k) such that all pairwise cross products are nonzero and the points are not contained in any semicircle. But we can also count directly: Count all triples with max_gap < π, provided that we also ensure no two points have angle difference 0 or π. But if max_gap < π, automatically all pairwise differences are <π (since any difference ≤ max_gap? Actually if points have same angle, difference 0 < π, but cross=0. So we need to handle zero angle difference separately. Also if any difference exactly π, then max_gap would be at least π, so max_gap < π excludes gap=π. So the only issue is duplicate angles (difference 0). So we need to exclude triples where at least two points have the same angle (collinear same direction) because then max_gap could be <π but cross=0. Actually if two points have same angle, then the max_gap might still be <π, but cross between them is zero, so triple not good. So we must not count those.
Similarly, what about three points all collinear same ray? All same angle, cross all zero, not good.
Thus, overall good triples = number of triples of distinct points with distinct angles (i.e., no two points collinear same direction) such that when sorted by angle, the maximum gap between consecutive distinct angles (including wrap) is less than π. However, we also need to consider that points can be collinear opposite direction? That yields angle difference π, which would make max_gap at least π; so those would be excluded automatically by max_gap < π condition. But we need to be careful: If we have points with angles that differ by exactly π, then max_gap will be at least π, so they wouldn't be counted. So they are automatically excluded from the candidate set.
So essentially we need to count triples of points with distinct angles and with max_gap < π. But also note: If we have multiple points with same angle, we cannot treat them as a single point because triples involving two points from same angle are invalid. So we might need to subtract those.
One approach: Count all triples (n choose 3) minus triples that are "bad". The bad triples include:
- Triples where all three points lie within some closed semicircle (including boundaries) OR triples where any pair has cross product zero (i.e., same angle or opposite angles). But note: The set of triples contained in a closed semicircle already includes all triples with points on the same ray? Yes, because they are within a semicircle.