//by kcz 1552920354@qq.com
//if you find any bug in my code
//please tell me
#include<bits/stdc++.h>
using namespace std;

template <typename T> void chmin(T &x,const T &y)
{
	if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
	if(x<y)x=y;
}
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define gc (c=getchar())
char readc()
{
	char c;
	while(gc<'-');
	return c;
}
int read()
{
    char c;
    while(gc<'-');
    if(c=='-')
    {
        int x=gc-'0';
        while(gc>='0')x=x*10+c-'0';
        return -x;
    }
    int x=c-'0';
    while(gc>='0')x=x*10+c-'0';
    return x;
}
#undef gc

const int N=1000+5,M=2000+5,D=1e9+7;
ll mi(ll x,int y)
{
	ll ans=1;
	while(y)
	{
		if(y&1)ans=ans*x%D;
		x=x*x%D;y>>=1;
	}
	return ans;
}
struct edge
{
	int x,y,w;
	bool in;
};
bool operator <(const edge &a,const edge &b)
{
	return a.w<b.w;
}
edge l[M];
int fa[N],sz[N];
int tfa[N],tw[N];
int find(int x)
{
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
int qiu(int x,int y)
{
	int ans=0;
	while(x!=y)
	if(sz[y]>=sz[x])
	{
		chmax(ans,tw[x]);x=tfa[x];
	}
	else
	{
		chmax(ans,tw[y]);y=tfa[y];
	}
	return ans;
}

int main()
{
	//freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
	int n=read(),m=read();
	ll X;
	cin>>X;
	rep(i,1,m)l[i]=(edge){read(),read(),read()};
	sort(l+1,l+m+1);
	rep(i,1,n){fa[i]=i;sz[i]=1;}
	ll sum=0;
	rep(i,1,m)
	{
		int x=find(l[i].x),y=find(l[i].y);
		if(x==y)continue;
		if(sz[x]>sz[y])swap(x,y);
		fa[x]=y;sz[y]+=sz[x];
		tfa[x]=y;tw[x]=l[i].w;
		l[i].in=1;
		sum+=l[i].w;
	}
	ll ans=0;
	if(sum==X)ans=(mi(2,n-1)-2)*mi(2,m-(n-1))%D;
	int cnt1=0,cnt0=0;
	rep(i,1,m)
	if(!l[i].in)
	{
		int w0=qiu(l[i].x,l[i].y);
		ll ns=sum-w0+l[i].w;
		if(ns>X)++cnt1; else
		if(ns==X)++cnt0; 
	}
	(ans+=2*(mi(2,cnt0)-1)%D*mi(2,cnt1))%=D;
	cout<<ans;
}

