방향 그래프에서 모든 두 정점 사이에 연속한 위치를 잇는 간선만 존재하도록 하는 순열의 개수를 세고, 개수가 n 이하이면 그 값들을 사전순으로 출력한다.
어려움9그래프조합론구현위상 정렬아직 제출이 없습니다시간 제한4초메모리 제한512 MBYou are given a directed graph with n vertices and m edges. The vertices are labeled from 1 to n. You need to find all the permutations of vertices p_1,p_2,…,p_n satisfying the following constraint:
We define the value of a permutation p_1,p_2,…,p_n as (∑_i=1np_i⋅10n−i)mod(109+7).
Output the number of such permutations modulo 109+7. If the number of such permutations is not greater than n, you also need to consider them all in lexicographical order, and output their values in this order.
The first line contains an integer T (T≤105) indicating the number of test cases.
For each test case, the first line contains two integers n and m (n≥1, m≥0, 1≤∑n≤5⋅105, 1≤∑m≤106).
Each of the following m lines contains two integers u and v (1≤u,v≤n, u=v) indicating that there is a directed edge from u to v in the graph. Note that the graph can contain parallel edges.
For each test case, output the number of the permutations modulo 109+7 in the first line. If the number of permutations is not greater than n, print another line with space-separated values of all the permutations, considered in lexicographical order. You \textbf{don't need to} output an empty line if the number is greater than n or there is no solution.