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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
// typedef __int128_t Int;
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
using pii = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct DisjointSet {
  // ２つのグループを１つにまとめる　と　2つの要素が同じグループに所属しているかどうかを判定する
  vector<int> rank, p, S; // p->parent  S[findSet(v)] ->連結成分の大きさ
  DisjointSet() {}
  DisjointSet(int size) {
    S.resize(size, 1);
    rank.resize(size, 0);
    p.resize(size, 0);
    rep(i, 0, size) makeSet(i);
  }
  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }
  bool same(int x, int y) { // 判定する
    return findSet(x) == findSet(y);
  }
  void unite(int x, int y) { // 連結するときにはこれを使う
    if (same(x, y))
      return;
    link(findSet(x), findSet(y));
  }
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }

    S[x] = S[y] = S[x] + S[y];
  }
  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]); // path compression
    }
    return p[x];
  }
  int connectedComponentSize(int x) { return S[findSet(x)]; }
};

struct Edge {
  int source, target, cost;
  Edge(int source = 0, int target = 0, int cost = 0)
      : source(source), target(target), cost(cost){};

  bool operator<(const Edge &e) const { return cost < e.cost; }
};
using Graph = vector<vector<Edge>>;
void dfs(int s, int t, int c, vector<vector<int>> &pathMax, Graph &g) {
  dump(s, t, c);
  pathMax[s][t] = c;
  for (auto e : g[t]) {
    if (pathMax[s][e.target] == -1)
      dfs(s, e.target, max(c, e.cost), pathMax, g);
  }
}
long long modpow(long long base, long long exponent, long long mod) {
  long long res = 1;
  while (exponent > 0) {
    if (exponent & 1)
      res = res * base % mod;
    base = base * base % mod;
    exponent >>= 1;
  }
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N, M;
  cin >> N >> M;
  int X;
  cin >> X;
  vector<Edge> edges;
  rep(i, 0, M) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    edges.push_back(Edge(u, v, w));
  }
  vector<vector<Edge>> mst(N);
  vector<Edge> remain;
  sort(all(edges));
  int S = 0;
  DisjointSet dset(N);
  rep(i, 0, edges.size()) {
    Edge e = edges[i];
    if (!dset.same(e.source, e.target)) {

      mst[e.source].push_back(e);
      swap(e.source, e.target);
      mst[e.source].push_back(e);

      S += e.cost;
      dset.unite(e.source, e.target);
    } else {
      remain.push_back(e);
    }
  }

  vector<vector<int>> pathMax(N, vector<int>(N, -1));
  rep(i, 0, N) { dfs(i, i, 0, pathMax, mst); }
  auto diff = [&](Edge e) { return e.cost - pathMax[e.source][e.target]; };
  int D = X - S;
  dump(D);
  if (D < 0) {
    cout << 0 << endl;
  } else {
    int lower = 0, equal = 0, upper = 0;
    for (auto e : remain) {
      dump(diff(e), e.source, e.target, e.cost, pathMax[e.source][e.target]);
      if (diff(e) < D)
        lower++;
      else if (diff(e) == D)
        equal++;
      else
        upper++;
    }
    dump(lower, equal, upper);

    int ans = (2 * (modpow(2, equal, MOD) - 1) * modpow(2, upper, MOD)) % MOD;
    if (D == 0) {
      (ans += (modpow(2, N - 1, MOD) - 2) * modpow(2, M - (N - 1), MOD)) %= MOD;
    }
    cout << ans << endl;
  }

  return 0;
}