Problem:
Score : 900 points

Problem Statement

We have an undirected weighted graph with N vertices and M edges.
The i-th edge in the graph connects Vertex U_i and Vertex V_i, and has a weight of W_i.
Additionally, you are given an integer X.

Find the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo 10^9 + 7:

The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of X.

Here, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.

Constraints

1 \leq N \leq 1 000

1 \leq M \leq 2 000

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq W_i \leq 10^9 (1 \leq i \leq M)

If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j).

U_i \neq V_i (1 \leq i \leq M)

The given graph is connected.

1 \leq X \leq 10^{12}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
X
U_1 V_1 W_1
U_2 V_2 W_2
:
U_M V_M W_M

Output

Print the answer.

Sample Input 1

3 3
2
1 2 1
2 3 1
3 1 1

Sample Output 1

6

Sample Input 2

3 3
3
1 2 1
2 3 1
3 1 2

Sample Output 2

2

Sample Input 3

5 4
1
1 2 3
1 3 3
2 4 6
2 5 8

Sample Output 3

0

Sample Input 4

8 10
49
4 6 10
8 4 11
5 8 9
1 8 10
3 8 128773450
7 8 10
4 2 4
3 4 1
3 1 13
5 2 2

Sample Output 4

4

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 1100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
        cnt_[i] = 1;
    }
}

// 木の根を求める
int find(int x){
    if(par[x] == x){
        return x;
    }else{
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
        cnt_[y] += cnt_[x];
    }else{
        par[y] = x;
        cnt_[x] += cnt_[y];
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}


int main() {
    int n, m;
    cin >> n >> m;
    ll X;
    cin >> X;
    vector<pair<ll,P>> v(m);
    rep(i,m){
        cin >> v[i].second.first >> v[i].second.second >> v[i].first;
    }
    sort(v.begin(), v.end());
    ll ng = 0, ok = 0, free = 0;
    rep(i,m){
        UFinit();
        int x, y;
        x = v[i].second.first;
        y = v[i].second.second;
        ll w = v[i].first;
        ll sum = w;
        unite(x,y);
        rep(j,m){
            x = v[j].second.first;
            y = v[j].second.second;
            w = v[j].first;
            if(find(x)!=find(y)){
                unite(x,y);
                sum += w;
            }
        }
        // cout << i << ' ' << sum << endl;
        if(sum < X){
            ng++;
        }else if(sum == X){
            ok++;
        }else{
            free++;
        }
    }
    ll MOD = 1000000007;
    ll ans = 1;
    if(ok == 0){
        cout << 0 << endl;
        return 0;
    }

    if(ok == m){
        rep(i,m){
            ans *= 2;
            ans %= MOD;
        }
        ans += MOD - 2;
        ans %= MOD;
        cout << ans << endl;
        return 0;
    }

    rep(i,ok){
        ans *= 2;
        ans %= MOD;
    }
    ans--;
    rep(i,free){
        ans *= 2;
        ans %= MOD;
    }
    ans *= 2;
    ans %= MOD;
    cout << ans << endl;
    return 0;
}