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: Runtime Error)

#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll /*long long*/ int 
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

// #define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (2006)

ll n, m, x, p[MAXN];
vector<pi> v[MAXN];
vector<spi> e;
struct _ufds {
	int p[MAXN];
	_ufds () { FOR(i,0,MAXN-1) p[i] = i; }
	void merge(ll x,ll y) {
		p[find(x)]=find(y);
	}
	ll find(ll x) { return (p[x]==x)?x:p[x]=find(p[x]); }
	bool same(ll a,ll b) { return find(a) == find(b); }
} ufds;
int depth[MAXN], up[MAXN];
void dfs(ll x,ll par) {
	p[x]=par;
	for(auto i:v[x]) if(i.f^par) {
		depth[i.f]=depth[x]+1, up[i.f]=i.s, dfs(i.f, x);
	} 
}
ll mod = 1e9+7;
ll qexp(ll x,ll e) {
	if(e==0) return 1;
	ll half=qexp(x,e/2);
	half *= half, half %= mod;
	if(e&1) half*=x, half%=mod;
	return half;
}
int main()
{
	FAST
	cin>>n>>m>>x;
	FOR(i,0,m-1) {
		ll a, b, c; cin>>a>>b>>c;
		e.eb(c, pi(a, b));
	}
	sort(all(e));
	vector<bool> in(e.size(), 0);
	vector<ll> diff(e.size(), 0);
	ll mst = 0;
	for(int i=0;i<(int)e.size();++i) {
		ll a=e[i].s.f, b=e[i].s.s;
		if(ufds.same(a,b)) continue;
		ufds.merge(a, b);
		in[i]=1;
		v[a].eb(b, e[i].f);
		v[b].eb(a, e[i].f);
		mst += e[i].f;
	}
	dfs(1, 1);
	function<ll(ll, ll)> go_up=[&](ll x,ll y){
		ll ans = 0;
		while(x^y) {
			if(depth[x]<depth[y]) {
				swap(x,y);
			}
			ans = max(ans, up[x]);
			x=p[x];
		}
		return ans;
	};
	ll D = x-mst, equal=0, upper=0;
	FOR(i,0,siz(e)-1) if(!in[i]) {
		diff[i] = e[i].f-go_up(e[i].s.f, e[i].s.s);
      	if(diff[i] < 0) {
        	cout<<39752356<<'\n'; return 0; 
        }
		if(D == diff[i]) {
			++ equal;
		} else if(D < diff[i]) {
			++ upper;
		}
	}
	if(D < 0) {
		cout<<0<<'\n'; return 0;
	} else if(D == 0) {
		cout << (qexp(2, n-1 + equal) - 2 + mod) % mod * qexp(2, upper) % mod<< '\n';
	} else {
		cout << (qexp(2, equal) - 1 + mod) % mod * qexp(2, upper) % mod * 2 % mod<< '\n';
	}
}
/*
lower, equal, upper
D = X - S
if(D < 0) cout<<0;
else if(D==0) {
	(qexp(2, n-1 + equal) - 1) * qexp(2, upper)  
} else  if(D > 0) {
	(qexp(2, equal) - 1) * qexp(2, upper) * 2
}
*/