#include <iostream>
#include <fstream>
#include <sstream>

#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <deque>
#include <string>

#include <algorithm>
#include <numeric>

#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>

#define pb push_back
#define pbk pop_back
#define mp make_pair
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define foreach(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
#define len(a) ((int) (a).size())

#ifdef CUTEBMAING
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

using namespace std;

typedef long long int64;
typedef long double ld;
typedef unsigned long long lint;

const int N = 1e6 + 100;
const int M = 1e9 + 7;
const int K = 20;

const int inf = (1 << 30) - 1;
const int64 linf = (1ll << 62) - 1;

inline int power(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) {
			res = (1ll * res * a) % M;
		}
		a = (1ll * a * a) % M;
		b >>= 1;
	}
	return res;
}

int n, m;
vector<tuple<int, int, int>> e;
bool bad[N];
int a[N], b[N], c[N];

int parent[N];

inline int findSet(int v) {
	if (parent[v] != v) {
		parent[v] = findSet(parent[v]);
	}
	return parent[v];
}

vector<int> g[N];

int bin[N][K];
int mn[N][K];
int in[N], out[N], curTime = 0;

void dfs(int v, int p = -1, int mmn = -inf) {
	in[v] = curTime++;
	bin[v][0] = p;
	mn[v][0] = mmn;
	for (int i = 1; i < K; i++) {
		if (bin[v][i - 1] == -1) {
			bin[v][i] = -1;
		} else {
			bin[v][i] = bin[bin[v][i - 1]][i - 1];
			mn[v][i] = max(mn[v][i - 1], mn[bin[v][i - 1]][i - 1]);
		}
	}
	for (int e : g[v]) {
		int to = a[e] ^ b[e] ^ v;
		if (p != to) {
			dfs(to, v, c[e]);
		}
	}
	out[v] = curTime++;
}

inline int anc(int u, int v) {
	return in[u] <= in[v] && out[v] <= out[u];
}

inline int lca(int u, int v) {
	if (anc(u, v)) {
		return u;
	}
	if (anc(v, u)) {
		return v;
	}
	for (int i = K - 1; i >= 0; i--) {
		if (bin[u][i] != -1 && !anc(bin[u][i], v)) {
			u = bin[u][i];
		}
	}
	assert(bin[u][0] != -1);
	return bin[u][0];
}

inline int getMaxLca(int v, int l) {
	int result = -inf;
	for (int i = K - 1; i >= 0; i--) {
		if (bin[v][i] != -1 && (bin[v][i] == l || !anc(bin[v][i], l))) {
			result = max(result, mn[v][i]);
			v = bin[v][i];
		}
	}
	return result;
}

inline int getMax(int u, int v) {
	int l = lca(u, v);
	return max(getMaxLca(u, l), getMaxLca(v, l));
}


int main() {
#ifdef XCODE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> m;
    if (n <= 2) {
    	cout << 0 << endl;
    	return 0;
    }
    int64 x; cin >> x;
    for (int i = 0; i < m; i++) {
    	int u, v, c; scanf("%d%d%d", &u, &v, &c), u--, v--;
    	e.emplace_back(c, u, v);
    }
    sort(all(e));
    for (int i = 0; i < n; i++) {
    	parent[i] = i;
    }
    int ans = 0;
    int64 total = 0;
    for (int i = 0; i < m; ) {
    	int it = i;
    	while (it < m && get<0>(e[i]) == get<0>(e[it])) {
    		it++;
    	}
    	int cnt = 0;
    	for (int j = i; j < it; j++) {
    		if (findSet(get<1>(e[j])) == findSet(get<2>(e[j]))) {
    			cnt++;
    		}
    	}
    	int cur = (power(2, it - i - cnt) - (i == 0 ? 2 : 1) + M) % M;
    	cur = (1ll * cur * power(2, cnt)) % M;
    	cur = (1ll * cur * (i > 0 ? 2 : 1)) % M;
    	ans = (ans + 1ll * cur * power(2, m - it)) % M;
    	for (int j = i; j < it; j++) {
    		int u = findSet(get<1>(e[j]));
    		int v = findSet(get<2>(e[j]));
    		if (u != v) {
    			parent[u] = v;
    			total += get<0>(e[j]);
    			g[get<1>(e[j])].pb(j);
    			g[get<2>(e[j])].pb(j);
    			bad[j] = true;
    			eprintf("%d %d %d\n", get<1>(e[j]) + 1, get<2>(e[j]) + 1, get<0>(e[j]));
    		}	
    	}
    	i = it;
    }
    for (int i = 0; i < m; i++) {
    	tie(c[i], a[i], b[i]) = e[i];
    }
	dfs(0);
	int cnt = 0, cnt2 = 0;
	ans = 0;
	if (total == x) {
		ans = (power(2, n - 1) - 2 + M) % M;
		ans = (1ll * ans * power(2, m - n + 1)) % M;
	}
	for (int i = 0; i < m; i++) {
		if (bad[i]) {
			continue;
		}
		if (total + c[i] - getMax(a[i], b[i]) == x) {
			cnt++;
		}
		if (total + c[i] - getMax(a[i], b[i]) > x) {
			cnt2++;
		}
	}
	int cur = (2ll * (power(2, cnt) - 1 + M)) % M;
	cur = (1ll * cur * power(2, cnt2)) % M;
	ans = (ans + cur) % M;
	cout << ans << endl;
    return 0;
}


Measured memory_bytes: 39296