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 trees and there are trails connecting tree and with a magical value of . 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 is said to be a magical walks if the magical values of the trails it visits in order are and:
- ,
- for all .
Two magical walks are different if the set of their trails are different.
Write a program that calculates number of different magical walks modulo !
Input
The first line contains the integers () and ().
The following lines contains three integers , and ( for each trail; for each trail) representing a trail between trees and with a magical value of .
Any pair of meadows is connected by at most one trail.
For tests worth points: and for all trails and .
For tests worth points: for all trails.
For tests worth points: and .
For tests worth points: and .
For tests worth points: No additional limitations.
Output
You need to write a single line with an integer: the number of magical walks modulo .
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 magical walks are:
- length of : , , , ,
- length of : , , , ,
- length of : , .
In the second sample case there are magical walks of length , and of length .
In the third sample case there are magical walks of length , and of length and of length .