#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<unsigned P> struct ModInt {
  using M = ModInt;
  unsigned v;
  constexpr ModInt() : v(0) {}
  constexpr ModInt(unsigned _v, int) : v(_v) {}
  template<class Z> ModInt(const Z& a) : v((a < 0 ? P - -a % P : a) % P) {}
  static constexpr unsigned p() { return P; }
  M operator+() const { return *this; }
  M operator-() const { return {v ? P - v : 0, 0}; }
  explicit operator bool() const noexcept { return v; }
  bool operator!() const noexcept { return !(bool)*this; }
  M& operator*=(M r) { v = (uint64_t)v * r.v % P; return *this; }
  M& operator/=(M r) { return *this *= r.inv(); }
  M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; }
  M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; }
  friend M operator*(M l, M r) { return M(l) *= r; }
  friend M operator/(M l, M r) { return M(l) /= r; }
  friend M operator+(M l, M r) { return M(l) += r; }
  friend M operator-(M l, M r) { return M(l) -= r; }
  friend ostream& operator<<(ostream& os, M r) { return os << r.v; }
  friend bool operator==(M l, M r) { return l.v == r.v; }
  friend bool operator!=(M l, M r) { return !(l == r); }
  M inv() const {
    int a = v, b = P, x = 1, u = 0;
    while (b) {
      int q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * u, u);
    }
    assert(a == 1);
    return x;
  }
  template<class Z> M pow(Z n) const {
    if (n < 0) return pow(-n).inv();
    M res = 1;
    for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;
    return res;
  }
};
using Mint = ModInt<(unsigned)1e9 + 7>;

struct UnionFind {
  const int n;
  V<> t; // root ? -sz : par
  UnionFind(int _n) : n(_n), t(n, -1) {}
  int find(int v) { return t[v] < 0 ? v : t[v] = find(t[v]); }
  void unite(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return;
    if (-t[u] < -t[v]) swap(u, v);
    t[u] += t[v];
    t[v] = u;
  }
  bool same(int u, int v) { return find(u) == find(v); }
  int size(int v) { return -t[find(v)]; }
};

namespace LCA {
  using T = int;
  struct Edge { int to; T w; };
  T op(const T& x, const T& y) { return max(x, y); }
  constexpr T e = 0;

  V<> dep;
  VV<> par;
  VV<T> val;
  void init(const VV<Edge>& g, int r) {
    int n = g.size();
    dep.resize(n);
    par.assign(__lg(2 * n - 1), V<>(n, -1));
    val.assign(__lg(2 * n - 1), V<T>(n, e));
    auto dfs = [&](auto& F, int v, int p) -> void {
      for (const auto& f : g[v]) if (f.to != p) {
        dep[f.to] = dep[v] + 1;
        par[0][f.to] = v;
        val[0][f.to] = f.w;
        F(F, f.to, v);
      }
    };
    dep[r] = 0;
    dfs(dfs, r, -1);
    for (int k = 1; k < (int) par.size(); ++k) {
      for (int v = 0; v < n; ++v) {
        if (par[k - 1][v] == -1) continue;
        par[k][v] = par[k - 1][par[k - 1][v]];
        val[k][v] = op(val[k - 1][v], val[k - 1][par[k - 1][v]]);
      }
    }
  }
  int get_par(int v, int h) {
    for (int k = 0; h > 0; h >>= 1, ++k) {
      if (v == -1) break;
      if (h & 1) v = par[k][v];
    }
    return v;
  }
  int lca(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    v = get_par(v, dep[v] - dep[u]);
    if (u == v) return u;
    for (int k = par.size() - 1; k >= 0; --k) {
      if (par[k][u] != par[k][v]) {
        u = par[k][u];
        v = par[k][v];
      }
    }
    return par[0][u];
  }
  T get_val(int v, int h) {
    T res = e;
    for (int k = 0; h > 0; h >>= 1, ++k) {
      if (v == -1) break;
      if (h & 1) {
        res = op(res, val[k][v]);
        v = par[k][v];
      }
    }
    return res;
  }
  T acc(int u, int v) {
    int a = lca(u, v);
    return op(get_val(v, dep[v] - dep[a]), get_val(u, dep[u] - dep[a]));
  }
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, m; cin >> n >> m;
  lint x; cin >> x;
  struct Edge {
    int u, v, w;
    bool operator<(const Edge& r) const { return w < r.w; }
  };
  V<Edge> es(m);
  for (int j = 0; j < m; ++j) {
    int u, v, w; cin >> u >> v >> w, --u, --v;
    es[j] = {u, v, w};
  }
  sort(begin(es), end(es));

  lint y = 0;
  VV<LCA::Edge> g(n);
  {
    UnionFind uf(n);
    for (const auto& e : es) if (not uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      y += e.w;
      g[e.u].emplace_back(LCA::Edge{e.v, e.w});
      g[e.v].emplace_back(LCA::Edge{e.u, e.w});
    }
    LCA::init(g, 0);
  }
  if (y > x) {
    return cout << 0 << '\n', 0;
  }
  V<bool> b(m);
  for (int j = 0; j < m; ++j) {
    UnionFind uf(n);
    uf.unite(es[j].u, es[j].v);
    lint z = es[j].w;
    for (int k = 0; k < m; ++k) if (k != j and not uf.same(es[k].u, es[k].v)) {
      uf.unite(es[k].u, es[k].v);
      z += es[k].w;
    }
    b[j] = z == y;
  }
  if (y == x) {
    int c = accumulate(begin(b), end(b), 0);
    Mint res = Mint(2).pow(m) - 2 * Mint(2).pow(m - c);
    return cout << res << '\n', 0;
  }

  V<> v;
  for (int j = 0; j < m; ++j) if (not b[j]) {
    int mx = LCA::acc(es[j].u, es[j].v);
    v.push_back(es[j].w - mx);
  }
  sort(begin(v), end(v));
  auto p = equal_range(begin(v), end(v), x - y);
  int c = p.second - p.first, d = end(v) - p.second;
  if (!c) {
    return cout << 0 << '\n', 0;
  }
  Mint res = 2 * (Mint(2).pow(c) - 1) * Mint(2).pow(d);
  cout << res << '\n';
}

Measured memory_bytes: 512