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)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void read(int &x){
	x=0; char c=getchar(); int p=1;
	for (;c<48;c=getchar())if (c=='-')p=-1;
	for (;c>47;c=getchar())x=(x<<1)+(x<<3)+(c^48);
	x*=p;
}
void read(ll &x){
	x=0; char c=getchar(); int p=1;
	for (;c<48;c=getchar())if (c=='-')p=-1;
	for (;c>47;c=getchar())x=(x<<1)+(x<<3)+(c^48);
	x*=p;
}
void Max(int &x,int y){
	if (x<y)x=y;
}
void Max(ll &x,ll y){
	if (x<y)x=y;
}
#define mo 1000000007
#define M 4005
int n,m,top[M],cnt[M],son[M],de[M],L[M],tot,fa[M],id[M],nx[M],ecnt=1;
ll val,sum,len[M];
bool mark[M];
struct edg{
	int x,y;
	ll l;
	void init(){
		read(x); read(y); read(l);
	}
}edge[M];
bool cmp(edg a,edg b){
	return a.l<b.l;
}
struct ed{
	int x;
	ll l;
	int nx;
}e[M];
int get(int x){
	if (fa[x]==x)return x;
	return fa[x]=get(fa[x]);
}
void add(int x,int y,ll l){
	e[ecnt]=(ed){y,l,nx[x]};
	nx[x]=ecnt++;
}
void ins(int x,int y,ll l){
	add(x,y,l);
	add(y,x,l);
}
struct Reimu{
	ll f[M<<2];
	#define ls (p<<1)
	#define rs (p<<1|1)
	void build(int l,int r,int p){
		if (l==r){
			f[p]=len[id[l]];
			return;
		}
		int mid=(l+r)>>1;
		build(l,mid,ls);
		build(mid+1,r,rs);
		f[p]=max(f[ls],f[rs]);
	}
	ll qu(int l,int r,int x,int y,int p){
		if (l==x&&r==y){
			return f[p];
		}
		int mid=(l+r)>>1;
		if (y<=mid)return qu(l,mid,x,y,ls);
		if (x>mid)return qu(mid+1,r,x,y,rs);
		return max(qu(l,mid,x,mid,ls),qu(mid+1,r,mid+1,y,rs));
	}
}T;
void dfs1(int f,int x){
	cnt[x]=1; de[x]=de[f]+1; fa[x]=f;
	for (int i=nx[x];i;i=e[i].nx)if (e[i].x!=f){
		len[e[i].x]=e[i].l;
		dfs1(x,e[i].x);
		cnt[x]+=cnt[e[i].x];
		if (cnt[e[i].x]>cnt[son[x]]){
			son[x]=e[i].x;
		}
	}
}
void dfs2(int f,int x){
	L[x]=++tot; top[x]=f; id[tot]=x;
	if (son[x]){
		dfs2(f,son[x]);
		for (int i=nx[x];i;i=e[i].nx)if (e[i].x!=fa[x]&&e[i].x!=son[x]){
			dfs2(e[i].x,e[i].x);
		}
	}
}
ll chk(edg e){
	int x=e.x,y=e.y;
	ll res=0;
	for (;top[x]!=top[y];){
		if (de[top[x]]>de[top[y]]){
			Max(res,T.qu(1,n,L[top[x]],L[x],1));
			x=fa[top[x]];
		}
		else {
			Max(res,T.qu(1,n,L[top[y]],L[y],1));
			y=fa[top[y]];
		}
	}
	if (de[x]<de[y]){
		if (L[x]<L[y])Max(res,T.qu(1,n,L[x]+1,L[y],1));
	}
	else {
		if (L[y]<L[x])Max(res,T.qu(1,n,L[y]+1,L[x],1));
	}
	return sum+e.l-res;
}
void solve(){
	int i,tot1=1,tot2=1;
	ll tmp;
	for (i=1;i<=m;i++){
		tmp=chk(edge[i]);
		if (tmp==val){
			tot2=tot2*2%mo;
		}
		else if (tmp>val){
			tot1=tot1*2%mo;
		}
	}
	tot2--;
	printf("%lld\n",2ll*tot1%mo*tot2%mo);
}
int main(){
//	freopen("mysterium.in","r",stdin);
//	freopen("mysterium.out","w",stdout);
	read(n); read(m); read(val);
	int i,x,y;
	for (i=1;i<=m;i++){
		edge[i].init();
	}
	for (i=1;i<=n;i++){
		fa[i]=i;
	}
	sort(edge+1,edge+m+1,cmp);
	for (i=1;i<=m;i++){
		x=get(edge[i].x);
		y=get(edge[i].y);
		if (x!=y){
			fa[x]=y;
			sum+=edge[i].l;
			mark[i]=1;
		}
	}
	if (sum>val){
		printf("0\n");
		return 0;
	}
	else {
		for (i=1;i<=m;i++){
			if (mark[i]){
				ins(edge[i].x,edge[i].y,edge[i].l);	
			}
		}
		dfs1(1,1);
		dfs2(1,1);
		T.build(1,n,1);
		solve();
	}
	return 0;
}