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: Wrong Answer)

// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 1e5 + 2;

int P[N], W[N], V[N], U[N], Q[N];

int GetPar(int v) { return P[v] == v ? v : P[v] = GetPar(P[v]); }
bool Merge(int v, int u) {
	v = GetPar(v), u = GetPar(u);
	if (v == u) return false;
	P[v] = u;
	return true;
}
int Power(int a, int b) {
	int res = 1;
	for (; b; b >>= 1, a = 1LL * a * a % MOD) if (b & 1) res = 1LL * res * a % MOD;
	return res;
}

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);	
	
	int n, m, x; cin >> n >> m >> x;
	for (int i = 0; i < m; i++) cin >> V[i] >> U[i] >> W[i], Q[i] = i;
	sort(Q, Q + m, [&](int i, int j) { return W[i] < W[j]; });
	int les = 0, eq = 0, gre = 0;
	for (int i = 0; i < m; i++) {
		int s = 0;
		for (int j = 0; j < n; j++) P[j + 1] = j + 1;
		if (Merge(V[Q[i]], U[Q[i]])) s += W[Q[i]];
		for (int j = 0; j < m; j++) if (Merge(V[Q[j]], U[Q[j]])) s += W[Q[j]];
		if (s < x) les++;
		if (s == x) eq++;
		if (s > x) gre++;
	}
	if (les) cout << 2LL * (Power(2, eq) - 1) * Power(2, gre) << endl;
	else if (eq) cout << 1LL * (Power(2, eq) - 2) * Power(2, gre) % MOD << endl;
	else cout << 0 << endl;

	return 0;
}