K. Find a Cycle
time limit per test
15 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a directed graph with $$$n$$$ vertices and $$$m$$$ edges. There may be multiple edges between same pair of vertices. Self-loops are also allowed in this graph. The vertices are labeled by $$$1,2,\dots,n$$$. The length of the $$$i$$$-th edge $$$u_i\rightarrow v_i$$$ is $$$998\,244\,353^{w_i}$$$.

Your task is to find a cycle in the given graph such that the average length of all edges on the cycle is minimized, or determine there is no cycle.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1\leq n\leq 1000$$$, $$$1\leq m\leq 2000$$$), denoting the number of vertices and edges.

Each of the following $$$m$$$ lines contains three integers $$$u_i,v_i$$$ and $$$w_i$$$ ($$$1\leq u_i,v_i\leq n$$$, $$$0\leq w_i \lt n$$$), describing an edge.

It is guaranteed that there are at most $$$10$$$ cases such that $$$n \gt 100$$$ or $$$m \gt 100$$$.

Output

For each test case, output a single line containing an integer, denoting the average length of all edges on the cycle that you find.

More precisely, if the answer is $$$\frac{p}{q}$$$, you should output the minimum non-negative integer $$$r$$$ such that $$$q \cdot r \equiv p \pmod{(10^9+7)}$$$. You may safely assume that such $$$r$$$ always exists in all test cases. But if there is no cycle, please print "-1" instead.

Example
Input
6
3 3
1 2 0
2 3 0
3 1 0
2 1
1 2 0
2 2
1 2 0
2 1 1
3 5
1 2 0
1 3 1
2 3 2
3 2 0
2 3 1
4 5
1 2 3
2 3 3
3 1 3
2 4 1
4 1 1
1 1
1 1 0
Output
1
-1
499122177
499122177
540815376
1