Under construction

Still being built. Things may change

Tri-color Spanning Tree

Time limit2sMemory limit1024 MB

Summary
Count spanning trees of a three-colored graph with at most g green and at most b blue edges, modulo 1e9+7, for n up to 40.
Level

Hard8 of 10

Topics
Graph, Matrix, Combinatorics
Solved
No attempts yet

Problem

bobo has an undirected graph GG whose edges are colored red, green, or blue. He wants to count the spanning trees of GG that have at most gg green edges and at most bb blue edges. Report the count modulo (109+7)(10^9 + 7).

Input

The first line contains four integers nn, mm, gg, and bb. nn and mm are the number of vertices and edges of GG (1≤n≤401 \leq n \leq 40, 0≤m≤1050 \leq m \leq 10^5, 0≤g,b<n0 \leq g, b < n).

The vertices are numbered 1,2,…,n1, 2, \dots, n.

Each of the next mm lines contains three integers aia_i, bib_i, and cic_i, meaning that there is an edge between vertices aia_i and bib_i (1≤ai,bi≤n1 \leq a_i, b_i \leq n, ai≠bia_i \neq b_i, 1≤ci≤31 \leq c_i \leq 3). The value ci=1,2,3c_i = 1, 2, 3 means the ii-th edge is red, green, or blue, respectively.

Output

Print a single integer, the number of spanning trees.

Examples2

  1. Example 1

    Input
    2 3 0 0
    1 2 1
    1 2 2
    1 2 3
    
    Expected output
    1
    
  2. Example 2

    Input
    3 6 1 0
    1 2 1
    1 2 1
    2 3 1
    2 3 2
    3 1 2
    3 1 2
    
    Expected output
    10