// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd(){static uint seed=416;return seed+=0x71dad4bfu,seed^=seed>>5,seed+=0xc6f74d88u,seed^=seed<<17,seed+=0x25e6561u,seed^=seed>>13;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args){read(t);read(args...);}
template <typename _Tp,typename... Args>Finline _Tp min(const _Tp &a,const _Tp &b,const Args &...args){return a<b?min(a,args...):min(b,args...);}
template <typename _Tp,typename... Args>Finline _Tp max(const _Tp &a,const _Tp &b,const Args &...args){return a<b?max(b,args...):max(a,args...);}
Finline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
const int mod=1000000007;
template<typename _Tp1,typename _Tp2>Finline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}
template<typename _Tp1,typename _Tp2>Finline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}
ll pw[N];
struct edge{
	int v,nxt,w;
}c[N<<1];
int front[N],edge_cnt;
Finline void addedge(int u,int v,int w)
{
	c[++edge_cnt]=(edge){v,front[u],w};
	front[u]=edge_cnt;
}
struct node{
	int x,y,w;
	Finline bool operator < (const node &o)const
	{
		return w<o.w;
	}
}a[N],b[N];
int fa[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int anc[N][21],w[N][21],dep[N];
void dfs(int x,int fa)
{
	dep[x]=dep[fa]+1;
	anc[x][0]=fa;
	for(int i=1;i<=20;++i)
	{
		anc[x][i]=anc[anc[x][i-1]][i-1];
		w[x][i]=max(w[x][i-1],w[anc[x][i-1]][i-1]);
	}
	for(int i=front[x];i;i=c[i].nxt)
	{
		int v=c[i].v;
		if(v==fa) continue;
		w[v][0]=c[i].w;
		dfs(v,x);
	}
}
int calc(int x,int y)
{
	if(dep[x]<dep[y]) std::swap(x,y);
	int ans=0;
	for(int i=20;i>=0;--i) if(dep[anc[x][i]]>=dep[y]) chmax(ans,w[x][i]),x=anc[x][i];
	if(x==y) return ans;
	for(int i=20;i>=0;--i) if(anc[x][i]!=anc[y][i]) chmax(ans,w[x][i]),chmax(ans,w[y][i]),x=anc[x][i],y=anc[y][i];
	chmax(ans,w[x][0]),chmax(ans,w[y][0]);
	return ans;
}
int main()
{
	pw[0]=1;
	for(int i=1;i<N;++i) pw[i]=pw[i-1],add(pw[i],pw[i]);
	int n,m;read(n,m);
	ll S;read(S);
	for(int i=1;i<=m;++i) read(a[i].x,a[i].y,a[i].w);
	std::sort(a+1,a+m+1);
	std::iota(fa+1,fa+n+1,1);
	ll sum=0;
	int pos=0;
	for(int i=1;i<=m;++i)
	{
		int x=a[i].x,y=a[i].y,w=a[i].w;
		int a=find(x),b=find(y);
		if(a==b)
		{
			::b[++pos]=::a[i];
			continue;
		}
		sum+=w;
		fa[a]=b;
		addedge(x,y,w),addedge(y,x,w);
	}
	if(S<sum) return puts("0"),0;
	ll ans=0;
	if(S==sum) add(ans,(pw[n-1]-2)*pw[m-n+1]%mod);
	dfs(1,0);
	for(int i=1;i<=pos;++i) b[i].w-=calc(b[i].x,b[i].y);
	std::sort(b+1,b+pos+1);
	for(int i=1;i<=pos;++i) if(b[i].w+sum==S) add(ans,pw[pos-i+1]);
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 5508