Finite Walking
Time limit2sMemory limit256 MB
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 with vertices and edges. Loops and multiple edges are allowed. Each edge has a positive integer assigned to it, and a counter which is initially zero.
Consider the following process on this graph: we choose the starting vertex arbitrarily, then we go along some edge starting in and ending in , then we go along some edge from to 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 in any direction, its counter changes to . We can stop the process after any number of steps (possibly, zero).
Let us call the array after this process a configuration array. Consider all possible processes corresponding to all possible finite paths in . 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 .
Input
The first line of input contains two space-separated integers , (, ), the number of vertices and edges of the graph.
The next lines describe the edges, one per line: the -th edge is given by three integers , , (, ), 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 .