<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

196. Matrix Multiplication
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



Let us consider an undirected graph G = <V, E> which has N vertices and M edges. Incidence matrix of this graph is an N × M matrix A = {aij}, such that aij is 1 if i-th vertex is one of the ends of j-th edge and 0 in the other case. Your task is to find the sum of all elements of the matrix ATA where AT is A transposed, i.e. an M × N matrix obtained from A by turning its columns to rows and vice versa.

Input

The first line of the input file contains two integer numbers — N and M (2 le N le 10,000, 1 le M le 100,000). 2M integer numbers follow, forming M pairs, each pair describes one edge of the graph. All edges are different and there are no loops (i.e. edge ends are distinct).

Output

Output the only number — the sum requested.

Sample test(s)

Input
4 4
1 2
1 3
2 3
2 4

Output
18

Author:	Andrew Stankevich, Georgiy Korneev
Resource:	Petrozavodsk Winter Trainings 2003
Date:	2003-02-06

<|response|>
1. Abridged Problem Statement  
Given an undirected simple graph with N vertices and M edges, form its incidence matrix A of size N×M, where A[i][j]=1 if vertex i is an endpoint of edge j, and 0 otherwise. Compute the sum of all entries of the M×M matrix AᵀA.

2. Key Observations  
- AᵀA is an M×M matrix whose (j,k) entry equals the dot product of column j and column k of A.  
- Each column of A corresponds to an edge and has exactly two 1’s (its two endpoints).  
- For j = k (diagonal entries): dot product = 1+1 = 2. There are M such entries, contributing 2M.  
- For j ≠ k (off-diagonal entries): the dot product is the count of shared endpoints between edges j and k. In a simple graph, two distinct edges can share at most one vertex, so each off-diagonal entry is either 0 or 1.  
- The total of all off-diagonal entries (ordered pairs j≠k) equals the total number of ordered pairs of distinct edges that meet at a common vertex.  
- For a vertex v of degree d_v, there are d_v incident edges, which yield d_v·(d_v−1) ordered pairs of distinct edges around v. Summing over all vertices gives ∑_v d_v·(d_v−1).

3. Full Solution Approach  
a. Read N, M.  
b. Initialize an array deg[1..N] to zero.  
c. For each edge (u, v):  
   - Increment deg[u] and deg[v].  
d. Compute  
     sum_pairs = ∑_{v=1..N} deg[v] · (deg[v] − 1)  
e. The final answer is  
     answer = 2·M  +  sum_pairs  
f. Print answer.

Time complexity: O(N + M).  
Memory: O(N).

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    // deg[i] will store the degree of vertex i (1-based indexing)
    vector<long long> deg(N + 1, 0);

    // Read each edge, update degrees
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        // Increment degree counts
        deg[u]++;
        deg[v]++;
    }

    // Compute sum of deg[v] * (deg[v] - 1) over all vertices
    long long sum_pairs = 0;
    for (int v = 1; v <= N; v++) {
        // For vertex v, ordered pairs of distinct incident edges:
        // deg[v] choices for the first edge, (deg[v]-1) for the second
        sum_pairs += deg[v] * (deg[v] - 1);
    }

    // Diagonal contribution: each of the M edges contributes 2
    long long diagonal = 2LL * M;

    long long answer = diagonal + sum_pairs;
    cout << answer << "\n";

    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)

    # Read number of vertices N and edges M
    N = int(next(it))
    M = int(next(it))

    # Initialize degree array for vertices 1..N
    deg = [0] * (N + 1)

    # For each edge, increment the degrees of its endpoints
    for _ in range(M):
        u = int(next(it))
        v = int(next(it))
        deg[u] += 1
        deg[v] += 1

    # Compute sum of ordered pairs of edges sharing each vertex
    # For a vertex of degree d, there are d*(d-1) ordered pairs
    sum_pairs = 0
    for v in range(1, N + 1):
        d = deg[v]
        sum_pairs += d * (d - 1)

    # Add diagonal contribution: each edge contributes 2
    answer = sum_pairs + 2 * M

    # Output the result
    print(answer)

if __name__ == "__main__":
    main()
```