We need to count subsets of N items such that no two are "on bad terms". Two items i, j are bad iff A_i * A_j + B_i * B_j = 0. Equivalent to vectors (A_i, B_i) and (A_j, B_j) have dot product zero. So condition: for any two selected, their vectors must not be orthogonal (dot product != 0). But wait: "bad terms" means they cannot be chosen together; they are bad if A_i * A_j + B_i * B_j = 0. So we cannot select a pair that are orthogonal. So we need to count subsets with no orthogonal pair.
N up to 2e5, coordinates large up to 1e18. We need to count subsets modulo 1e9+7.
Observation: The condition is only about pairs that are orthogonal. This is reminiscent of grouping vectors by direction (up to scaling) and considering that orthogonal groups are mutually exclusive? Because if two vectors are not scalar multiples, they might be orthogonal or not. But the condition is pairwise: we cannot have two vectors from certain pairs of groups?
Let's consider representation: For a vector (a,b), consider its normalized direction: we can reduce by gcd? But coordinates large, rational direction. The dot product zero condition: a1a2 + b1b2 = 0. That can be rearranged: (a1/b1) * (a2/b2) = -1? If b's nonzero. But careful.
If we treat each vector as a point in rational slope. The orthogonality condition is symmetric. We can think of grouping vectors that are collinear (i.e., multiples of each other) because then for two vectors from same group, their dot product = k1 k2 (a^2 + b^2) maybe? Actually if (a1,b1) = t*(a,b), (a2,b2)=s*(a,b), then dot product = t s (a^2+b^2). This equals 0 iff a^2+b^2=0 which only if a=b=0. But if (a,b) is nonzero (since if both zero, then dot with any? Actually if both zero vector, then (0,0) dot product = 0 with any vector, but also with itself? Let's consider zero vector: (0,0). For i with (0,0), dot product with any j is 0, so they are bad with any other? Condition: i and j are on bad terms if A_i * A_j + B_i * B_j = 0. So zero vector with any vector yields 0, so they are bad. Also zero vector with another zero vector? Yes, 0. So zero vectors cannot be paired with any other vector, nor with each other (since any pair of zero vectors also dot 0). So effectively, each zero vector can only be chosen alone? Actually we can select at most one zero vector? Because if we select two zero vectors, they are bad. Also if we select one zero vector and any other non-zero, they are bad. So the set can contain at most one zero vector, and only if we include no other vectors. That means zero vectors are like a special category: we can either take none, or take exactly one among them. But we can also take subsets consisting solely of non-zero vectors, with no zero vectors.
Thus handle zero vectors separately.
Now for non-zero vectors. They have direction. The condition is about orthogonality, not about collinearity. Two vectors that are collinear (positive or negative multiples) are not orthogonal unless the vector is null (zero) which we excluded. Because for non-zero vector v, dot(v, t v) = t |v|^2, which is zero only if t=0 but t non-zero? Actually t can be negative? t negative gives negative product, but |v|^2 positive, so negative, not zero. So collinear vectors are always safe? Not exactly: if v and t v: dot = t (a^2+b^2) ≠ 0 if a^2+b^2 ≠ 0 and t ≠ 0. So they are not orthogonal. So within a group of collinear vectors, any subset is allowed because they are never bad with each other.
But vectors from different groups may be orthogonal or not. Specifically, if we have two directions d1 and d2, they might satisfy v1·v2=0. For given direction, there is a specific set of directions orthogonal to it (essentially the perpendicular direction). For a non-zero vector (a,b), the condition for (c,d) to be orthogonal is a c + b d = 0. This is a linear equation; the set of vectors orthogonal to it is a one-dimensional subspace (line through origin). So all vectors orthogonal to (a,b) are collinear to (-b, a) (or (b, -a)). So the orthogonal group is itself a direction: the direction perpendicular to original. So there is a pairing: direction v and its orthogonal direction v_perp. Also note that if v is perpendicular to w, then w is perpendicular to v. Also v_perp_perp is opposite direction? Actually v_perp_perp = -v? Let's see: v=(a,b), perp = (-b, a). Then perp of perp = (-a, -b) = -v. So essentially the set of vectors collinear to v and those collinear to v_perp are mutually orthogonal? Actually any vector in the v direction (i.e., t v) is orthogonal to any vector in the v_perp direction (i.e., s v_perp) because v·v_perp = a*(-b)+ ba = -ab+ba=0. Indeed. However, note that vectors collinear to v_perp are all orthogonal to those collinear to v. But also what about vectors that are not exactly collinear to either v or v_perp? They may or may not be orthogonal to v; some may be orthogonal but not exactly v_perp direction? Actually any vector orthogonal to v must be collinear to v_perp (since in 2D, orthogonal complement is 1D). So indeed, the only vectors that are orthogonal to a given direction are exactly those in the perpendicular direction. So the bad relation partitions directions into pairs (or possibly self-orthogonal if v is orthogonal to itself? That would require |v|^2=0, which only for zero vector). So for non-zero vectors, a direction d and its perpendicular direction d' are the only ones that cause conflict. Also note that a direction might be perpendicular to itself? That would require a^2+b^2=0 => a=b=0. Not non-zero. So each direction is either not orthogonal to any other except its perpendicular direction. Also note that d and d' might be the same if they are collinear? That would require (a,b) is proportional to (-b,a). That implies a = t (-b), b = t a. Solve: from second, b = t a => substitute first: a = t (- (t a)) = -t^2 a => a(1+ t^2)=0 => a=0, then b = t0 =0, but non-zero? So no non-zero solution. So d and d' are distinct directions. However, they might be considered the same if we treat direction up to sign? Actually direction up to scaling includes both v and -v, but v and -v are collinear. For perpendicular, v_perp = (-b, a). Is v_perp possibly collinear to v? No unless zero. So they are distinct. But note: if we consider direction as line (ignoring orientation), then v and -v are same direction. But v_perp and -v_perp are same direction. So we have two distinct directions (lines) which are orthogonal to each other. So it's a pair of "orthogonal lines". So the conflict graph is: vertices are vectors (items). Edges exist between any two vectors if one's direction is perpendicular to the other's direction. But also if they are from the same direction? No edge. So it's a bipartite-like conflict between two groups of directions? Actually there could be many directions. The conflict graph is not just between two groups; it's between each direction and its perpendicular direction. But a given direction might be perpendicular to multiple directions? In 2D, if v is fixed, the set of directions orthogonal to v is exactly one line. So each direction has exactly one perpendicular direction (line). However, a direction could be perpendicular to more than one if we consider distinct lines? For example, v=(1,0). Orthogonal direction vectors satisfy 1d=0 => c=0. So direction of (0,1) (and (0,-1) same line). So only one line. So indeed, each direction line L has a unique orthogonal line L_perp. So the relation pairs up directions: L and L_perp are paired. However, note that it's possible that L_perp = L? No for non-zero. So it's an involution without fixed points. So the set of all directions is partitioned into pairs (L, L_perp) and possibly some isolated directions that have no perpendicular counterpart among the given vectors? Actually every direction has a perpendicular direction, but if that perpendicular direction is not present among our vectors, then there is no conflict because there are no vectors in that direction to cause orthogonal pairs. But wait: The conflict is between any two vectors from our set. So if we have direction L, and there are no vectors in L_perp in our set, then there is no conflict involving L vectors with any other vectors (except possibly within L, but that's fine). So the conflict edges only exist between vectors from a direction L and vectors from its perpendicular direction L_perp, if both present. So the conflict graph is a disjoint union of independent components? Actually consider three directions: L1, L2, L3, with L2 = L1_perp, L3 is some other direction not perpendicular to L1 or L2? But L3 may be perpendicular to something else, say L4, which may or may not exist. So the conflict edges only connect L with its perpendicular direction. So the overall conflict graph is a collection of disjoint components, each component being either:
- a single direction (with no perpendicular direction present), all vectors within are independent (no conflicts with each other, only possible conflicts with vectors from that direction's perpendicular direction, but if not present, no conflicts).
- a pair of directions (L and L_perp) that are mutually conflicting: any vector from L is in conflict with any vector from L_perp. But also note that vectors within each direction are not conflicting with each other.
Thus the conflict graph is a collection of independent sets? Actually it's a bipartite graph between two groups (if both present) where all edges are present (complete bipartite) because any vector from L is orthogonal to any vector from L_perp. Check: For any v in L (collinear with some base) and any w in L_perp (collinear with perpendicular base), dot product = (t v0)·(s w0) = t s (v0·w0) = 0. Yes. So indeed, all cross pairs are conflicting.
But is it always true that if two vectors are from directions that are not perpendicular, they are never conflicting? Suppose two vectors from directions that are not collinear and not perpendicular. Then dot product is non-zero (since only zero when orthogonal). So yes, they are safe. So the conflict graph is exactly a union of cliques? No, it's a collection of complete bipartite components (each between a direction and its perpendicular direction) and isolated vertices (directions with no perpendicular counterpart). Plus zero vectors behave differently.
But wait: Could there be a direction L such that there exist vectors in both L and L_perp, but also vectors in some other direction M that is orthogonal to L? But in 2D, if M is orthogonal to L, then M must be collinear to L_perp. So M is either L_perp or the opposite direction (which is same line). So no third direction orthogonal to L. So indeed, the only orthogonal direction is L_perp.
Thus the conflict graph is a collection of independent "pairs of groups" (each group is a set of vectors with same direction up to scaling). Additionally, there might be groups that are self-contained (no conflicts with others). So the counting of subsets with no conflicting pairs reduces to: for each direction group (or each pair of mutually orthogonal groups) we need to choose subsets that do not simultaneously contain vectors from both groups. For a pair (G, H) where G and H are mutually conflicting (all cross edges), the valid choices are: any subset of G ∪ H that does not contain both a vector from G and a vector from H. So allowed subsets: any subset of G alone, any subset of H alone, or the empty set. But note that subsets of G alone include any combination within G; similarly H. But also we could choose a mix from G and H? No, because that would include at least one from each? Actually if we choose at least one from G and at least one from H, that is forbidden. So allowed are subsets that are entirely contained in G, or entirely contained in H, or empty. But also subsets that include some from G and none from H are allowed; subsets with some from H and none from G are allowed; subsets with both are not allowed. So essentially, the selection from that component must be a subset of G or a subset of H (including empty). But careful: The empty set is counted in both, but we'll handle double counting later. Equivalent to: total ways from the component = (ways from G) + (ways from H) - 1 (for double count empty). Because subsets that are subset of G: all subsets of G = 2^{|G|}. Subsets that are subset of H: 2^{|H|}. Empty counted twice, so total = 2^{|G|} + 2^{|H|} - 1.
Alternatively, we can think: the component is a complete bipartite conflict between two independent sets. The number of independent sets in a complete bipartite graph K_{m,n} (where edges represent conflict? Actually we want subsets with no edges (no conflicting pairs). The complement: we want stable sets (independent sets) in the conflict graph. In the conflict graph, vertices are vectors, edges exist between G and H. Within G and within H, no edges. So the graph is a bipartite graph that is complete between parts. Independent sets can contain any number of vertices from G as long as they contain no vertices from H, or any number from H as long as they contain no vertices from G, or the empty set. So indeed independent set size = (2^{|G|} - 1) (non-empty subsets of G) + (2^{|H|} - 1) (non-empty subsets of H) + 1 (empty) = 2^{|G|} + 2^{|H|} - 1. Good.
For a single group G (with no perpendicular group present), there are no conflicts at all, so any subset of G is allowed: 2^{|G|}.
Also for the zero vectors: each zero vector is conflicting with any other vector (including other zero vectors) because dot product 0. So they form a complete graph (clique) among themselves and with all other vectors? Actually zero vector with any other vector is conflicting. So if we include any zero vector, we cannot include any other vector (including other zero vectors). So the whole set of zero vectors is like a set of items where you can select at most one of them, and also if you select any, you cannot select any non-zero vectors. So essentially we can treat all zero vectors as a special "group" Z. But careful: If we select a zero vector, we must select no other vectors from any other group (including other zero vectors). So the conflict is complete between Z and all vertices (including Z itself). So the entire conflict graph is not just product of independent components; it's more interconnected. Actually, zero vectors conflict with all other vectors, including themselves? A zero vector with itself: dot product = 0, so they are also bad. So we cannot select two zero vectors together. So indeed, from the set of zero vectors, we can either select none, or select exactly one. Moreover, if we select one zero vector, we cannot select any non-zero vector. So the selection of zero vectors interacts globally.
Thus we need to consider the whole set: non-zero vectors are partitioned into direction groups; each direction group G has a perpendicular direction group G_perp (maybe present or not). The zero vectors Z are special.
The conflict edges:
- Within each direction group: none.
- Between a direction group G and its perpendicular group H: all edges.
- Between groups that are not perpendicular: no edges.
- Between any zero vector and any other vector (including other zero vectors): all edges. So zero vectors conflict with everything (including themselves? Actually with themselves, yes, but careful: the condition i != j, but subset cannot have two items that are on bad terms. So if we select two zero vectors i and j (i≠j), they are bad, so not allowed. Also selecting one zero vector alone is okay because no pair. So indeed, selecting more than one zero vector is forbidden. And selecting a zero vector with any non-zero is forbidden. So zero vectors are essentially a "mutually exclusive and exclusive with all others" set.
Thus the overall conflict graph is: a set of "non-zero" vertices that are partitioned into components as described (each component either a single direction group or a pair of direction groups). These components have no conflicts between different components (since if direction groups are not perpendicular and not zero, they are not conflicting). Also zero vertices conflict with everything. So we can treat the zero vertices as a special component that is fully connected to all other vertices and also internally fully connected. So the whole graph is: a complete graph on zero vertices, and a complete bipartite between zero vertices and all other vertices, plus the rest components independent from each other (no edges across components except the zero connections). So to count independent sets (subsets with no edges), we need to consider:
-
We can choose not to take any zero vertex. Then we are free to choose any independent set from the union of non-zero components, and these components are independent (no edges between them). So the number of independent sets from the non-zero part (with zero not chosen) is the product over components of (independent set counts for each component). Because choices across components are independent (no conflicts).
-
We can choose exactly one zero vertex. Then we cannot choose any other vertex (zero or non-zero). So there are |Z| ways to pick that zero vertex. This gives |Z| subsets of size 1 from zero only. Also, we could choose just that zero vertex and nothing else; indeed. So total subsets that include a zero vertex: exactly |Z| (since you can only pick one zero, and cannot pick any others). But also could we pick zero vertices only? Actually picking exactly one zero vertex is allowed. Picking zero vertices with no non-zero yields |Z| possibilities. Picking zero with any non-zero is not allowed. So subsets with zero vertices are: empty set? No, empty has no zero. So non-empty zero-containing subsets: exactly |Z| subsets (each single zero vertex). But wait, what about the empty set? It will be counted in the case of no zero vertex (as part of product from components when we take empty from all components). So we need to include it.
Thus total independent sets = (product over non-zero components of comp_count) * (1? Actually product of comp counts includes the empty choice from all components; that corresponds to selecting no non-zero vertices. So that product counts subsets with no zero vertices and any selection from components that respects internal constraints). Then plus the contributions from subsets that include exactly one zero vertex and no non-zero vertices, which is |Z|. However, also note: could we select a subset that includes a zero vertex and also includes some non-zero vertices? No, because conflict. So not allowed.
But is there any possibility of selecting no zero but some non-zero? Yes that's included in product.
Thus total = (product over non-zero components of comp_count) + |Z|.
But careful: Is the empty set counted twice? In product, when we take empty from all components, we get empty set (no vertices). In the zero case, we add subsets containing exactly one zero vertex. The empty set is not included in the zero addition because those subsets contain a zero. So no double count.
But wait: Could there be subsets with more than one zero? No, forbidden. So yes.
Now we need to verify with example.
Example 1:
3
1 2
-1 1
2 -1
Let's compute:
v1=(1,2); v2=(-1,1); v3=(2,-1).
Check orthogonality:
v1·v2 = 1*(-1)+21 = -1+2=1 ≠0.
v1·v3 = 12 + 2*(-1) = 2-2=0 → bad.
v2·v3 = (-1)2 + 1(-1) = -2 -1 = -3 ≠0.
So conflicts: v1 and v3 are bad.
Now directions:
v1=(1,2) direction: slope 2? Normalize? Let's compute: maybe its perpendicular direction is (-2,1) or (2,-1)? Actually v1_perp = (-2,1) (since dot (1,2)·(-2,1) = -2+2=0). v3=(2,-1) is indeed collinear to (-2,1)? Because (-2,1) scaled by -1 gives (2,-1). Yes, v3 is collinear to v1_perp. So group G: direction (1,2) has size 1. Group H: direction (-2,1) (i.e., (2,-1) etc) has size 1. So they form a pair component. v2 direction is (-1,1). Check its perpendicular: ( -1? For v=(-1,1), perp = (-1, -1)? Actually compute: v·w=0 => (-1)a+1b=0 => b = a. So perpendicular direction is (1,1) up to scaling? Wait: if we take w=(1,1): (-1)1+11=0? That gives 0. Yes. So v2 perpendicular direction is (1,1). But we have no vector collinear to (1,1). So v2 is a single direction component with no perpendicular present. Zero vectors: none.
Thus components:
- pair (G size1, H size1): comp_count = 2^1 + 2^1 -1 = 2+2-1=3.
- single group size1: comp_count = 2^1 = 2.
Product = 3*2 = 6. Then plus |Z|=0 => 6. But expected output is 5. So we overcounted by 1. Why? Let's list all subsets of the 3 items, avoiding conflict (v1 and v3 cannot both be selected). Subsets:
Empty: {}
{v1}
{v2}
{v3}
{v1,v2}
{v2,v3}
{v1,v2,v3}? contains v1 and v3 -> bad
{v1,v3} -> bad
So allowed: 6? Actually we have 6: empty, {1}, {2}, {3}, {1,2}, {2,3} = 6. But expected answer 5. Did we miss that {1,2} is allowed? Check v1 and v2 not bad. {2,3} allowed. So 6 subsets. But sample says 5. Let's re-check sample input and output:
Input:
3
1 2
-1 1
2 -1
Output: 5
Maybe I miscomputed something: Let's check orthogonality: (1,2)·(2,-1)=2-2=0, yes bad. So indeed, they can't be together. So allowed subsets: {}, {1}, {2}, {3}, {1,2}, {2,3} => 6. But answer is 5. Perhaps they consider that i and j are on bad terms if A_iA_j + B_iB_j = 0, including when i=j? But condition says i≠j, so not including self. So {1} is fine, {2} fine, {3} fine. So 6.
Wait maybe there is also conflict between (-1,1) and (2,-1)? dot = -2 -1 = -3, not zero. So that's fine.
Maybe the problem counts only non-empty subsets? The statement: "We will choose one or more of these sardines" meaning at least one sardine. So empty set is not allowed. So we need to count only non-empty subsets. Indeed, the sample output 5 matches 6-1=5. Because they said "one or more". So we need to count non-empty subsets. Good catch. The problem statement: "We will choose one or more of these sardines". So count subsets of size at least 1. So we must subtract 1 from the total (empty set). In our total formula, we need to exclude empty. But careful: In our product of component counts, we will count empty set as part of the product (by taking empty from each component). Then plus the zero single selections (non-empty). So overall total including empty = (product over components of comp_count) + |Z|. Then subtract 1 to exclude empty. But wait, zero selections: exactly one zero is non-empty; but empty set is not among those. So total non-empty = (product over components of comp_count) - 1 (the empty selection from components) + |Z|. Because the product includes the empty selection; we subtract it. Alternatively, we can compute non-empty directly: For non-zero part, we can take product over components of comp_count, but that product includes the all-empty case. So we need to subtract 1 from that product to exclude the case where we pick no non-zero, then add |Z| for the subsets with exactly one zero (which are non-empty). However, careful: The subsets that are empty (neither zero nor non-zero) are exactly the all-empty from product. So total non-empty = (product over components of comp_count) - 1 + |Z|.
But also note: Could there be subsets that include zero and also non-zero? Not allowed. So that's it.
But is there any interaction from zero vectors that might also conflict with each other within themselves? Our handling: we count exactly one zero as allowed. But what about selecting two zeros? Not allowed. So |Z| counts number of single-zero subsets. So okay.
Now verify: product comp_count = 3*2=6, minus 1 =5, plus |Z|=0 =>5. Correct.
Now check second sample to verify later.
Thus solution:
- Count zero vectors: Z = number of (A_i, B_i) = (0,0).
- For non-zero vectors, we need to group by "direction" such that two vectors are considered in same group if they are collinear (i.e., (A_i, B_i) is a scalar multiple of each other). However careful: Since coordinates are large (up to 1e18), and can be negative, we need to normalize to a canonical representation that is unique for all vectors that are collinear.
We also need to identify for each direction its perpendicular direction, to pair groups. Because if both direction and its perpendicular exist, they form a component. Otherwise it's a single group.
We need to compute for each direction group its size. Then for each component: if it's a single group with size s (no perpendicular group present), comp_count = 2^s. If it's a pair of groups (G and H) with sizes s1, s2, comp_count = (2^{s1} + 2^{s2} - 1) (mod M). Then total_non_empty = (product over all components of comp_count) - 1 + |Z| (mod M). But careful: The product includes all components of non-zero groups. Also, note that zero vectors are separate; they are not in any component. So final answer = (prod comp_i) - 1 + Z (mod M). However, we must ensure modulo handling: (prod mod M - 1 + Z) mod M.
But also consider the scenario where there are zero vectors and also non-zero groups. The empty set is excluded; we add Z for zero-only subsets. Also, we need to consider subsets that contain exactly one zero? Already counted. Are there any subsets that contain a zero and also some non-zero? No, because conflict. So okay.
Edge cases: Z may be zero; answer = prod -1 (mod M). If Z >0, we need to add Z.
But wait: Could there be also subsets that consist of exactly one zero and no non-zero: counted in Z. Could there be subsets that consist of multiple zeros? No, not allowed. So Z is correct.
Now, what about if there are only zero vectors? Then N = Z, and there are no non-zero groups. Then product over empty set is 1? Usually product of no components is 1 (empty product). Then total subsets (including empty) = 1 (empty) + Z? Because from our formula: (product over components) + Z = 1 + Z. But we need non-empty subsets: (1 + Z) - 1 = Z. That's correct: we can choose any single zero vector (since picking more than one is forbidden). So answer = Z mod M. Good.
Now, what about if there are no zero vectors and only non-zero groups? Then answer = product comp_i - 1.
Now we need to compute direction groups efficiently for up to 2e5 vectors.
Representation of direction: We can consider each vector (a,b) with (a,b) != (0,0). We want to map it to a normalized representation such that collinear vectors map to same key. Common approach: reduce (a,b) to a canonical ratio a/b or b/a? But need to handle zeros in components. Since a and b can be zero individually? For non-zero vector, not both zero. Could have a=0, b!=0; or b=0, a!=0. Also negative signs.
We need a unique representation for the line (through origin) ignoring magnitude and sign? Because vectors that are scalar multiples (including negative scalars) are collinear? Yes, they are collinear. For the purpose of orthogonality, note that v and -v are collinear and also have same perpendicular direction? Dot product of v with w being 0 is symmetric. If v and -v are in same group, that's fine because they are not orthogonal to each other (since dot(v, -v) = -|v|^2 ≠0). So grouping by line (including both directions) is correct. So we need to map each vector to its line (i.e., equivalence class under scalar multiplication by any non-zero real). Since coordinates are integers, we can reduce by dividing by gcd and fixing a sign to get a canonical representation.
For a vector (a,b) non-zero, define normalized as (a/g, b/g) where g = gcd(a,b) (positive gcd). But careful: gcd(0, b) = |b|. So if a=0, then g = |b|. Similarly if b=0, g=|a|. This yields a primitive integer vector. However, we need to consider sign: we want (a,b) and (-a,-b) to map to same representation. To do that, we can enforce a canonical sign: e.g., make the first non-zero coordinate positive. So if a > 0, keep as is; if a < 0, multiply by -1; if a == 0, then use b's sign: b > 0 keep, else multiply by -1. This gives a unique representation.
But we must be careful about large numbers: a,b up to 1e18, computing gcd is fine using 64-bit, but Python can handle big ints.
Now for perpendicular direction: For a given normalized vector (x,y), its perpendicular direction is (-y, x) or (y, -x). Both are collinear. We need to map that to a canonical representation as well. Then we can group.
Thus algorithm:
- Read N.
- Initialize Z = 0.
- For each i, read a,b.
- If a == 0 and b == 0: increment Z.
- Else: compute normalized representation (canonical) for direction d.
- Store d (as a tuple) and also compute its perpendicular canonical representation p.
- We'll need to group by d to count sizes.
But we need to pair groups: for each direction d, if its perpendicular p is present, then d and p form a pair. But we must be careful not to double count: we should iterate over groups and pair them. We'll use a dictionary map direction -> count. After processing all non-zero vectors, we have counts for each direction.
Now we need to combine into components: For each direction d that hasn't been processed, if there exists a direction p = perp(d) in the map, then we have a pair component with sizes cnt[d] and cnt[p]. The comp count = (2^cnt[d] + 2^cnt[p] - 1) mod M. Then mark both as processed. If p not present, then it's a single component: comp count = 2^cnt[d] mod M.
We also need to compute powers of 2 up to N modulo M, to compute 2^cnt.
But caution: The mapping of direction must be unique and consistent, and perpendicular must be computed in canonical form. However, there is a nuance: The perpendicular direction of d is not necessarily the same as d, but we need to ensure that the pairing is consistent: perp(perp(d)) = -d, which maps to same direction as d (since we collapsed sign). Let's test: d = (1,2) canonical. perp(d) = (-2,1). Canonical: first coordinate -2, we apply sign rule: first non-zero is -2 (<0) so multiply by -1 -> (2,-1). Then perp of that: (-(-1), 2) = (1,2)? Actually perp(2,-1) = (-(-1), 2) = (1,2). That's canonical? (1,2) first coordinate positive, good. So perp(perp(d)) maps back to d. So pairing is symmetric.
Thus we can safely pair.
Potential issues: For vectors that are themselves (0, b) with b>0? Normalized: (0,1) after reduction (since gcd(0,b)=b, divide -> (0,1) if b>0, else (0,-1) but sign rule: if a=0, look at b: b>0 keep (0,1); b<0 multiply -> (0,1) as well? Actually our rule: if a==0, then use b's sign: if b>0 keep (0,b); if b<0 multiply by -1 -> (0, -b). Since -b>0, so canonical is (0,1) for any non-zero b. So (0, b) becomes (0,1). Good.
Similarly (a,0) becomes (1,0) after sign rule: if a>0, (1,0); if a<0, multiply -> (1,0) since first coord positive after sign? Actually for (a,0) with a<0: gcd=|a|, reduced (-1,0). Apply sign: first non-zero coordinate -1 <0 -> multiply -> (1,0). So canonical (1,0). So consistent.
Now, perpendicular: (1,0) -> (0,1). (0,1) -> (-1,0) canonical (1,0). So they pair correctly.
Now, we must also consider zero vectors separately as discussed.
Now compute product mod M = 1e9+7.
Complexity O(N log(max(|A|,|B|))) due to gcd.
Now we need to verify with second sample.
Sample 2:
10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4
Let's compute manually maybe to see if answer matches 479.
We'll implement later in code to test, but let's reason.
First count zero vectors: none, as all non-zero? Check: any (0,0)? No.
Now group by direction.
We'll list vectors:
1: (3,2)
2: (3,2) duplicate
3: (-1,1)
4: (2,-1)
5: (-3,-9) -> simplified: divide by gcd(3,9)=3? Actually (-3,-9) gcd=3, reduced (-1,-3). Sign: first coord -1 <0 -> multiply -1 -> (1,3). So direction (1,3).
6: (-8,12) -> gcd(8,12)=4? 8 and 12 gcd 4, reduced (-2,3). Sign: first coord -2<0 -> (2,-3). Wait (2,-3) after sign? Actually multiply by -1: (2,-3). That is canonical? But our sign rule: if first non-zero coordinate is positive? first coord 2 >0, so okay. But then we have (2,-3). Could also represent as ( -2,3) -> after sign becomes (2,-3). So (2,-3) is canonical.
7: (7,7) -> gcd=7, reduced (1,1). canonical (1,1).
8: (8,1) -> gcd=1, reduced (8,1). But need canonical? (8,1): first coord positive, so (8,1). But could reduce further? gcd(8,1)=1, so it's primitive. So (8,1).
9: (8,2) -> gcd=2, reduced (4,1). (4,1) first coord positive -> (4,1).
10: (8,4) -> gcd=4, reduced (2,1). (2,1) first coord positive -> (2,1).
Now we need to also compute perpendiculars and see groupings.
But careful: The grouping by direction should be based on the canonical primitive vector. So we have counts:
(3,2): appears twice.
(-1,1) canonical? Let's compute (-1,1): gcd=1, sign: first coord -1<0 -> (1,-1). So direction (1,-1). But note (1,-1) is collinear to (-1,1). So vector 3 becomes (1,-1). But also check (2,-1) vector 4: (2,-1) gcd=1, sign: first coord 2>0 -> (2,-1). But (2,-1) is not collinear to (1,-1) because (2,-1) is 2*(1, -0.5)? Not multiple. So separate.
But (1,-1) and maybe other vectors like (7,7) is (1,1) perpendicular? We'll handle later.
Now list all directions with counts:
We'll compute properly:
Vectors:
1: (3,2) -> primitive? gcd(3,2)=1, first coord positive, so (3,2)
2: same -> (3,2) again
3: (-1,1) -> gcd(1,1? Actually gcd(1,1)=1) -> (-1,1) sign: first coord -1<0 -> multiply -1 => (1,-1). So direction (1,-1)
4: (2,-1) -> gcd(1)=? gcd(2,1)=1, sign: first coord 2>0 => (2,-1)
5: (-3,-9) -> reduce: divide by 3 -> (-1,-3), sign => (1,3)
6: (-8,12) -> gcd(4) -> (-2,3) sign => (2,-3)
7: (7,7) -> divide by 7 -> (1,1) sign => (1,1)
8: (8,1) -> (8,1) sign => (8,1)
9: (8,2) -> divide by 2 -> (4,1) sign => (4,1)
10: (8,4) -> divide by 4 -> (2,1) sign => (2,1)
Now we have directions with counts:
(3,2): 2
(1,-1): 1
(2,-1): 1
(1,3): 1
(2,-3): 1
(1,1): 1
(8,1): 1
(4,1): 1
(2,1): 1
Now compute perpendicular directions.
For each direction, compute canonical perpendicular.
Define function perp_canon(x,y): compute (x', y') = (-y, x). Then normalize to canonical primitive (reduce gcd, sign rule). This yields the direction key for perpendicular.
Compute:
-
(3,2): perp = (-2, 3). Reduce: gcd(2,3)=1, sign: first coord -2<0 => (2,-3). So perp direction = (2,-3). Check our list: (2,-3) is present count 1. So pair (3,2) with (2,-3).
-
(1,-1): perp = (1,1) because (-(-1), 1) = (1,1). Reduce: (1,1) is present count 1. So pair (1,-1) with (1,1).
-
(2,-1): perp = (1,2) because (-(-1), 2) = (1,2). Reduce: (1,2) gcd=1 sign positive -> (1,2). Is (1,2) present? Not in our list. So (2,-1) has no counterpart.
-
(1,3): perp = (-3,1) -> reduce: gcd(3,1)=1, sign: first coord -3<0 => (3,-1). So perp direction (3,-1). Not present? Check list: we have (3,2), (2,-1), etc. (3,-1) not present. So single.
-
(2,-3): perp = (3,2) (as computed). This is already paired with (3,2). So we'll handle pairing.
-
(1,1): perp = (-1,1) -> canonical (1,-1). So pair with (1,-1) already.
-
(8,1): perp = (-1,8) -> reduce: gcd(1,8)=1, sign: first coord -1<0 => (1,-8). (1,-8) not present. So single.
-
(4,1): perp = (-1,4) -> canonical (1,-4). Not present.
-
(2,1): perp = (-1,2) -> canonical (1,-2). Not present.
Now we have pairs:
- Group A: (3,2) with size 2 and (2,-3) with size 1.
- Group B: (1,-1) size 1 and (1,1) size 1.
Other singles:
(2,-1) size 1
(1,3) size 1
(8,1) size 1
(4,1) size 1
(2,1) size 1
Now compute component counts:
For pair (3,2)+(2,-3): sizes 2 and 1 => comp = 2^2 + 2^1 - 1 = 4+2-1=5.
For pair (1,-1)+(1,1): sizes 1 and 1 => comp = 2+2-1=3.
Single (2,-1): 2^1=2
Single (1,3): 2^1=2
Single (8,1): 2
Single (4,1): 2
Single (2,1): 2
Product = 5 * 3 * 2 * 2 * 2 * 2 * 2 = 53=15; 152=30; 302=60; 602=120; 1202=240; 2402=480. So product = 480. Z=0. Non-empty total = product -1 = 479. Indeed matches sample output 479. Perfect.
Thus solution is correct.
Now we need to be careful about modulo: M = 1e9+7. Powers of 2 up to N (2e5) precompute mod M. Also product mod M.
Now we need to be careful about the zero vectors: Z can be up to N, but answer includes Z mod M. That's fine.
Now consider case where there are both zero and non-zero vectors. Example: Suppose we have Z=2, and some non-zero groups. Then answer = (product comp_i - 1 + Z) mod M. Need to ensure product mod M and subtraction handled.
But what about when product - 1 + Z may become negative? We'll add M to adjust.
Now we should confirm that the approach of treating components independently is valid given that there are no edges between components. Indeed, no conflict between vectors from different direction groups unless they are perpendicular. But we have paired all perpendicular groups into components. For a direction d that does not have its perpendicular present, there are no vectors that conflict with it from other groups. So any selection from that group is independent of selections from other groups. However, we must also consider that a direction might be perpendicular to more than one group? As argued, only one perpendicular direction line. But could it be that two different canonical directions are actually the same line? Our normalization ensures each line has a unique key. So each direction has exactly one perpendicular direction key. So conflict only arises between d and its perpendicular if both present.
Thus independent components.
Now we need to think about potential overflow or performance issues. With N up to 2e5, we can process each vector, compute its canonical representation, store counts in dictionary. Then compute powers.
Edge cases:
- Vectors that are (0,0) we already count as Z.
- Vectors with one coordinate zero: handled by normalization.
- Vectors with large gcd: we reduce.
Now we must be careful: When we compute perpendicular, we might get the same canonical representation as the original? Could that happen? For a vector to be collinear with its perpendicular, we would need (a,b) proportional to (-b,a). This implies a = k*(-b) and b = ka for some k. Substituting: a = -k b, b = k a => b = k (-k b) = -k^2 b => b(1+k^2)=0 => b=0, then a = -k0=0, so a=0, but then vector zero. For non-zero, impossible. So no self-perpendicular non-zero.
Now about zero vectors: Are there any conflicts among zero vectors and themselves? Yes, they conflict with each other. So we can't have two zero vectors. So we treat as described. But also, note that zero vectors conflict with all non-zero vectors. So selection of zero excludes any non-zero. So our counting is correct.
Now what about vectors that are not zero but might be orthogonal to themselves? Dot product of a vector with itself is a^2+b^2, which is zero only if both zero. So no self conflict.
Now we also need to consider whether we need to treat the empty set specially as per problem (one or more). So final answer = (product comp_i - 1 + Z) % MOD.
Now we need to be careful: If Z > 0, the empty set is not allowed, but subsets containing exactly one zero are allowed, and subsets containing non-zero are allowed as long as at least one selected from non-zero components? But product comp_i includes empty selection from all components, which yields empty subset of non-zero vectors. That case combined with no zero yields empty set. We subtract 1 to remove empty set. Then we add Z to account for subsets containing exactly one zero (and no non-zero). Those subsets are non-empty. Also, what about subsets containing zero and also some non-zero? Not allowed. So total non-empty subsets = (product comp_i) - 1 + Z. But note that product comp_i includes all choices from components, including those where we pick some non-zero vectors. That's good.
But is it possible that we have both zero and non-zero selections? No, because conflict prohibits it. So we don't double count.
Now what about the case where Z > 0 and there are no non-zero groups? Then product comp_i is 1 (empty product). Answer = 1 - 1 + Z = Z. That's correct.
Now if Z = 0 and no non-zero? Not possible because N>=1, so at least one vector. But if N=0? Not in constraints.
Now we must implement.
Implementation steps:
- Read N.
- Initialize Z = 0.
- Use a dictionary dir_count: map from canonical tuple (x,y) to count.
For each vector (a,b):
if a==0 and b==0: Z += 1
else:
(x,y) = canonical(a,b)
increment dir_count[(x,y)]
- Precompute pow2[0..N] modulo MOD. (N up to 2e5, fine)
- Process groups: We'll have a set of processed keys.
product = 1
For each key in dir_count:
if key in processed: continue
Compute perp_key = canonical_perp(key) # function that returns canonical perpendicular direction
if perp_key in dir_count and perp_key != key (shouldn't be equal):
# they form a pair
cnt1 = dir_count[key]
cnt2 = dir_count[perp_key]
comp = (pow2[cnt1] + pow2[cnt2] - 1) % MOD
product = product * comp % MOD
processed.add(key)
processed.add(perp_key)
else:
# single group
cnt = dir_count[key]
comp = pow2[cnt] % MOD
product = product * comp % MOD
processed.add(key)
- After loop, answer = (product - 1 + Z) % MOD
Ensure positive: (product - 1 + Z) % MOD.
But careful: When we have a pair, we need to ensure we don't double count when we encounter perp_key later. That's why we mark both as processed.
Now we need to implement canonical normalization and perpendicular.
Function canonical(a,b):
If a==0 and b==0: return (0,0) maybe, but we won't call for zero vector.
Compute g = gcd(a,b). Since we need positive gcd, use math.gcd which returns non-negative. Actually gcd(0,b)=abs(b). So g = math.gcd(a,b) after taking absolute? math.gcd returns non-negative gcd for integers, and gcd(0,b)=abs(b). So g = gcd(a,b). But a,b may be negative, gcd returns positive (abs). Good.
a //= g; b //= g.
Now apply sign normalization: we want a unique representation for the line (including both directions). So we want (a,b) and (-a,-b) to be same. Standard method: enforce that the first non-zero coordinate is positive. So if a > 0: leave. elif a < 0: multiply both by -1. elif a == 0: then check b: if b > 0: leave; else (b < 0) multiply both by -1. Since b cannot be 0 because if a=0, b cannot be 0 (non-zero vector), so b !=0. So that's fine.
return (a,b)
But note: after division, a,b are integers, could be large but within 2e5 after division? Actually they may be large (up to 1e18/g). But still fits in Python int.
Now function perp_canon(key):
(x,y) = key
compute (nx, ny) = (-y, x)
Then return canonical(nx, ny). Since canonical handles reduction.
But note: If (nx, ny) might be (0,0)? Only if x=y=0, but key is non-zero direction, so not zero.
Now we must ensure that for a direction that is already canonical, its perpendicular may not be primitive? But canonical will reduce it.
Now we need to consider that the dictionary keys are canonical tuples, so perp_key may not be in the dictionary if there is no vector with that direction.
Now we must be cautious about potential overflow in product; we do modulo.
Now test with sample.
Now consider cases with large Z. Answer modulo.
Now check correctness for edge cases.
But is there any subtlety about the fact that two vectors that are collinear but one is a negative multiple are considered same direction? Yes, we treat them as same group. That's correct because they are not orthogonal (unless zero). And for the purpose of perpendicular pairing, we need to treat direction as line (undirected). For perpendicular, we consider the line perpendicular. Since we collapsed sign, the perpendicular of (a,b) canonical is unique and also sign-collapsed.
Now we also need to be careful about vectors like (a,b) that are large but after reduction we may have many distinct groups. That's fine.
Now we might also need to consider that the perpendicular direction might map to the same key as the original if the vector is self-perpendicular? As argued, only zero. So fine.
Now we should also consider the possibility of duplicate vectors (same a,b) but they are distinct items. They fall into same group, which is fine.
Now final answer modulo.
But wait: Did we consider the possibility of having a direction group and its perpendicular group both present, but one of them may have been processed already when iterating? We'll handle with processed set.
Now complexity: O(N log Max) for gcd.
Now we also must consider the case where there are zero vectors and also non-zero vectors. But our product comp_i only counts selections from non-zero components. And then we add Z for zero-only selections. However, note: if we have zero vectors, can we also select the empty non-zero set (i.e., no non-zero) and then also select one zero? That's counted by adding Z. But is there any overlap with the product comp_i? The product includes the case of selecting no non-zero (empty selection from all components), which corresponds to subset of non-zero = empty. That case is counted in product as 1 (the empty product times the factor 1 from each component? Wait product over components: each component contributes its number of possible selections from that component, which includes the possibility of selecting nothing from that component. So the product counts the number of ways to select a subset of the non-zero vectors that respects constraints. Among these, the selection where we choose nothing from every component is exactly 1. So that corresponds to the empty set of non-zero vectors. So product includes that. Then we add Z to count subsets that have exactly one zero and no non-zero. That's fine.
Now is there any subset that includes zero and also some non-zero? Not allowed.
Now is there any subset that includes more than one zero? Not allowed.
Now what about subset that includes zero and also empty non-zero? That's the Z we counted.
Now verify with a simple test: N=1, vector (0,0). Then Z=1, product (no groups) = 1. Answer = 1-1+1=1. Valid: only one non-empty subset: {the sardine}. Good.
N=2, both (0,0): Z=2, product=1, answer=1-1+2=2. Valid subsets: either first alone, second alone. Not both together. So answer 2. Good.
N=2, one (0,0) and one non-zero (1,0). Then Z=1. Non-zero group: direction (1,0). No perpendicular present. comp=2^1=2. product=2. Answer = 2-1+1=2. Allowed subsets: {zero}, {non-zero}. Also {zero, non-zero} not allowed. Empty not allowed. So 2. Good.
N=2, both non-zero and perpendicular e.g., (1,0) and (0,1). Then directions: (1,0) and (0,1). They are perpendicular and both present. Group sizes: 1 and 1. comp = 2+2-1=3. product=3. Z=0. Answer=3-1=2. Allowed subsets: { (1,0) }, { (0,1) } (since together is forbidden). Also empty not allowed. So 2. Good.
N=2, both non-zero and not perpendicular, e.g., (1,0) and (1,1). directions: (1,0) and (1,1) not perpendicular. So two single groups: each comp=2. product=2*2=4. answer=4-1=3. Allowed subsets: any non-empty except? Actually there is no conflict, so all non-empty subsets: {1}, {2}, {1,2} =3. Good.
Now consider case where a group has size >1 and its perpendicular group also has size >1. Works.
Now think about potential issues with the product of component counts being huge; we take modulo.
Now, note that our component count for a pair uses 2^{s1} + 2^{s2} - 1. This could be computed modulo MOD. Also for single group: 2^s.
Now we need to precompute powers of 2 up to N. Since s can be up to N, but sum of sizes = number of non-zero vectors.
Now all good.
Now we should also consider potential case where there is a direction that appears, and its perpendicular direction also appears, but after reduction, the perpendicular direction might be the same as the original? As argued not. So fine.
Now implement.
But we also need to be mindful of large integer gcd with negative numbers: math.gcd works with absolute values; it returns non-negative. We'll compute g = math.gcd(a,b) after taking absolute? Actually math.gcd(a,b) returns the greatest common divisor of |a| and |b|. For Python, math.gcd(negative, positive) returns positive gcd. So we can do:
def normalize(a,b):
if a