The Overdosing Ubiquity
Contest: Codeforces Round 439 (Div. 2)
Difficulty rating: 2800
Tags: brute force, dfs and similar, graphs

The fundamental prerequisite for justice is not to be correct, but to be strong. That's why justice is always the victor.

The Cinderswarm Bee. Koyomi knows it.

The bees, according to their nature, live in a tree. To be more specific, a complete binary tree with n nodes numbered from 1 to n. The node numbered 1 is the root, and the parent of the i-th (2 ≤ i ≤ n) node is $$\left\lfloor \frac { i } { 2 } \right\rfloor$$. Note that, however, all edges in the tree are undirected.

Koyomi adds m extra undirected edges to the tree, creating more complication to trick the bees. And you're here to count the number of simple paths in the resulting graph, modulo 109 + 7. A simple path is an alternating sequence of adjacent nodes and undirected edges, which begins and ends with nodes and does not contain any node more than once. Do note that a single node is also considered a valid simple path under this definition. Please refer to the examples and notes below for instances.

Input format:
The first line of input contains two space-separated integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 4) — the number of nodes in the tree and the number of extra edges respectively.

The following m lines each contains two space-separated integers u and v (1 ≤ u, v ≤ n, u ≠ v) — describing an undirected extra edge whose endpoints are u and v.

Note that there may be multiple edges between nodes in the resulting graph.

Output format:
Output one integer — the number of simple paths in the resulting graph, modulo 109 + 7.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, the paths are: (1); (2); (3); (1, 2); (2, 1); (1, 3); (3, 1); (2, 1, 3); (3, 1, 2). (For the sake of clarity, the edges between nodes are omitted since there are no multiple edges in this case.)

In the second example, the paths are: (1); (1, 2); (1, 2, 3); (1, 3); (1, 3, 2); and similarly for paths starting with 2 and 3. (5 × 3 = 15 paths in total.)

In the third example, the paths are: (1); (2); any undirected edge connecting the two nodes travelled in either direction. (2 + 5 × 2 = 12 paths in total.)

Example input:
3 0

Example output:
9

Example input:
3 1
2 3

Example output:
15

Editorial:
Iterate over all possible combination, order and direction of extra edges.
There are no more than O(m!2m) ways to go through these extra edges, each of which will bring us at most O(n2) more simple paths. If we count all these simple paths using simple depth-first search, the time complexity will be O(n2m!2m), which is the same as the order of the answer.
However, we can reduce the original graph to a pretty small one, for example, by keeping all the nodes on some cycle and compressing the others. Noticing that the longest simple path on a complete binary tree is just $${\mathcal {O}}(\log n)$$, the compressed graph will contain at most $${\mathcal {O}}(m\log n)$$ nodes. Running simple depth-first search on such a small graph will lead to an $$O\left( (m \log n)^2 m! 2^m \right)$$ solution, which fits in with the small constraint of m and works really fast in practice.