#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(v) (v).begin(),(v).end()

#define PII pair<int,int>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } 
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } 
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}

#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}


using namespace std;

const int max_n = 1e3 + 5;

int n;

VI g[max_n];
int cost[1005][1005];
VI h[max_n];
lint X;
vector<pair<int, PII> > edges;
int p[max_n];

int fufind(int v) {
	return (v == p[v]) ? v : (p[v] = fufind(p[v]));
}

set<PII> H;

int lev[max_n];
int par[max_n];

int mod = 1e9 + 7;
int p2[5005];

int dfs(int v, int l) {
	FORE(i, h[v]) {
		int w = *i;
		if (w != par[v]) {
			par[w] = v;
			lev[w] = l+1;
			dfs(w, l+1);
		}
	}
}

int maxcost(int v, int w) {
	int res = 0;
	while (lev[v] > lev[w]) {
		res = max(res, cost[v][par[v]]);
		v = par[v];
	}
	while (lev[v] < lev[w]) {
		res = max(res, cost[w][par[w]]);
		w = par[w];
	}
	while (v != w) {
		res = max(res, cost[v][par[v]]);
		res = max(res, cost[w][par[w]]);
		v = par[v];
		w = par[w];
	}
	return res;
}

int main() {
		p2[0] = 1;
		FOR(i,1,5000) p2[i] = (p2[i-1] + p2[i-1]) % mod;
		read(n);
		make(m);
		scanf("%lld\n", &X);
		FOR(i,0,m) {
			make3(a, b, c);
			a--; b--;
			g[a].pb(b);
			g[b].pb(a);
			cost[a][b] = c;
			cost[b][a] = c;
			edges.pb(mp(c, mp(a,b)));
		}
		sort(all(edges));
		FOR(i,0,n) {
			p[i] = i;
		}
		int ile = n;
		lint mst = 0;
		FORE(j, edges) {
			int x = j->nd.nd;
			int y = j->nd.st;
			int px = fufind(x);
			int py = fufind(y);
			if (px == py) continue;
			p[px] = py;
			mst += cost[x][y];
			h[x].pb(y);
			h[y].pb(x);
			ile--;
			H.insert(mp(x,y)); H.insert(mp(y,x));
		}
		if (ile != 1 || mst > X) {
			printf("0\n");
			return 0;
		}
		lev[0] = 0; par[0] = 0;
		dfs(0,0);
//		debug2(mst, X);
		if (mst < X) {
			int dobre = 0;
			int zamalo = 0;
			int zaduzo = 0;
			FORE(j, edges) {
				int x = j->nd.nd;
				int y = j->nd.st;
				if (H.find(mp(x,y)) != H.end()) continue;
				lint nadmiar = cost[x][y] - maxcost(x, y);
				if (nadmiar > X - mst) zaduzo++;
				if (nadmiar == X - mst) dobre++;
				if (nadmiar < X - mst) zamalo++;
				// (2 ^(dobre) - 1)* 2^zaduzo * 2
			}
			lint ans = (p2[dobre] + mod - 1)%mod;
			ans = (ans * 1LL * p2[zaduzo + 1]) % mod;
			printf("%lld\n", ans);
			return 0;
		} 
		else if (mst == X) {
			int dobre = 0;
			int zaduzo = 0;
			FORE(j, edges) {
				int x = j->nd.nd;
				int y = j->nd.st;
				if (H.find(mp(x,y)) != H.end()) continue;
				lint nadmiar = cost[x][y] - maxcost(x, y);
				if (nadmiar > X - mst) zaduzo++;
				if (nadmiar == X - mst) dobre++;
			}
				dobre += n-1;
				// ans = 2^M - 2^(M-dobre)*2
				lint ans = p2[edges.size()];
				ans = ans + (mod - p2[edges.size() - dobre + 1]);
				ans %= mod;
				printf("%lld\n", ans);

			
		}
}


Measured memory_bytes: 4480