Under construction

Still being built. Things may change

Finite Walking

Time limit2sMemory limit256 MB

Summary
Given a graph whose edges have counters taken modulo a_i, count the distinct final counter arrays reachable by walks from any start, modulo 10^9+7.
Level

Hard8 of 10

Topics
Graph, DFS, Number theory, Math
Solved
No attempts yet

Problem

You are given an undirected graph GG with nn vertices and mm edges. Loops and multiple edges are allowed. Each edge has a positive integer aia_i assigned to it, and a counter bib_i which is initially zero.

Consider the following process on this graph: we choose the starting vertex v1v_1 arbitrarily, then we go along some edge e1e_1 starting in v1v_1 and ending in v2v_2, then we go along some edge e2e_2 from v2v_2 to v3v_3 and so on. In other words, we follow some path in the graph, which may contain some edges and vertices multiple times. Every time we traverse edge ii in any direction, its counter bib_i changes to (bi+1) mod ai(b_i + 1) \bmod a_i. We can stop the process after any number of steps (possibly, zero).

Let us call the array (b1,b2,…,bm)(b_1, b_2, \ldots, b_m) after this process a configuration array. Consider all possible processes corresponding to all possible finite paths in GG. How many different configuration arrays can they produce? Two configuration arrays are considered different if they differ in at least one element. As the answer can be quite large, give it modulo 109+710^9 + 7.

Input

The first line of input contains two space-separated integers nn, mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 0≤m≤4⋅1050 \le m \le 4 \cdot 10^5), the number of vertices and edges of the graph.

The next mm lines describe the edges, one per line: the ii-th edge is given by three integers uiu_i, viv_i, aia_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 1≤ai≤1091 \le a_i \le 10^9), the endpoints of the edge and the number assigned to it. Vertices are indexed starting from 1.

Note that loops and multiple edges are allowed.

Output

In the only line print the number of different configuration arrays modulo 109+710^9 + 7.

Examples1

  1. Example 1

    Input
    5 5
    1 2 2
    2 3 2
    3 4 2
    4 5 2
    5 1 1
    
    Expected output
    14