We need to understand the problem: We have an undirected graph (may have self-loops) with n vertices and m edges. A path (not necessarily simple) can traverse edges multiple times. A "good path" is defined as a path (starting and ending at any vertices) that goes over exactly m-2 edges twice, and the other two edges exactly once. So overall the path uses each of the two edges once, and each of the other edges twice. Note that the path may traverse edges in any order. The path is counted by the set of two edges that are used exactly once; the order and direction don't matter. Two paths are considered the same if the set of edges traversed exactly once is the same. So essentially we need to count the number of unordered pairs of edges {e, f} (can e = f? No, because then we would have one edge used once and others used twice? But they said "the other 2 exactly once" meaning two edges are traversed exactly once each, so they must be distinct edges? Possibly they could be the same edge? But then we would have one edge counted as two edges? No, it's two edges; they are distinct. So we consider unordered pairs of distinct edges) such that there exists a path that traverses exactly those two edges once, all others twice, and possibly traverses edges multiple times, but overall exactly twice for m-2 edges, once for the chosen two.
We need to count the number of edge pairs for which such a path exists.
Interpretation: The path is essentially an Eulerian trail in a multigraph where we duplicate each edge except the two special ones? But careful: The path uses each edge some number of times; we can think of constructing a multigraph where we replace each edge with multiplicity equal to the number of times it is traversed (either 1 or 2). Then the path becomes an Eulerian trail in that multigraph (since it traverses each edge exactly as many times as it appears). For the path to exist, the multigraph must be connected (or at least the edges used must be in a connected component that contains the start and end, but since the path must traverse all edges exactly as given, all edges with positive multiplicity must be in one connected component that the path covers; also it must satisfy Eulerian trail conditions: either all vertices have even degree (for a circuit) or exactly two vertices have odd degree (for a trail that starts at one odd and ends at the other). Since the path can start and end anywhere, it's an Eulerian trail (not necessarily circuit). The multigraph formed by taking the original graph and duplicating each edge except the two special ones (i.e., those two edges are taken once, others twice) must have an Eulerian trail.
Thus, for a given set of two edges {e1, e2}, we consider the multigraph G' where each edge e has multiplicity: mult(e) = 2 if e not in {e1, e2}; mult(e) = 1 if e in {e1, e2}. We need to check if G' has an Eulerian trail.
Recall: A connected multigraph (or one where all edges are in a single connected component) has an Eulerian trail iff the number of vertices with odd degree is either 0 or 2. For an Eulerian circuit (start=end) all degrees even; for a trail, exactly two odd.
So we need to consider the degree of each vertex in G'. The degree in G' is the sum of multiplicities of incident edges. Let deg(v) in original graph be d(v) (counting each self-loop? For a self-loop, it contributes 2 to the degree in the original graph? Actually in standard undirected graph, a self-loop contributes 2 to the degree of its vertex. But careful: In the problem statement, roads starting and ending in the same city exist. In graph terms, a self-loop at v contributes 2 to the degree of v (since each end is at v). But in counting multiplicity for traversals, a self-loop is an edge. If we traverse it once, it contributes 2 to the degree of that vertex in the multigraph? Because each traversal of a self-loop adds 2 to the vertex degree? In Eulerian trail conditions, degree is counted as number of edge-ends incident. For a self-loop, it contributes 2 to the degree. So for our multiplicities: if a self-loop has multiplicity k, then it contributes 2k to the degree of its vertex. So we must be careful.
Thus, we need to compute for each vertex v, its degree in G' = sum_{edges incident to v} multiplicity(edge) * (2 if edge is a self-loop? Actually for normal edge (u,v) with u != v, it contributes 1 to degree of each endpoint per traversal. For self-loop (v,v), it contributes 2 per traversal. So it's easier to treat each edge as contributing to degree based on its type. Let's define for an edge e:
- If e is a non-loop between u and v, then its contribution to deg(u) and deg(v) is equal to multiplicity.
- If e is a self-loop at v, its contribution to deg(v) is 2 * multiplicity.
Now, we need the parity of degrees: odd vs even. For Eulerian trail, the condition involves odd degree count.
Thus, given the set S of two edges (maybe loops), compute the parity of each vertex's total degree in G'. The multigraph may not be connected; but we also need connectivity: All edges with multiplicity > 0 must belong to a single connected component (in terms of the underlying graph? The Eulerian trail requires the graph to be connected when ignoring isolated vertices? Actually if there are isolated vertices (no incident edges with multiplicity > 0), it's fine; but all edges must be in one component. However, note that the original graph might be disconnected. If we duplicate edges, the multigraph inherits connectivity from the original graph: edges are present only where original edges exist. If the original graph is disconnected, then edges from different components are not connected; a path cannot include edges from two components because there is no connection between them. Therefore, for an Eulerian trail to exist covering all edges with positive multiplicity, the set of edges with positive multiplicity (i.e., all edges) must be in one connected component. Since we are considering all edges (all original edges are present with multiplicity 1 or 2), if the original graph is not connected (has multiple components with edges), then there is no path that traverses edges from different components, regardless of multiplicities, because you cannot travel between components without edges. So a necessary condition is that the original graph is connected (ignoring isolated vertices? Actually isolated vertices with no edges are fine). But the problem does not require graph to be connected. However, if it is disconnected, then there can be no single path that traverses all edges (even with repetitions) because the path is a walk that must be contiguous; to go from one component to another you would need an edge connecting them, which doesn't exist. So unless the graph is connected (i.e., the underlying graph of edges is connected) there cannot be any good path. But maybe there is a scenario where the graph is disconnected but you could still traverse all edges by starting in one component, finishing in another? No, because there is no connection between components. So the whole graph must be connected to allow a walk that covers all edges. However, we could consider that some components have no edges? If the graph has isolated vertices, they don't matter. So condition: the graph formed by edges must be connected. In example 2: n=5, m=3, edges: 1-2, 2-3, 4-5. That graph has two components (1-2-3 and 4-5). The answer is 0, consistent. In example 3: n=2, edges: self-loop at 1, edge 1-2; this graph is connected (since vertex 2 is connected to 1 via edge 1-2, and vertex 1 has self-loop and edge to 2). So connectivity is required.
Thus, for any candidate pair {e1, e2} to be valid, the original graph must be connected? Actually even if the original graph is connected, the multigraph G' might still be connected (since we didn't remove edges). Duplicating edges doesn't disconnect anything. So connectivity of original graph is necessary for any pair to be valid. So if the original graph is not connected, answer = 0. So we can assume connectivity for counting.
Now we need to count the number of unordered pairs of distinct edges such that the resulting multigraph degrees have 0 or 2 odd vertices.
Let's analyze parity contributions.
Let d(v) be degree in original graph (counting each loop as 2). Let l(v) be number of self-loops at v? Actually each self-loop contributes 2 to d(v). But we might need to treat loops separately because their parity contribution per traversal is 2 per multiplicity, which is even regardless of multiplicity parity? Wait: For parity of degree in G', consider an edge e. If e is a normal edge (u,v) with u!=v, then its contribution to the parity of deg(u) is multiplicity mod 2 (since each traversal adds 1). So if multiplicity is odd, it flips parity; if even, parity unchanged. For a self-loop, contribution to parity of its vertex is (2 * multiplicity) mod 2 = 0 always, because 2*anything is even. So self-loops never affect parity! Indeed, in any multigraph, self-loops contribute 0 mod 2 to the degree (since they add 2 each time). Therefore, self-loops are irrelevant for parity conditions (they always contribute even degree). However, they do affect connectivity and the existence of Eulerian trail? A self-loop at a vertex does not connect to any other vertex; but it's attached to that vertex. If the graph is otherwise connected, the self-loop is part of that component. So they are fine.
Thus, for parity, we only care about non-loop edges. But careful: If the edge is a loop, it cannot be one of the two edges used exactly once? The problem states edges are roads; loops are allowed. If a loop is one of the two edges, its multiplicity is 1. But because a loop contributes 0 mod 2 to degree parity, it doesn't affect odd/even count. So loops can be part of the pair without affecting parity condition. However, does traversing a loop once cause any issue for Eulerian trail other than parity? Possibly not; the loop adds an even degree increment (2), so it doesn't create odd degree. So loops are benign.
Thus, for a given set of two edges (which could be loops or normal edges), we need to compute odd-degree vertices in G'. Let's denote the original graph: vertices V, edges E. For each vertex v, let deg_normal(v) be the number of incident non-loop edges (i.e., count each edge connecting v to w≠v once). Let loop(v) be number of loops at v. Then total original degree d(v) = deg_normal(v) + 2loop(v). But parity of d(v) is parity of deg_normal(v) because 2loop(v) is even.
Now, in G', each normal edge e (u,v) has multiplicity either 2 (if e not in S) or 1 (if e in S). For a loop, multiplicity is either 2 or 1, but contribution to parity is 0 regardless. So the parity of vertex degree in G' is determined solely by normal edges: For each normal edge e incident to v, if e ∈ S (i.e., used once), then it contributes 1 mod 2; if e ∉ S, then multiplicity 2 contributes 0 mod 2. Also, loops contribute 0 mod 2 regardless. Therefore, parity of deg_G'(v) ≡ (number of incident normal edges that are in S) mod 2.
That is, for each vertex v, let a(v) = indicator (parity) = 1 if odd, 0 if even. Then a(v) = (∑_{e ∈ S, e incident to v, e non-loop} 1) mod 2. For loops in S, they don't affect parity.
Thus, the odd-degree vertices in G' are precisely those vertices that are incident to an odd number of the normal edges in S. If S contains a loop, it does not affect any vertex's parity. So the parity condition: The number of vertices with odd a(v) must be 0 or 2.
Now S is an unordered pair of edges (distinct). They can be loops, or one loop and one normal, etc.
We need to count how many unordered pairs of distinct edges satisfy that condition and also that the graph is connected? Actually connectivity condition is independent of S? Wait: The graph G' is the same underlying graph but with multiplicities. The existence of an Eulerian trail also requires that all edges with positive multiplicity belong to a single connected component. Since all edges are present (multiplicity > 0 for all original edges), the underlying graph must be connected. So connectivity must hold for the original graph. If the original graph is disconnected, no pair works, as discussed. So we can pre-check connectivity: if not connected, answer = 0.
But also, what if the original graph is connected but after selecting S, does G' become disconnected? No, multiplicities don't affect connectivity; edges are still present (even if multiplicity 0? but no, all edges have multiplicity at least 1). So connectivity remains the same. So connectivity condition reduces to original graph being connected.
Thus, the answer is: number of unordered pairs of distinct edges (from the m edges) such that the set S (the two edges) satisfies: the number of vertices that are incident to an odd number of the non-loop edges in S is either 0 or 2.
We need to count this efficiently for up to n,m ≤ 10^6.
Let's denote:
- total edges: m.
- among them, let L = number of loops (edges where u=v).
- Let N = m - L = number of non-loop edges.
We need to consider pairs of edges (e1, e2) from all m edges, distinct.
Case breakdown based on types:
- Both edges are loops.
- One loop and one non-loop.
- Both non-loop edges.
We need to count pairs satisfying parity condition.
First, note that loops do not affect parity. So:
-
If both edges are loops, S contains only loops; then there are no normal edges in S, so a(v)=0 for all v. So number of odd vertices = 0 → valid. Thus, all unordered pairs of distinct loops are valid.
-
If one edge is a loop and the other is a non-loop, then S contains exactly one normal edge e (non-loop). Then the parity a(v) is determined by that one edge: its two endpoints (if distinct) will have odd degree (since each is incident to that edge exactly once). If the non-loop edge is a loop? Wait non-loop means u != v. Then its two endpoints each get parity 1. So the number of odd vertices = 2 (if the endpoints are distinct; if it's a loop? but it's non-loop, so they are distinct). But what if the non-loop edge is a self-loop? That's contradictory; we treat loops separately. So for a non-loop edge, endpoints are two distinct vertices, both become odd. So odd count = 2, which is acceptable (since Eulerian trail can start at one odd and end at the other). However, there is a nuance: What if the two endpoints are the same? That would be a loop, but we are considering non-loop. So it's fine. Therefore, any pair consisting of one loop and one non-loop is valid? But need also to consider if the graph is connected? Already assumed graph is connected. So yes, any such pair yields odd count = 2, which is acceptable. However, wait: There is an edge case: What if the non-loop edge is a bridge and the loop is somewhere else? That doesn't affect Eulerian trail? The Eulerian trail condition only cares about degree parity and connectivity. Since graph is connected, all edges are in one component; G' remains connected. So it should be fine. So all such pairs are valid.
But is there any scenario where a single normal edge (used once) with a loop (used once) yields odd count not 2? Possibly if the normal edge is a self-loop? No. So always 2. So all pairs of one loop + one non-loop are valid.
-
If both edges are non-loop edges. Then S contains two normal edges (they could be the same? No, distinct). We need to compute the parity pattern: For each vertex v, a(v) = (number of edges in S incident to v) mod 2. Since there are two edges, each vertex can be incident to 0, 1, or 2 of them. Mod 2: 0 if incident to 0 or 2 edges; 1 if incident to 1 edge. So the vertices with odd degree are those that are incident to exactly one of the two edges. So the number of odd vertices can be:
- If the two edges are disjoint (no shared vertices): Then each edge contributes odd to its two endpoints, total 4 vertices with odd degree? Actually each endpoint gets 1. Since no overlap, each of the four vertices is incident to exactly one edge, so odd count = 4.
- If the two edges share a common vertex (i.e., they are adjacent, like form a path of length 2): Then let vertices: e1 = (a,b), e2 = (b,c) with b common. Then vertices: a incident to e1 only → odd; b incident to both e1 and e2 → even (2 mod2=0); c incident to e2 only → odd. So odd count = 2.
- If the two edges are the same? Not allowed.
- If the two edges are parallel? But there are no parallel edges.
- If the two edges are loops? That's not this case.
- If the two edges are such that they share both endpoints? That would be the same edge (if they are parallel) but not allowed. For simple graph, two distinct edges cannot have both same endpoints because that would be multiple edges. So only possibilities: share exactly one vertex (adjacent) or share none.
But also what about the case where the two edges are incident to the same vertex but also maybe one is a self-loop? Not considered. So only two subcases: disjoint (odd count=4) or adjacent (odd count=2). There's also the possibility that the two edges are both incident to the same vertex but are both loops? Not relevant.
Thus, for two non-loop edges, the parity condition (odd count 0 or 2) is satisfied only when the two edges share a common vertex (i.e., they are incident to the same vertex) resulting in odd count = 2. Also, could odd count be 0? That would require each vertex to be incident to an even number of edges from S, i.e., 0 or 2. Since we have two edges, the only way to have odd count 0 is if every vertex is incident to either 0 or 2 edges. With two edges, the vertices incident to edges are the endpoints. If the two edges share a vertex, then that vertex gets 2, odd=0? Wait: In the adjacent case, we saw that the common vertex gets 2 (even), and the two other endpoints get 1 each -> odd count=2. So odd count not 0. To have odd count 0, we would need each endpoint to be incident to an even number, i.e., 0 or 2. For two edges, if they are disjoint, the four endpoints each have 1 -> odd. If they share a vertex, that vertex has 2 (even) but the other two have 1 each -> odd count 2. So odd count can never be 0 with two distinct non-loop edges because total sum of degrees (in terms of edge incidence) from S is 4 (each edge contributes 2 incidence counts). The number of odd vertices must be even. Could it be 0? To have 0 odd, all vertex incidences are even. That would require that each vertex is incident to either 0 or 2 edges. Since the total incidence sum is 4, the only possibility is that there are either 2 vertices each incident to 2 edges, or 4 vertices each incident to 1? Wait, each vertex incident to 2 edges: then we would need exactly 2 vertices, each incident to 2 edges. That would mean both edges are between the same two vertices? But that would be parallel edges, which are not allowed (simple graph). Alternatively, one vertex incident to 4? Not possible with only 2 edges. So indeed with two distinct non-loop edges, odd count cannot be 0 because each edge's endpoints are distinct (no parallel edges). But what if the two edges are such that one is a loop? That's separate case. So for two non-loop distinct edges, odd count is either 4 (disjoint) or 2 (share a vertex). So the condition (0 or 2 odd) is satisfied only when they share a vertex, i.e., are incident at a common vertex.
But wait: Could there be a scenario where one of the edges is a loop? Already separate. So for two non-loop edges, valid if they are adjacent (share a vertex). However, consider the case where the graph is not connected? Already accounted.
Also, could there be a scenario where the two edges are such that one of them is a self-loop? That's the mixed case.
Thus, overall, valid unordered pairs {e1, e2} are:
- Any pair of two loops (if L >= 2): count = C(L,2).
- Any pair consisting of one loop and one non-loop: count = L * N.
- Any pair of two non-loop edges that are incident (share a common vertex).
But careful: Are there any restrictions if the graph is connected? Already we assumed connectivity. But is connectivity sufficient for existence of Eulerian trail given degree condition? In addition to degree parity, we also need that all edges with positive multiplicity are in a single connected component. Since graph is connected, and all edges are present, the multigraph is connected. However, there is a nuance: When we have multiplicities, connectivity is about the underlying simple graph? The underlying graph of G' is the same as original graph (same vertex set, same edges, though multiplicities are higher). Since all edges are still present, the underlying graph is unchanged and is connected. So it remains connected.
But wait: There is a special case: If the original graph is connected but consists solely of loops? Could that happen? For example, n=2, m=2, edges: (1,1) and (2,2). The graph is not connected because vertex 1 and 2 are not linked by any edge; loops don't connect vertices. The underlying graph is disconnected: vertices 1 and 2 are isolated? Actually vertex 1 has a loop, but that does not connect to vertex 2. So the graph is not connected (there is no path between 1 and 2). So that graph would be disconnected, so answer 0. But if we had n=1, m=2: both loops on vertex 1. Then the graph is connected (only one vertex). So connectivity holds.
Thus, we only consider connected graphs. But we should be careful: In a graph with loops, connectivity definition: A graph is connected if the underlying undirected graph (ignoring loops) is connected, i.e., there is a path between any two vertices using edges (including loops? loops don't help). So loops do not contribute to connectivity. So a graph with loops only on a single vertex is connected (since only one vertex). With multiple vertices, loops alone cannot connect them.
Thus, our counting as above assumes graph is connected. But what if graph is connected but has some vertex with degree condition that prevents Eulerian trail even if parity condition holds? For Eulerian trail, besides connectivity and parity, there is also requirement that the graph (multigraph) is connected when considering edges with positive multiplicity. That's satisfied. However, there is an additional subtlety: The Eulerian trail must traverse each edge exactly as per multiplicities. For a multigraph with loops and multiple edges, if it is connected and parity condition holds, there exists an Eulerian trail (starting and ending at the odd vertices if two odd, or any vertex if zero). That's classical result, also works for graphs with loops? The standard Eulerian trail theorem holds for graphs with loops if we treat loops as edges that contribute 2 to the degree. Indeed, a loop can be traversed in a trail, and it contributes 2 to degree, preserving parity. So yes, condition is sufficient.
But one more nuance: The path must be a trail (no repeated edges? Actually it can repeat edges, but we are constructing an Eulerian trail in the multigraph where each copy is distinct. That's allowed. So the theorem applies.
Thus, the count is as described.
But wait: Are there any cases where the two edges are both non-loop and share a vertex, but the resulting Eulerian trail might not exist because of some other reason? Let's test: Example 1: star graph with center 1 and leaves 2,3,4,5 (no loops). n=5, m=4 edges: (1,2),(1,3),(1,4),(1,5). L=0, N=4. According to our counting: valid pairs are non-loop adjacent edges. Since all edges share vertex 1, any two distinct edges are adjacent (share vertex 1). So number of unordered pairs of non-loop edges = C(4,2)=6. That matches answer 6. Good.
Example 2: disconnected, answer 0. Works.
Example 3: n=2, edges: (1,1) loop, (1,2) non-loop. So L=1, N=1. Graph is connected? Vertex 2 connected to 1 via edge, vertex 1 has loop; so connected. Counting: pairs of two loops: C(1,2)=0; mixed: LN=11=1; non-loop pairs: C(N,2)=0 since only one. So total=1. That matches answer 1. Good.
Thus, the formula seems correct: answer = (if graph connected) C(L,2) + L * N + (number of unordered pairs of distinct non-loop edges that share at least one common vertex).
But is there any other possibility for two non-loop edges to yield odd count 0? What about if the two edges are the same? Not allowed. Or if they are parallel? Not allowed. But what about if the graph has multi-edges? Problem statement says: "Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist." So no parallel edges (multiple edges between same pair of distinct vertices). But what about two edges that are both loops at the same vertex? They are loops, we counted them in C(L,2). They are considered adjacent? Actually loops at the same vertex don't share a vertex? They both are incident to the same vertex. But loops don't affect parity, and we counted them separately. Could a pair of loops also be considered as "non-loop"? No, loops are separate. So our classification is fine.
Now, but there is a subtlety: The Eulerian trail condition requires exactly 0 or 2 vertices of odd degree. For two loops, we got 0 odd, so valid. For loop+non-loop, we got 2 odd (the endpoints of the non-loop). For two non-loop adjacent, we got 2 odd. So all good.
But wait: Could there be a scenario where the two edges are both non-loop but are the same edge? Not allowed.
Thus, answer = as above, provided the graph is connected. If graph is not connected, answer=0.
Now we need to compute for large n,m up to 1e6. Input edges list. We must first check connectivity of the graph (ignoring isolated vertices with no edges). Since n up to 1e6, m up to 1e6, we can use DFS/BFS but recursion might be deep; use iterative BFS with adjacency list. However, memory: adjacency list for up to 1e6 edges (undirected) will have 2e6 entries, okay. But careful: n up to 1e6, but many vertices could be isolated. In connectivity check, we need to consider only vertices that have at least one incident edge? Actually graph is considered connected if for any two vertices that appear in edges (i.e., are part of the edge set), there is a path. Isolated vertices (with no incident edges) are irrelevant because they are not required to be visited; the path does not need to go to them. The Eulerian trail only covers edges; isolated vertices are not visited. So connectivity condition should be: the graph formed by the edges (i.e., the set of vertices that have at least one incident edge) must be connected. More formally, let the "non-isolated" vertices be those with degree >0 (including loops? loops give degree>0). Then these vertices must be in one connected component. It is possible that there are isolated vertices with no edges; they don't matter. So we can check connectivity only on the induced subgraph of vertices with at least one edge. However, typical connectivity on full vertex set would treat isolated vertices as separate components, which would cause graph to be disconnected even if all edges are in one component. So we should ignore isolated vertices. For example: n=3, m=2 edges: 1-2, 2-3. Vertex 3? Actually 3 has edge, so it's connected. But if we had vertex 3 isolated, then full graph disconnected but edges are all in component containing vertices 1 and 2, and vertex 3 is isolated. Does the path need to cover isolated vertex? No, the path only traverses roads. The problem doesn't require visiting all cities, only traversing all roads. So isolated vertices are irrelevant. So connectivity condition is that the graph of edges (i.e., the graph formed by the vertices that have at least one incident edge) is connected. So we need to check that.
Thus algorithm:
- Read n, m.
- Build adjacency list for the graph. But note: loops should be represented? For connectivity, loops don't connect to other vertices, but they are attached to a vertex; that vertex is part of the component. So for connectivity, we can just treat loops as having no effect on connectivity (they are just edges that start and end at same vertex). They don't create connections to other vertices. So they are fine.
- Determine which vertices have at least one incident edge (i.e., degree > 0, counting loops appropriately? For degree count: In adjacency list, we can store neighbors; for loops we could add the vertex to its own neighbor list? That might cause self-cycles but for BFS, we need to avoid infinite loops? Usually, for connectivity, a self-loop does not help to reach other vertices, but we must mark the vertex as visited when we start BFS. Including a self-loop in adjacency list (connecting to itself) can cause recursion if not careful but BFS can handle if we check visited. But more importantly, if a vertex only has a loop and no other edges, it's an isolated component? Actually it's a component consisting of just that vertex (with a loop). For our connectivity condition, all vertices with edges must belong to one component. So if there is a vertex with only a loop and no other edges, and another vertex with edges, they are separate components because there is no path between them. That would make the graph disconnected. So loops alone don't connect vertices. So we need to treat loops as not connecting to other vertices. In adjacency list for BFS, we can include the loop as an edge to itself, but that doesn't help reach other vertices; it's fine. However, if we include it, BFS from a vertex with a loop will still not reach other vertices. So it will treat it as its own component. So we need to verify that after BFS from any vertex with an edge, we visit all vertices that have degree >0. So loops are okay.
So we can compute degree (number of incident edges, counting loops once? Actually for degree we can count number of edges incident, loops counted once? But for connectivity we just need to know if vertex is part of the edge set. So we can maintain a list of vertices with any incident edge (including loops). Then run BFS/DFS from one such vertex, traversing only non-loop edges? Actually for BFS, we must traverse edges to neighbors. For loops, they are not useful for traversal to other vertices. But including them as neighbors (self) won't help. However, we must ensure we include edges to other vertices correctly. So we build adjacency list for non-loop edges only? But loops can be ignored for connectivity because they don't connect to other vertices. However, a vertex with only loops is still a vertex that has edges. It must be in the same component as others? If there is a vertex with only loops and no connection to other vertices, then it's a separate component and the graph is disconnected. So we need to detect that. But BFS from a vertex with non-loop edges will not reach that vertex because there is no path. So connectivity fails. That's correct.
Thus, we can construct adjacency list for all edges (including loops? For non-loop edges, add both directions; for loops, we could add self-loop or ignore for BFS because traversing a self-loop doesn't help leaving the vertex. However, for BFS we need to know that the vertex is part of the graph; but we only start BFS from a vertex with degree>0. For loops, if we start from a vertex that only has loops, we will mark it visited but won't go anywhere else. So to check connectivity among all vertices with any edge, we need to ensure that there is exactly one connected component among vertices with edges, considering only non-loop edges (since loops don't provide connectivity). But careful: Loops can be part of the component? If vertex A has a loop but also is connected via non-loop edges to the rest, then it's obviously in the same component due to those non-loop edges. So loops themselves don't affect component connectivity. So we can ignore loops for connectivity and just consider the graph of non-loop edges. However, if there is a vertex with only loops, then in the graph of non-loop edges it will be isolated (degree 0). But we need to consider it as a vertex with edges. In connectivity of the whole edge set, such a vertex would be a separate component because there is no path from it to any other vertex. So if there exists at least one vertex that has only loops (and no non-loop edges) and there exists any other vertex with any edge (including loops or non-loops), then the graph is disconnected. Also, if there are two vertices each having only loops, they are separate components because no connection between them. So the condition is: the graph formed by considering vertices and non-loop edges must have all vertices with any edge (including loops) in a single connected component? Wait: If a vertex has only loops, it has no incident non-loop edges; thus in the graph of non-loop edges, it's isolated. However, we cannot reach it from other vertices via any path because loops don't help. So indeed, for the entire graph (including loops) to be connected, there must be a path between any two vertices with edges. Since loops are not providing connections, the only way for a vertex with only loops to be connected to others is if it has some non-loop edge. Therefore, if there is any vertex that has at least one edge (loop or non-loop) but no non-loop edges, and there is any other vertex with an edge elsewhere, the graph is disconnected. So essentially, all vertices that have any edge must belong to the same connected component of the graph formed by non-loop edges. More precisely, define the graph G' = (V, E_nonloop). Then a vertex with only loops is an isolated vertex in G'. For the whole graph to be connected (in terms of having a path using edges, including loops? Loops don't help), there must be a path between any two vertices using edges; such a path cannot involve loops. So the connectivity of the whole graph is exactly the connectivity of G' (the graph without loops). However, if there are vertices that have only loops, they are isolated in G' and thus each forms its own component, making G' disconnected unless there is exactly one such vertex and no other vertices with edges? But then the whole graph would be just that vertex with loops, which is connected (since only one vertex). If there are multiple isolated vertices in G' that have loops, they are separate components, so whole graph disconnected. So we can just check connectivity on the graph ignoring loops, but also need to consider that vertices with degree zero (no edges at all) are irrelevant. So we can compute the set of vertices that have any incident edge (including loops). Then we check if the subgraph induced by these vertices (using only non-loop edges) is connected. If there are vertices that have only loops (i.e., have edges but no non-loop edges), then they are isolated in that subgraph, so the subgraph would have multiple components unless there is exactly one such vertex and no others? Actually if there is exactly one vertex that has any edges, and it may have loops, then the subgraph of non-loop edges might be empty (only that vertex, but no edges). In that case, the graph is considered connected (only one vertex). If there are multiple vertices with only loops, each is isolated, so they are separate components -> disconnected. So the condition is: the number of connected components in the graph consisting of all vertices (with at least one edge) and non-loop edges, must be exactly 1. But if there are vertices with only loops, they appear as isolated vertices in that graph; if there is more than one such isolated vertex, then we have multiple components. However, if there is exactly one vertex with edges, regardless of whether it has non-loop edges or loops, the graph is connected. So we need to handle that.
Simplify: We can run BFS/DFS on the original graph including loops? But loops won't help connectivity. We can treat loops as not adding neighbors. So we can build adjacency list only for non-loop edges. Then we find the set S of vertices with degree>0 (including loops). To compute this set, we can mark vertices that appear in any edge (including loops). Actually loops give that vertex in edge list. So S = { u | there exists an edge (u,v) in input } union { v }. So all vertices that appear as endpoints of any edge (including loops). For a loop (u,u), u appears. So S includes all vertices that have any edge. Then we run BFS from an arbitrary vertex in S, traversing only non-loop edges (so adjacency list). We'll mark visited vertices. After BFS, if all vertices in S are visited, then the graph (including loops) is connected. Because any vertex with only loops is in S, but will it be visited? If it has only loops, it has no non-loop edges, so in BFS from another vertex, we cannot reach it, thus it will not be visited. So that would indicate disconnected unless the start vertex itself is that vertex. But if S contains multiple vertices with only loops, BFS from one won't reach the others. So our condition becomes: BFS from some vertex in S using only non-loop edges must visit all vertices in S. But that's only true if the subgraph of non-loop edges connects all vertices in S. That requires that no vertex in S is isolated in the non-loop graph unless it's the only vertex. So indeed, we can check: count the number of connected components in the graph consisting of vertices S and non-loop edges. If it's >1, graph disconnected.
Alternatively, we can compute the number of connected components that contain at least one edge (including loops?). Actually we need to consider components that have edges. A component might consist of a single vertex with loops only; that's a component with edges. So if there is more than one such component, disconnected.
So we can compute components using union-find (DSU) over vertices with non-loop edges, but also need to account for vertices with only loops as separate components? DSU on non-loop edges will only union vertices that are connected via those edges. After processing all non-loop edges, each vertex that appears (in S) will belong to some set. Vertices not appearing in any non-loop edge but appearing in loops will be isolated (not connected to any other via DSU). So we can assign each such vertex its own component. Then we need to check if there is more than one component that contains any vertex from S (i.e., any vertex that has an edge). If yes, graph disconnected. So we can count the number of components that have at least one edge. That's easier: we can maintain a flag for each component whether it contains any edge. But we need to handle large n (1e6) and m (1e6), union-find with path compression is fine.
Simpler: Since n,m up to 1e6, we can also perform BFS using adjacency list, but we need to build adjacency list which might be memory heavy but acceptable (2*m entries). BFS also O(n+m). However, we need to also compute other statistics: count of loops L, count of non-loop edges N, and also the number of unordered pairs of non-loop edges that share a common vertex. That latter requires some computation: For each vertex, we need to know the number of non-loop edges incident to it (i.e., its degree in the simple graph, ignoring loops). Then the number of unordered pairs of distinct non-loop edges that share a vertex is sum over vertices of C(deg(v), 2). But careful: This counts each pair of edges that share a vertex multiple times if the two edges share two vertices? In a simple graph, two distinct non-loop edges can share at most one vertex. If they share both vertices, they would be parallel edges, which are not allowed. So each unordered pair of edges that are incident (share a vertex) is counted exactly once? Actually if two edges share a vertex v, then they are both incident to v. If they share two vertices? Not possible. So each pair is incident at exactly one vertex? Could they be incident at two vertices if they are the same edge? No. So each pair of edges that are adjacent (share a vertex) will be counted in the combination C(deg(v),2) for that common vertex. And since they share exactly one vertex, they will be counted exactly once. However, what about two edges that are both loops at the same vertex? Those are loops, not non-loop edges. So they are not counted here. So the number of unordered pairs of non-loop edges that are adjacent is exactly sum_{v} C(deg(v), 2), where deg(v) is the number of non-loop edges incident to v (each edge contributes to degree of both its endpoints). That's a standard formula for counting pairs of edges sharing a vertex. But careful: deg(v) counts each edge incident to v once. For an edge (u,v), it contributes to deg(u) and deg(v). So when we sum C(deg(v),2) over all v, each unordered pair of edges that share a vertex is counted exactly once because they share exactly one vertex. So that's correct.
But we need to ensure we only count unordered pairs of distinct edges. So indeed, the number of valid pairs of non-loop edges is sum_{v} C(deg(v), 2). However, is it always true that any two non-loop edges sharing a vertex are valid? According to our analysis, yes, because they produce odd count = 2. But is there any subtle case where the Eulerian trail might not exist despite parity and connectivity? Let's double-check: For two non-loop edges that share a vertex, we get odd vertices = the two endpoints that are not shared (if they are distinct) or could there be a case where the two edges share a vertex but the two other endpoints are the same? That would mean the two edges are (a,b) and (b,a) which are parallel, not allowed. Or (a,b) and (a,b) same edge. So not possible. Also if the two edges are (a,b) and (a,a)? The second is a loop, but that's not non-loop. So fine. So always the two odd vertices are the two endpoints that are not common. So odd count=2. That satisfies parity.
But what about the case where the two edges share a vertex and that vertex has degree 0 in the rest? That doesn't matter. The Eulerian trail exists if the graph is connected and parity condition holds. But is there any scenario where the graph is connected but after selecting the two edges, the Eulerian trail exists? We already reasoned yes. However, there might be an edge case: What if the graph is a tree (or acyclic) and the two edges share a vertex. Then G' is the original graph where all edges have multiplicity 2 except those two edges have multiplicity 1. Is it always possible to traverse all edges with those multiplicities? Let's test a small example: A path of 2 edges: vertices 1-2-3. Edges: e12 (1-2), e23 (2-3). Suppose we take S = {e12, e23}. Then multiplicities: e12=1, e23=1, others none (only two edges). So G' has edges: 1-2 and 2-3, each once. This is just a path from 1 to 3. An Eulerian trail exists (1-2-3). Yes. If there are additional edges elsewhere, all have multiplicity 2. Those double edges can be traversed as pairs. Intuitively, we can think of taking an Eulerian trail in the multigraph. Since it's connected and parity holds, there is always a trail.
But consider a graph with a bridge? That doesn't affect existence of Eulerian trail; it's still possible as long as the degree condition holds. For example, a graph that is a single bridge connecting two components each with even degrees? But if the graph is connected and we have multiplicities 2 for all edges except two, we might have to cross some edges twice. But standard Eulerian trail theorem ensures existence.
Thus, answer formula holds.
Now, one more nuance: The path must go over m-2 edges twice, and the other 2 exactly once. This implies that each edge is traversed at least once; no edge is left untraversed. So we are using all edges. That's what we did.
Now, check for possibility of multiple edges? Not allowed.
Also, what about the case where the two edges are loops? In that case, the odd count is 0. So we need to check connectivity: The graph must be connected. For loops only graph (e.g., n=1, m=2 loops), that's connected (single vertex). So pairs of loops are valid. In a graph with loops and other edges, connectivity still required.
Now, what about a pair consisting of two non-loop edges that are the same? Not allowed.
Now, are there any pairs where one or both edges are loops that could cause connectivity issues? If graph is connected, loops themselves do not affect connectivity. So all such pairs are valid.
Thus, the answer, given that the graph is connected, is:
ans = C(L,2) + L * N + sum_{v} C(deg(v), 2)
where deg(v) is the number of non-loop edges incident to v (i.e., degree in the simple graph ignoring loops). Also, we must consider if the graph is not connected: ans = 0.
But is there any scenario where the two edges selected are such that the multigraph G' is not connected even though the original graph is connected? That would be impossible because G' has exactly the same set of edges (though multiplicities differ) and thus the underlying simple graph is same, so connectivity unchanged. So no.
Now, we need to verify with some more examples.
Consider a graph that is connected but has a vertex with no non-loop edges, only loops, and other vertices with edges. For example: n=2, edges: (1,1) loop, (1,2) non-loop. That is connected because vertex 2 connected to 1 via edge. L=1, N=1, deg(1)=1 (edge 1-2), deg(2)=1. Sum C(deg,2) = 0+0=0. C(L,2)=0, L*N=1. Total=1. That's example 3. Works.
Consider graph: n=2, edges: (1,1) loop, (2,2) loop. That's two loops on different vertices. The graph is disconnected because there is no path between 1 and 2. So answer should be 0. Our check would detect disconnected, ans=0. Our formula would give C(2,2)=1, LN=20=0, sum=0 -> total 1, but we override to 0 because not connected. Good.
Consider graph: n=1, m=3: edges: three loops on vertex 1. L=3, N=0. Graph connected (single vertex). Then ans = C(3,2)=3. Are there indeed 3 good paths? Let's test manually: We have three loops: a,b,c. We need to choose two loops to traverse once, and the remaining one twice. Since all loops are on same vertex, does there exist a trail that goes through each loop with specified multiplicities? Starting and ending at the only vertex 1. Since loops contribute even degree each (2 per traversal), the total degree from loops: For the two loops with multiplicity 1, each contributes 2; for the one loop with multiplicity 2, contributes 4. Total degree at vertex 1 = 2+2+4=8, even. So Eulerian circuit exists. So any choice of two loops gives a valid good path. Number of unordered pairs of distinct loops = C(3,2)=3. So correct.
Consider graph: n=3, edges: 1-2, 2-3, and a loop on 2. That's connected. L=1, N=2. deg(1)=1, deg(2)=2 (edges 1-2 and 2-3) plus loop? deg for non-loop only: deg(2)=2, deg(3)=1. sum C(deg)= C(1,2)=0 + C(2,2)=1 + C(1,2)=0 =1. C(L,2)=0, LN=12=2. Total = 3. Let's enumerate pairs:
- Two loops? only one loop.
- Loop+edge12, Loop+edge23 -> 2 pairs (both valid).
- Two non-loop edges: edge12 and edge23 share vertex 2 -> valid, count 1.
So total 3. Check if any invalid? All should be valid if graph connected. Let's test a specific: choose edges {loop, edge12}. Then S: loop used once, edge12 once, edge23 twice. Compute degrees: edge12 once -> contributes 1 to deg of 1 and 2; loop once -> contributes 2 to deg of 2 (even, so 0 parity); edge23 twice -> contributes 2 to deg of 2 and 3 (even). So parity: vertex1 odd (1), vertex2? deg: from edge12:1, from loop:2 (even), from edge23:2 (even) total 1+2+2=5? Wait parity: we only care mod 2. 1 (edge12) mod2 =1, loop 2 mod2=0, edge23 2 mod2=0 => vertex2 odd? Actually edge12 contributes 1, loop contributes 0 (since 2 mod2=0), edge23 contributes 0 => vertex2 odd (1). Vertex3: edge23 contributes 0 => even? Actually edge23 twice gives 2 -> 0 mod2, so vertex3 even. So odd vertices: 1 and 2 => 2 odd, valid. Graph connected. So okay.
Now choose {edge12, edge23}. Then edge12 once, edge23 once, loop twice. Parity: edge12 1 -> v1 odd, v2 odd? Actually edge12 adds 1 to v1 and v2; edge23 adds 1 to v2 and v3; loop twice adds 0. So v1 odd, v2: 1+1=0 mod2 even, v3 odd. So odd vertices v1,v3 -> 2 odd, valid.
Now choose {loop, edge12} we did. Good.
Now what about a graph where the two non-loop edges are not adjacent? That would be invalid. So our counting via sum C(deg,2) counts only adjacent ones.
Now we must consider: Are there any cases where two non-loop edges that are not adjacent still yield a valid Eulerian trail? According to parity analysis, they give 4 odd vertices, which is not allowed. So invalid.
But wait, could there be a scenario where the two edges are not adjacent but the graph has some special structure that eliminates two of the odd vertices? For example, if one of the vertices is isolated? But if graph is connected, all vertices have degree at least 1? Not necessarily; a vertex could have degree 1 but it's endpoint of one of the selected edges; that vertex would be odd. The other selected edge also contributes two odd vertices. So total four distinct odd vertices? But could some of them coincide? If the edges share a vertex, then we get two odd vertices. If they are not adjacent, the four endpoints are distinct? Could they not be distinct if there is overlap? If the two edges are not adjacent, they share no vertices, so endpoints are four distinct vertices (since no parallel edges). However, what if some endpoint is the same because one edge is a loop? That's not non-loop. So for two non-loop edges, disjoint gives four distinct vertices, each odd. That yields 4 odd vertices, which is not 0 or 2, so invalid.
But could there be a scenario where one of those four vertices also has a loop that somehow changes parity? No, loops don't affect parity. So parity remains odd at those four vertices. Could there be a scenario where the graph has multiple edges? Not allowed. So it's invalid.
Thus our counting seems correct.
Now, we also need to consider the possibility that the graph has multiple components but one component has all edges? Actually if graph is disconnected, answer = 0. That's straightforward.
Now, we need to compute ans modulo? The problem asks to print the number, likely as a 64-bit integer. n,m up to 1e6, so C(L,2) could be up to ~5e11, LN up to 1e12, sum C(deg,2) could be up to O(m^2) in worst-case? Let's analyze maximum possible sum_{v} C(deg(v),2). For a graph with m edges, sum of deg(v) = 2N (since each non-loop edge contributes 2). The sum of C(deg(v),2) = sum deg(v)(deg(v)-1)/2. In the worst case, this can be large. For example, a star with center degree N, and N leaves each degree 1. Then sum = C(N,2) + NC(1,2)= N(N-1)/2. That's O(m^2) which for m=1e6 is about 5e11, fits in 64-bit (max about 5e11, plus other terms maybe up to 1e12, still fits in 64-bit? 1e12 is okay, 64-bit max is 9e18). But wait, N can be up to 1e6, so N(N-1)/2 ~ 5e11, plus LN could be up to 1e61e6 = 1e12, plus C(L,2) up to 5e11. Total up to ~2e12, still well within 64-bit (9e18). So we can use 64-bit integers (long long in C++). But careful: m up to 1e6, but L and N sum to m. So worst case L = m, N=0, then C(L,2) ~5e11. Or L=0, N=m, sum C(deg,2) can be up to about m^2/2 if graph is dense? But graph is simple, max degree is n-1, and sum of degrees = 2m. The maximum of sum C(deg,2) given fixed sum of degrees = 2m is achieved when the degree distribution is as uneven as possible, like one vertex has degree min(2m, n-1). But with n up to 1e6, the star graph with center degree = min(2m, n-1). Since m up to 1e6, 2m up to 2e6, and n can be up to 1e6, so center degree could be up to n-1 ~ 1e6, which is similar to m. So sum C(deg,2) ≤ C(m,2) roughly, if m ≤ n? Actually in a simple graph, maximum degree is n-1. For m edges, the maximum possible sum of C(deg,2) occurs when the graph is a star: center degree = m (if m <= n-1) and leaves degree 1. Then sum = C(m,2). If m > n-1, the maximum degree is limited to n-1. Then you can have a complete graph? Let's bound: The sum of squares of degrees is maximized when degrees are as unequal as possible given constraints. But since each vertex degree ≤ n-1 and sum of degrees = 2m, the maximum of sum deg(v)^2 occurs when as many degree as possible are at max, but we need to consider n up to 1e6. However, m also up to 1e6, so worst-case m=1e6, n=1e6, then a star can have center degree up to 999,999 (n-1) which is close to m. If m > n-1, you cannot have a star with all edges; you need multiple vertices with high degree. But still sum of C(deg,2) is at most something like O(m * sqrt(m)? Not sure. But we can compute safely using 64-bit. The maximum possible sum of C(deg,2) for simple graph with m edges is at most C(m,2) ? Actually each unordered pair of edges that share a vertex is counted once. The total number of pairs of edges sharing a vertex is at most total pairs of edges? That is C(m,2). Because each pair of edges either shares a vertex or not. So sum_{v} C(deg(v),2) is exactly the number of unordered pairs of edges that share a vertex. This cannot exceed C(m,2) because there are only C(m,2) pairs total. So sum_{v} C(deg(v),2) ≤ C(m,2) ~ 5e11. So total answer ≤ C(m,2) + LN + C(L,2) ≤ C(m,2) + something? Actually LN ≤ (m)(m) = m^2, but L+N=m, so maximum LN occurs when L = N = m/2, giving m^2/4 ~ 2.5e11, also ≤ C(m,2) for m large? C(m,2)=m(m-1)/2 ~ m^2/2, so L*N ≤ m^2/4 < m^2/2. So total answer < m^2, about 1e12 for m=1e6. So 64-bit is fine.
Now, we must also ensure that the graph is connected. If not, answer=0.
Now, we need to compute:
-
L = count of loops (u == v)
-
N = m - L
-
For each vertex, compute deg[v] = number of non-loop edges incident to v. For loops, we ignore for deg counting. Since edges are undirected, we increment deg[u] and deg[v] for each non-loop edge (u != v). Loops not counted.
-
Then sum_adjacent_pairs = sum_{v=1 to n} C(deg[v], 2) = sum deg[v]*(deg[v]-1)/2.
-
ans = C(L,2) + L * N + sum_adjacent_pairs, if graph connected, else 0.
But is there any other case? What about if L=0 and N=0? That would be m=0? But m ≥ 1? It can be 0? Constraints say 1 ≤ n, m ≤ 10^6. So m≥1, at least one edge. If m=1, then can we have a good path? We need to traverse m-2 = -1 edges twice? That's not possible. Wait m≥1, but condition m-2 edges twice: if m=1, then m-2 = -1, impossible. However, problem constraints m≥1, but do we consider m=1? The definition says path goes over m-2 edges twice and over the other 2 exactly once. For m=1, m-2 = -1, so it