magicforest

Time limit: 2s Memory limit: 256MB Input: Output:

Task

In a magical forest far-far away lives the Miraculous Deer. It likes to take magical walks along the trails of the forest. You're wondering, how many distinct magical walks could it take?

The forest contains NN trees and there are MM trails connecting tree aia_i and bib_i with a magical value of cic_i. A walk starts at a tree and goes through a number of trails arriving at a tree. It might visit the same tree multiple times. The length of the walk is the number of trails it uses. A walk of length kk is said to be a magical walks if the magical values of the trails it visits in order are m1,m2,,mkm_1, m_2, \ldots, m_k and:

  • k1k \geq 1,
  • mi+1=mi+1m_i+1=m_{i+1} for all 1ik11 \leq i \leq k-1.

Two magical walks are different if the set of their trails are different.

Write a program that calculates number of different magical walks modulo 109+710^9+7!

Input

The first line contains the integers NN (2N500 0002 \le N \le 500\ 000) and MM (1Mmin(N(N1)2,1 000 000)1 \le M \le \min\left( \frac{N(N-1)}{2}, 1\ 000\ 000 \right)).

The following MM lines contains three integers aia_i, bib_i and cic_i (1aibiN1 \le a_i \neq b_i \leq N for each trail; 1ci1091 \le c_i \leq 10^9 for each trail) representing a trail between trees aia_i and bib_i with a magical value of cic_i.

Any pair of meadows is connected by at most one trail.

For tests worth 77 points: ai=ia_i=i and bi=i+1b_i=i+1 for all trails and M=N1M=N-1.

For tests worth 99 points: ci3c_i \leq 3 for all trails.

For tests worth 1414 points: N22N \le 22 and M22M \leq 22.

For tests worth 2020 points: N1000N \le 1000 and M5000M \le 5000.

For tests worth 5050 points: No additional limitations.

Output

You need to write a single line with an integer: the number of magical walks modulo 109+710^9+7.

stdin

4 4
1 2 1
2 3 2
3 4 3
1 3 2

stdout

10

stdin

4 3
1 3 3
3 4 2
3 2 1

stdout

5

stdin

3 3
1 2 1
2 3 2
3 1 3

stdout

6

Notes

In the first sample case the 1010 magical walks are:

  • length of 11: 121 - 2, 232 - 3, 313 - 1, 343 - 4,
  • length of 22: 1231 - 2 - 3, 2132 - 1 - 3, 2342 - 3 - 4, 1341 -3 - 4,
  • length of 33: 12341 - 2 - 3 - 4, 21342 - 1 - 3 - 4.

In the second sample case there are 33 magical walks of length 11, and 22 of length 22.

In the third sample case there are 33 magical walks of length 11, and 22 of length 22 and 11 of length 33.

Log in or sign up to be able to send submissions!