Under construction

Still being built. Things may change

Hamilton Path

Interview

Time limit4sMemory limit512 MB

Summary
Count the permutations whose consecutive pairs are exactly the directed edges of the graph, with no other edges, and list their values when there are at most n.
Level

Easy3 of 10

Topics
Graph, Implementation
Solved
No attempts yet

Problem

A directed graph has nn vertices and mm edges. The vertices are labeled from 11 to nn. Find all permutations p1,p2,…,pnp_1, p_2, \ldots, p_n of the vertices that satisfy the following constraint.

  • For all 1≤i<j≤n1 \leq i < j \leq n, the edge (pi,pj)(p_i, p_j) exists if and only if j=i+1j = i + 1.

The value of a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n is (∑i=1npi⋅10n−i) mod (109+7)\left(\sum_{i = 1}^n p_i \cdot 10^{n - i}\right) \bmod (10^9 + 7).

Output the number of such permutations modulo 109+710^9 + 7. If the number of such permutations is at most nn, also list their values in the lexicographical order of the permutations.

Input

The first line contains an integer TT (T≤105T \leq 10^5), the number of test cases.

For each test case, the first line contains two integers nn and mm (n≥1n \geq 1, m≥0m \geq 0, 1≤∑n≤5⋅1051 \leq \sum n \leq 5 \cdot 10^5, 1≤∑m≤1061 \leq \sum m \leq 10^6). Each of the following mm lines contains two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n, u≠vu \neq v), meaning there is a directed edge from uu to vv. The graph may contain parallel edges.

Output

For each test case, print the number of permutations modulo 109+710^9 + 7 on the first line. If the number of permutations is at most nn, print another line with the values of all the permutations, separated by spaces, in lexicographical order of the permutations. If the number is greater than nn or there is no solution, you do not need to print the empty line.

Examples1

  1. Example 1

    Input
    1
    5 6
    3 4
    2 5
    5 3
    1 3
    4 2
    5 1
    
    Expected output
    2
    13425 34251