#define STOPIT
#include <bits/stdc++.h>
#define loop(n) for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto&& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto&& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; std::swap(a, m);
    u -= t * v; std::swap(u, v);
  }
  assert(m == 1);
  return u;
}
template <typename T>
class modular {
  private:
    int value;
  public:
    constexpr modular() = default;
    constexpr modular(const modular&) = default;
    constexpr modular(modular&&) = default;
    modular& operator=(const modular&) = default;
    modular& operator=(modular&&) = default;

    template <typename U>
    modular (const U& x) {value = normalize(x);}

    template <typename U>
    static auto normalize(const U& x) {
      int v = static_cast<int>(-mod() <= x && x < mod() ? x : x % mod());
      if (v < 0) v += mod();
      return v;
    }

    auto const& operator()() const { return value; }
    template <typename U>
    explicit operator U() const { return static_cast<U>(value); }
    constexpr static auto mod() { return T::value; }

    auto& operator+=(const modular& other) {
      if ((value += other.value) >= mod()) value -= mod();
      return *this;
    }
    auto& operator-=(const modular& other) {
      if ((value -= other.value) < 0) value += mod();
      return *this;
    }
    template <typename U>
    auto& operator+=(const U& other) {return *this += modular(other); }
    template <typename U>
    auto& operator-=(const U& other) {return *this -= modular(other); }
    auto operator-() const { return modular(-value); }
    auto& operator++() {return *this += 1;}
    auto& operator--() {return *this -= 1;}
    auto  operator++(int) {modular result(*this); operator++(); return result;}
    auto  operator--(int) {modular result(*this); operator--(); return result;}

    template <typename U = T>
    auto& operator*=(const modular& rhs) {
      value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
      return *this;
    }
    auto& operator/=(const modular& other) {
      return *this *= modular(inverse(other.value, mod()));
    }
};
template <typename T> struct is_modular : std::false_type {};
template <typename T> struct is_modular <modular<T>> : std::true_type{};
template <typename T> constexpr bool is_modular_v = is_modular<T>::value;

template <typename T> bool operator==(const modular<T>& lhs, const modular<T>& rhs) { return lhs() == rhs(); }
template <typename T, typename U> bool operator==(const modular<T>& lhs, U rhs) { return lhs == modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const modular<T>& rhs) { return modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const modular<T>& lhs, const modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> modular<T> operator+(const modular<T>& lhs, const modular<T>& rhs) { return modular<T>(lhs) += rhs; }
template <typename T, typename U> modular<T> operator+(const modular<T>& lhs, U rhs) { return modular<T>(lhs) += rhs; }
template <typename T, typename U> modular<T> operator+(U lhs, const modular<T>& rhs) { return modular<T>(lhs) += rhs; }

template <typename T> modular<T> operator-(const modular<T>& lhs, const modular<T>& rhs) { return modular<T>(lhs) -= rhs; }
template <typename T, typename U> modular<T> operator-(const modular<T>& lhs, U rhs) { return modular<T>(lhs) -= rhs; }
template <typename T, typename U> modular<T> operator-(U lhs, const modular<T>& rhs) { return modular<T>(lhs) -= rhs; }

template <typename T> modular<T> operator*(const modular<T>& lhs, const modular<T>& rhs) { return modular<T>(lhs) *= rhs; }
template <typename T, typename U> modular<T> operator*(const modular<T>& lhs, U rhs) { return modular<T>(lhs) *= rhs; }
template <typename T, typename U> modular<T> operator*(U lhs, const modular<T>& rhs) { return modular<T>(lhs) *= rhs; }

template <typename T> modular<T> operator/(const modular<T>& lhs, const modular<T>& rhs) { return modular<T>(lhs) /= rhs; }
template <typename T, typename U> modular<T> operator/(const modular<T>& lhs, U rhs) { return modular<T>(lhs) /= rhs; }
template <typename T, typename U> modular<T> operator/(U lhs, const modular<T>& rhs) { return modular<T>(lhs) /= rhs; }

template<typename T, typename U>
modular<T> power (const modular<T>& a, U b) {
  assert(b >= 0);
  modular<T> x = a, ret = 1;
  for (; b > 0; b /= 2) {
    if (b % 2 == 1) ret *= x;
    x *= x;
  }
  return ret;
}

template <typename T>
std::string to_string(const modular<T>& a) {
  return std::to_string(a());
}
template <typename T>
auto operator<<(std::ostream& os, const T& a)
  -> std::enable_if_t<is_modular_v<T>, std::ostream&>{
    return os << a();
  }
template <typename T>
auto operator>>(std::istream& is, T& a)
  -> std::enable_if_t<is_modular_v<T>, std::istream&> {
  long long x; is >> x;
  a = T(x);
  return is;
}

// using mod_type = int;

// struct variable_mod { static mod_type value; };
// mod_type variable_mod::value;
// mod_type& mod = variable_mod::value;
// using mint = modular< variable_mod >;

constexpr int mod = 1'000'000'007;
using mint = modular<std::integral_constant<std::decay_t<decltype(mod)>, mod>>;

class union_find {
    int n;
    std::vector<int> prt;

  public:
    union_find (int n) : n(n), prt(n, -1){}

    bool is_root (int x)        const {return prt.at(x) < 0;}
    int  size    (int x)        const {return -prt.at(find(x));}
    bool same(int x, int y)     const {return find(x) == find(y);}
    int  find    (int x)        const {
      while (!is_root(x)) x = prt.at(x);
      return x;
    }
    // Returns `true` if x and y are newly connected.
    // The smaller one x becomes a child of the larger one y.
    bool unite   (int x, int y) {
      if ((x = find(x)) == (y = find(y))) return false;
      if (size(x) > size(y)) std::swap(x, y);
      prt.at(y) += prt.at(x);
      prt.at(x) = y;
      return true;
    }
};

template < class Value >
class lowest_common_ancestor {
    struct edge {
      int to; Value cost;
      edge(int to, Value cost) : to(to), cost(cost){}
    };
    int n, lg, powlg;
    std::vector< std::vector< edge > > graph;
    std::vector< int >                 depth;
    std::vector< Value >               w_depth;
    std::vector< std::vector< int > >  table;
    std::vector< int > &               tail;

  public:
    using cost_type = Value;
    lowest_common_ancestor(int n) :
      n(n),
      lg(std::log2(n)),
      powlg(std::pow(2, lg)),
      graph(n),
      depth(n, 0),
      w_depth(n, 0),
      table(lg + 1, std::vector< int >(n)),
      tail(table.back())
      {}

    void insert (int u, int v, Value cost = 1) {
      graph.at(u).emplace_back(v, cost);
      graph.at(v).emplace_back(u, cost);
    }

    void build (int root = 0) {
      auto dfs = [&](auto f, int crr, int p) -> void {
        tail.at(crr) = p;
        for (auto const& e : graph.at(crr)) {
          if (e.to == p) continue;
          depth.at(e.to) = depth.at(crr) + 1;
          w_depth.at(e.to) = w_depth.at(crr) + e.cost;
          f(f, e.to, crr);
        }
      };
      dfs(dfs, root, root);
      for (int p = lg; p >= 1; p--) {
        auto & crr = table.at(p);
        auto & nxt = table.at(p - 1);
        for (int i = 0; i < n; i++) {
          nxt.at(i) = crr.at(crr.at(i));
        }
      }
    }

    auto query(int u, int v) const -> int {
      if (depth.at(u) < depth.at(v)) std::swap(u, v);
      auto diff = depth.at(u) - depth.at(v);
      if (diff > 0) {
        int coeff = powlg;
        for (auto const & row : table) {
          if (coeff < diff) {
            u = row.at(u);
            diff -= coeff;
          }
          coeff /= 2;
        }
        u = tail.at(u);
        diff--;
      }
      assert(diff == 0);
      assert(depth.at(u) == depth.at(v));
      if (u == v) return u;
      for (auto const & row : table) {
        auto next_u = row.at(u);
        auto next_v = row.at(v);
        if (next_u != next_v) {
          u = next_u;
          v = next_v;
        }
      }
      assert(u != v);
      u = tail.at(u), v = tail.at(v), assert(u == v);
      return u;
    }

    auto row_distance (int u, int v) const -> int {
      return depth.at(u) + depth.at(v) - 2 * depth.at(query(u, v));
    }

    auto weighted_distance (int u, int v) const -> Value {
      return w_depth.at(u) + w_depth.at(v) - 2 * w_depth.at(query(u, v));
    }
};

template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}

template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}

template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}

template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}

template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}

template < class Value >
struct vending_machine{
  Value i;
  vending_machine(Value i) : i(i){}
  auto issue() { return i++; }
  auto peek() const { return i; }
};
template < class Value >
auto make_vending_machine(Value i)
  { return vending_machine< Value >(i); }

/*
X が最小全域木の重みより小さいときは 0、
大きいとします。まず最小全域木は白いとして良いです。あとで 2 倍です。
最小全域木上で全点から DFS をして、各 2 点についてその間の最大の辺を前計算です。
すると交換で掛かるコストがわかるので、
  足りていなければ白、
  ぴったりならそれらのうち少なくとも一つは黒、
  大きすぎるならなんでもよいです。
なので、後ろ2つの数を a, b として、答えは 2 ^ b * (2 ^ a - 1) * 2 です。

ピッタリのときは、上記で c = a + n - 1 として、これらが全域木に使えるので、
答えは 2 ^ (m - c) * (2 ^ c - 2) です。

前計算パートは更に高速化できます。
最小全域木を構成するときに、頂点を n - 1 個増やしたグラフを構築します。
連結成分が頂点に対応していると思って、その対応を uf.find の結果を添字とする配列で管理です。
u, v が連結されたとき、新しい w を構成して、lca.insert(u,w), (v,w) です。
*/

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, m; std::cin >> n >> m;
  lint X; std::cin >> X;
  std::vector<std::tuple<lint,int,int>> edges;
  loop(m) {
    int u, v, w;
    std::cin >> u >> v >> w;
    u--,v--;
    edges.emplace_back(w,u,v);
  }
  std::sort(all(edges));
  debug(edges);

  std::vector<std::tuple<int,int,lint>> store;

  lint Y = 0;
  union_find uf(n);
  int N = n + n - 1;
  vending_machine<int> vm(n);
  std::vector<int> cmp(n);
  std::vector<lint> wt(N,-1);
  std::iota(all(cmp), 0);
  lowest_common_ancestor<int> lca(N);
  for (auto&& e : edges) {
    lint w; int u,v; std::tie(w,u,v) = e;
    if (!uf.same(u,v)) {
      Y += w;
      int cu = cmp.at(uf.find(u));
      int cv = cmp.at(uf.find(v));
      int cw = vm.issue();
      debug(cu,cv,cw);
      lca.insert(cu,cw);
      lca.insert(cv,cw);
      wt.at(cw) = w;
      uf.unite(u,v);
      cmp.at(uf.find(u)) = cw;
    } else {
      store.emplace_back(u,v,w);
    }
  }
  lca.build(N-1);
  debug(wt);

  int a = 0, b = 0;
  for (auto&& e : store) {
    int u,v; lint w; std::tie(u,v,w) = e;
    int c = lca.query(u,v);
    debug(u,v,c);
    lint d = wt.at(c);
    lint Z = Y + w - d;
    if (X == Z) a++;
    if (X < Z) b++;
  }
  debug(a,b);

  auto ans = [&] () -> mint {
    mint two = 2;
    if (X < Y) return 0;
    if (X == Y) {
      int c = a + n - 1;
      return power(two, m - c) * (power(two, c) - 2);
    }
    return power(two, b) * (power(two, a) - 1) * 2;
  }();
  std::cout << ans << std::endl;
  return 0;
}


Measured memory_bytes: 640