#P3037. [USACO11DEC] Simplifying the Farm G

[USACO11DEC] Simplifying the Farm G

Description

Farmer John has been taking an evening algorithms course at his local university, and he has just learned about minimum spanning trees. However, Farmer John now realizes that the design of his farm is not as efficient as it could be, and he wants to simplify the layout of his farm.

The farm is currently arranged like a graph, with vertices representing fields and edges representing pathways between these fields, each having an associated length. Farmer John notes that for each distinct length, at most three pathways on his farm share this length. FJ would like to remove some of the pathways on his farm so that it becomes a tree -- that is, so that there is one unique route between any pair of fields. Moreover, Farmer John would like this to be a minimum spanning tree -- a tree having the smallest possible sum of edge lengths.

Help Farmer John compute not only the sum of edge lengths in a minimum spanning tree derived from his farm graph, but also the number of different possible minimum spanning trees he can create.

Input Format

Line 11: Two integers NN and M(1N4×104;1M105)M (1 \le N \le 4\times 10^4; 1 \le M \le 10^5), representing the number of vertices and edges in the farm graph, respectively. Vertices are numbered as 1N1 \ldots N.

Lines 2M+12 \ldots M+1: Three integers aia_i, bib_i and n_in\_i (1ai,biN;1ni106)(1 \le a_i, b_i \le N; 1 \le n_i \le 10^6) representing an edge from vertex aia_i to bib_i with length nin_i. No edge length nin_i will occur more than three times.

Output Format

Line 11: Two integers representing the length of the minimal spanning tree and the number of minimal spanning trees (mod 109+710^9+7).

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

4 3 

Hint

Picking both edges with length 11 and any edge with length 22 yields a minimum spanning tree of length 44.