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: Accepted)

#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
typedef pair<int, P> E;
const LL MOD = 1000000007LL;
struct UnionFind {
	int *par;
	int *rank;
	int size;
	UnionFind(int n) :size(n) {
		par = new int[size];
		rank = new int[size];
		for (int i = 0; i < size; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	}
	int find(int x) {
		if (par[x] == x) return x;
		return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;
		if (rank[x] < rank[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (rank[x] == rank[y]) rank[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	void clear() {
		for (int i = 0; i < size; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	}
};
LL mod_pow(LL a, LL n) {
	if (n == 0) return 1;
	if (n % 2 == 0) {
		LL x = mod_pow(a, n / 2);
		return (x*x) % MOD;
	}
	return (a*mod_pow(a, n - 1)) % MOD;
}
LL MST[2000];
int N, M;
LL F(LL X) {
	int c = 0;
	for (int i = 0; i < M; i++) {
		if (MST[i] <= X) c++;
	}
	if (c == 0) return mod_pow(2, M);
	return mod_pow(2, M - c + 1);
}
int main() {
	cin >> N >> M;
	LL X;
	cin >> X;
	vector<E> edge;
	for (int i = 0; i < M; i++) {
		int U, V, W;
		cin >> U >> V >> W;
		U--; V--;
		edge.push_back(E(W, P(U, V)));
	}
	sort(edge.begin(), edge.end());
	UnionFind U(N);
	for (int i = 0; i < edge.size(); i++) {
		LL sum = edge[i].first;
		U.unite(edge[i].second.first, edge[i].second.second);
		for (int j = 0; j < edge.size(); j++) {
			if (i == j) continue;
			if (!U.same(edge[j].second.first, edge[j].second.second)) {
				U.unite(edge[j].second.first, edge[j].second.second);
				sum += edge[j].first;
			}
		}
		MST[i] = sum;
		U.clear();
	}
	LL ans = F(X - 1);
	ans = (ans - F(X) + MOD) % MOD;
	cout << ans << endl;
}