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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

//By Ralif Rakhmatullin
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<math.h>
#include<cassert>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<map>
#include<ctime>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<bitset>
#include<valarray>
#include<iterator>
#include<list>
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define S second
#define ld long double
#define F first
#define y1 LOL
#define ld long double
#define pb push_back
#define len length
#define sz size
#define beg begin
const ll INF = (ll)1e18 + 123;
const int inf=(int)2e9 + 123; 
const int mod=1e9+7;
using namespace std;
struct edge{
	int x, y;
	ll w;
	bool u;
	edge(){
		x = y = 0;
		w = INF;
		u = 0;
	}
	edge(int a, int b, ll c){
		x = a;
		y = b;
		w = c;
		u = 0;
	}
	bool operator < (edge a) const {
		return (w < a.w);
	}
	bool operator > (edge a) const {
		return (w > a.w);
	}
};
int n, m;
ll X, d[1011][1011], p[1011], sz[1011], hv[1011][1011];
vector<pair<int, ll> > g[1011];
priority_queue<pair<ll, int> > q;
void go(int st){
	for(int i = 1; i <= n; i ++){
		if(i != st)
			d[st][i] = INF;
	}
	q.push(mp(0, st));
	while(q.sz()){
		ll c = -q.top().F;
		int v = q.top().S;
		q.pop();
		if(c != d[st][v])
			continue;
		for(auto to : g[v]){
			if(d[st][to.F] > d[st][v] + to.S){
				d[st][to.F] = d[st][v] + to.S;
				hv[st][to.F] = max(hv[st][v], to.S);
				q.push(mp(-d[st][to.F], to.F));
			}
		}
	}
}
vector<edge> e;
int get(int x){
	if(p[x] == x)
		return x;
	return (p[x] = get(p[p[x]]));
}
void unite(int a, int b){
	a = get(a);
	b = get(b);
	if(a == b)
		return;
	if(sz[a] < sz[b])
		swap(a, b);
	p[b] = a;
	sz[a] += sz[b];
}
ll f[2011], up, eq, lw;
ll pw[2011];
int main(){
	unsigned int FOR;
 	asm("rdtsc" : "=A"(FOR));
  	srand(FOR);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//cout.tie(0);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	cin >> n >> m >> X;
	pw[0] = 1;
	for(int i = 1; i <= m; i ++){
		int x, y;
		ll w;
		cin >> x >> y >> w;
		e.pb(edge(x, y, w));
	}
	for(int i = 1; i <= n; i ++){
		pw[i] = (2ll * pw[i - 1]) % mod;
		p[i] = i;
		sz[i] = 1;
	}
	sort(e.beg(), e.end());
	ll S = 0;
	for(int i = 0; i < m; i ++){
		if(get(e[i].x) != get(e[i].y)){
			S += e[i].w;
			e[i].u = 1;
			unite(e[i].x, e[i].y);
			g[e[i].x].pb(mp(e[i].y, e[i].w));
			g[e[i].y].pb(mp(e[i].x, e[i].w));
		} 
	}    
	for(int i = 1; i <= n; i ++){
		go(i);
	}
	S = X - S;
	if(S < 0){
		cout << 0;
		return 0;
	}
	for(int i = 0; i < m; i ++){
		if(e[i].u == 1)
			continue;
		f[i] = e[i].w - hv[e[i].x][e[i].y];
		if(f[i] < S)
			lw ++;
		else
		if(f[i] > S)
			up ++;
		else
			eq ++;
	}
//	cout << d[3][1] << " " << hv[3][1] << endl;
//	cout << lw << " " << eq << " " << up << endl;
	if(S == 0){
		cout << ((1ll * (pw[n - 1] - 2) * pw[m - n + 1]) % mod + (2ll * (pw[eq] - 1) * pw[up]) % mod) % mod;
	}else{
		cout << (2ll * (pw[eq] - 1) * pw[up]) % mod;
	}
	return 0;
}