#include "bits/stdc++.h"
#include <random>
using namespace std;
using ll    =long long;    using dd   =double;        using pll    =pair<ll, ll>;  using tll    =tuple<ll,ll,ll>;
using vll   =vector<ll>;   using vdd  =vector<dd>;    using vpll   =vector<pll>;   using vtll   =vector<tll>;
using vvll  =vector<vll>;  using vvdd =vector<vdd>;   using vvpll  =vector<vpll>;  using vvtll  =vector<vtll>;
using vvvll =vector<vvll>; using vvvdd=vector<vvdd>;  using vvvpll =vector<vvpll>; using vvvtll =vector<vvtll>;
using vvvvll=vector<vvvll>;using vvvvdd=vector<vvvdd>;using vvvvpll=vector<vvvpll>;using vvvvtll=vector<vvvtll>;

constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
constexpr dd PI  = 3.1415926535897932;

//cin,cout高速化のおまじない＋桁数指定
struct Fast{
	Fast(){
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
	}
} fast;

#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, S, E)  for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define dep(i, E, S)  for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define EACH(e, v) for (auto&& e : v)
#define each(e, v) for (auto&& e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> inline T MaxE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmax(m,v[i]); return m;}//v[S]～v[E]の最大値
template<class T> inline T MinE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmin(m,v[i]); return m;}//v[S]～v[E]の最小値
template<class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } //先頭N個中の最大値
template<class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); }
template<class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template<class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template<class T> inline ll MaxI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]>v[m])m=i;} return m;}
template<class T> inline ll MinI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]<v[m])m=i;} return m;}
template<class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); }
template<class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); }
template<class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); }
template<class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); }
template<class T> inline T Sum(vector<T> &v, ll S,ll E){ T s=v[S]; REPS(i,S+1,E)s+=v[i]; return s; }
template<class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template<class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template<class T> inline ll sz(T &v){ return (ll)v.size(); }
template<class T> inline T POW(T a, ll n){ T r=1; for (; n>0; n>>=1, a*=a){ if (n&1)r*=a; } return r; }
inline ll POW(int a, ll n){ return POW((ll)a, n); }
inline ll CEIL(ll a, ll b){ return (a+b-1)/b; }
ll Gcd(ll a, ll b){ return (b==0) ? a : Gcd(b, a%b); }
ll Lcm(ll a, ll b){ return a * b / Gcd(a, b); }
inline ll MSB(ll a){for(ll o=63,x=-1;;){ll m=(o+x)/2; if(a<(1LL<<m))o=m; else x=m; if(o-x==1)return x;}}
inline ll Upper2N(ll s) { return 1LL<<MSB(s)<<1; }//s<2^nなる最小の2^n
inline vll Bit2Idx(ll m){vll v; for(ll i=0;m;m>>=1,i++) if(m&1)v.push_back(i); return move(v);}
inline ll BitNum(ll m){ for (ll c=0;; m>>=1){ c+=m&1; if (!m)return c; } }
inline ll Bit(ll s, ll i){ return (s>>i)&1; } //sの第ibit i=0-63
[[nodiscard]] inline ll BitOn(ll s, ll i){ return s|(1LL<<i); } //sの第ibitON i=0-63
[[nodiscard]] inline ll BitOff(ll s, ll i){ return s&~(1LL<<i); } //sの第ibitOFF i=0-63
[[nodiscard]] inline ll BitCut(ll s, ll i){ return s & (1LL<<++i)-1; }//sの第0～第ibitを抽出
inline vll str2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'0'); return move(v);}
inline vll ALP2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'A'); return move(v);}
inline vll alp2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'a'); return move(v);}
template<class T> inline ll FloorIdx(vector<T> &v, T x){ return upper_bound(ALL(v), x)-v.begin()-1; }
template<class T> inline ll CeilIdx(vector<T> &v, T x){ return lower_bound(ALL(v), x)-v.begin(); }
template<class T> inline ll UnderNumOf(vector<T> &v, T x){ //sort済vのx以下の個数を得る
	return upper_bound(v.begin(), v.end(), x) - v.begin(); }
template<class T, class Pr> inline ll UnderNumOf(vector<T> &v, T x, Pr pred){ //x以下個数
	return upper_bound(v.begin(), v.end(), x, pred) - v.begin(); }
template<class T> inline ll OverNumOf(vector<T> &v, T x){ //sort済vのx以上の個数を得る
	return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin()); }
template<class T, class Pr> inline ll OverNumOf(vector<T> &v, T x, Pr pred){ //x以上の個数
	return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin()); }

template<class T=ll> inline vector<T> cinv(ll N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);}
template<class T, class S> inline vector<pair<T, S>> cinv2(ll N){
	vector<pair<T, S>> v(N); REP(i,N){cin>>v[i].first>>v[i].second;} return move(v); }
template<class T> inline vector<T> cinv2(ll N){
	vector<T> v(N); REP(i, N){ll a, b; cin>>a>>b; v[i]={a,b};} return move(v);}
template<class T,class S,class R> inline vector<tuple<T, S, R>> cinv3(ll N){
	vector<tuple<T,S,R>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i]);} return move(v);}
template<class T> inline vector<T> cinv3(ll N){
	vector<T> v(N); REP(i, N){ll a,b,c; cin>>a>>b>>c; v[i]={a,b,c};} return move(v);}
template<class T,class S,class R,class Q> inline vector<tuple<T,S,R,Q>> cinv4(ll N){
	vector<tuple<T,S,R,Q>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i])>>get<3>(v[i]);} return move(v);}
template<class T> inline vector<T> cinv4(ll N){
	vector<T> v(N); REP(i, N){ll a,b,c,d; cin>>a>>b>>c>>d; v[i]={a,b,c,d};} return move(v);}
template<class T=ll> inline vector<vector<T>> cinvv(ll N, ll M){
	vector<vector<T>> vv(N); REP(i, N) vv[i] = cinv(M); return move(vv);}
template<class T> inline void coutv(vector<T> &v, string d=" "){
	ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?"":d);} cout<<'\n';}
template<class T> inline void coutv(deque<T> &v, string d=" "){
	ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?"":d);} cout<<'\n';}
template<class T> void bye(T a){cout << a << '\n'; exit(0);}
#if defined(_DEBUG)
ll dumpW = 5;
template<class T> void Dump(vector<T> &v){
	REP(i, (ll)v.size()) { cerr << ((i==0)?"[":" ") << setw(dumpW) << v[i]; }  cerr << "]";
}
void Dump(vll &v){
	REP(i, (ll)v.size()) {
		cerr << ((i==0)?"[":" ") << setw(dumpW);
		if       (v[i] == INF) cerr << "INF";
		else if (v[i] == -INF) cerr << "-INF";
		else                   cerr << v[i];
	}  cerr << "]";
}
template<class T> void Dump(vector<vector<T>> &v){
	ll N=(ll)v.size();  string d[2][2]={{" ", "["},  {"\n", "]\n"}};
	REP(i, N) { cerr << d[0][i==0];  Dump(v[i]);  cerr << d[1][i==N-1]; }
}
template<class T> void Dump(vector<vector<vector<T>>> &v){
	ll N=(ll)v.size();  string d[2]={"  <", "[ <"};
	REP(i, N) { cerr << d[i==0] << i << ">\n"; Dump(v[i]); }  cerr << "]\n";
}
template<class T> void Dump(vector<vector<vector<vector<T>>>> &v){
	ll N=(ll)v.size();  string d[2]={"  ---<", "[ ---<"};
	REP(i, N) { cerr << d[i==0] << i << ">---\n"; Dump(v[i]); }  cerr << "]\n";
}
template<class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v){
	ll N=(ll)v.size();  string d[2]={"  ======<", "[ ======<"};
	REP(i, N) { cerr << d[i==0] << i << ">======\n"; Dump(v[i]); }  cerr << "]\n";
}
template<class T, class S> void Dump(map<T, S> &m){
	for(auto e: m) cout << e.first << " : " << e.second << '\n';
}
#else
template<class T> void Dump(vector<T> &v){}
template<class T> void Dump(vector<vector<T>> &v){}
template<class T> void Dump(vector<vector<vector<T>>> &v){}
template<class T> void Dump(vector<vector<vector<vector<T>>>> &v){}
template<class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v){}
#endif

template<ll MOD> struct mll_{
	ll val;
	mll_(ll v = 0): val(v % MOD){ if (val < 0) val += MOD; }
	mll_ operator - () const { return -val; }
	mll_ operator + (const mll_ &b) const { return val + b.val; }
	mll_ operator - (const mll_ &b) const { return val - b.val; }
	mll_ operator * (const mll_ &b) const { return val * b.val; }
	mll_ operator / (const mll_ &b) const { return mll_(*this) /= b; }
	mll_ operator + (ll b) const { return *this + mll_(b); }
	mll_ operator - (ll b) const { return *this - mll_(b); }
	mll_ operator * (ll b) const { return *this * mll_(b); }
	friend mll_ operator + (ll a, const mll_ &b) { return b + a; }
	friend mll_ operator - (ll a, const mll_ &b) { return -b + a; }
	friend mll_ operator * (ll a, const mll_ &b) { return b * a; }
	friend mll_ operator / (ll a, const mll_ &b) { return mll_(a)/b; }
	mll_ &operator += (const mll_ &b) { val=(val+b.val)%MOD; return *this; }
	mll_ &operator -= (const mll_ &b) { val=(val+MOD-b.val)%MOD; return *this; }
	mll_ &operator *= (const mll_ &b) { val=(val*b.val)%MOD; return *this; }
	mll_ &operator /= (const mll_ &b) {
		ll c=b.val, d=MOD, u=1, v=0;
		while (d){
			ll t = c / d;
			c -= t * d; swap(c, d);
			u -= t * v; swap(u, v);
		}
		val = val * u % MOD;
		if (val < 0) val += MOD;
		return *this;
	}
	mll_ &operator += (ll b) { return *this += mll_(b); }
	mll_ &operator -= (ll b) { return *this -= mll_(b); }
	mll_ &operator *= (ll b) { return *this *= mll_(b); }
	mll_ &operator /= (ll b) { return *this /= mll_(b); }
	bool operator == (const mll_ &b) { return val == b.val; }
	bool operator != (const mll_ &b) { return val != b.val; }
	bool operator == (ll b) { return *this == mll_(b); }
	bool operator != (ll b) { return *this != mll_(b); }
	friend bool operator == (ll a, const mll_ &b) { return mll_(a) == b.val; }
	friend bool operator != (ll a, const mll_ &b) { return mll_(a) != b.val; }
	friend ostream &operator << (ostream &os, const mll_ &a) { return os << a.val; }
	friend istream &operator >> (istream &is, mll_ &a) { return is >> a.val; }
	static mll_ Combination(ll a, ll b){
		chmin(b, a-b);
		if (b<0) return mll_(0);
		mll_ c = 1;
		REP(i, b) c *= a-i;
		REP(i, b) c /= i+1;
		return c;
	}
};
using mll = mll_<1000000007LL>; //1000000007LL;// 998244353LL;
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
using vvvvvmll = std::vector<vvvvmll>;

template<class T=ll> vector<vector<pair<ll, T>>> cinWGraph(
					ll nodeNum, ll edgeNum, bool isDirected){ //無向false、有向true
	vector<vector<pair<ll, T>>> to(nodeNum);
	REP(i, edgeNum){
		ll v, u;  T c;  cin >> v >> u >> c;
		v--; u--;
		to[v].emplace_back(u, c);
		if (!isDirected) to[u].emplace_back(v, c);
	}
	return move(to);
}

class UnionFindTree{
private:
	vll par, rank, size;
public:
	UnionFindTree(ll num): par(num), rank(num), size(num, 1){
		REP(i, num) par[i] = i;
	}
	bool Same(ll x, ll y) { return Root(x) == Root(y); }
	ll Root(ll x) { return (par[x] == x) ? x : par[x]=Root(par[x]); }
	ll Size(ll x){ return size[Root(x)]; }
	void Unite(ll x, ll y){
		x = Root(x);
		y = Root(y);
		if (x == y) return;
		if (rank[x] < rank[y]) par[x] = y;
		else                   par[y] = x;
		if (rank[x] == rank[y]) rank[x]++;
		size[x] = size[y] = size[x] + size[y];
	}
};
/*
UnionFindTree uft(N);
uft.Unite(x,y);
bool b = uft.Same(x,y);
ll num = uft.Size(x);
ll  v  = uft.Root(x);
*/

#if 0
struct DepthOfTree{
	vvpll &to; vll depth;
	DepthOfTree(vvpll &to, ll root): to(to), depth(to.size()) { dfs(root, -1, 0); }
	void dfs(ll v, ll p, ll d){
		EACH(e, to[v]){
			ll u; ll w;  tie(u, w) = e;
			if (u!=p) dfs(u, v, depth[u]=d+1);
		}
	}
	vll get(){ return move(depth); }
};

struct ParentOfTree{
	vvpll &to; vll par;
	ParentOfTree(vvpll &to, ll root, ll p=-1): to(to), par(to.size()) { dfs(root, p); }
	void dfs(ll v, ll p){
		par[v] = p;  
		EACH(e, to[v]){
			ll u; ll w;  tie(u, w) = e;
			if (u!=p) dfs(u, v);
		}
	}
	vll get(){ return move(par); }
};

struct Dubling{
	vvll next;
	ll N, B;
	Dubling(): N(0), B(0) {}
	Dubling(const vll &nxt, ll nextTimesMax){ Init(nxt, nextTimesMax); }
	void Init(const vll &nxt, ll nextTimesMax){
		N = (ll)nxt.size();
		B = MSB(nextTimesMax) + 1;
		next.resize(N, vll(B, INF));
		REP(v, N) next[v][0] = nxt[v];
		REPS(i, 1, B-1) REP(v, N) next[v][i] = next_(next_(v, i-1), i-1);
	}
	ll Times(ll v, ll times){ //!< times個先  times=0も可
		DEPS(i, 0, MSB(times))  if ((times>>i)&1) v = next_(v, i);
		return v;
	}
	ll TimesPow2(ll v, ll i) { return next[v][i]; }  //!< 2^i個先
	ll next_(ll v, ll i){ return (v==INF) ? INF : next[v][i]; }
};

struct LCA{
	vvpll &to; vll depth; Dubling pDbl;
	LCA(vvpll &to, ll root): to(to), depth(move(DepthOfTree(to, root).get())),
		pDbl(ParentOfTree(to, root, INF).get(), (ll)to.size()) {}
	ll operator() (ll v, ll u){
		ll dv = this->depth[v], du = this->depth[u];
		if (dv < du) { swap(v, u); swap(dv, du); }
		if ((v = this->pDbl.Times(v, dv-du)) == u) return v;
		DEPS(i, 0, MSB(du)){
			ll vv = this->pDbl.TimesPow2(v, i), uu = this->pDbl.TimesPow2(u, i);
			if (vv != uu) { v = vv; u = uu; }
		}
		return this->pDbl.TimesPow2(v, 0);
	}
};
#endif

struct Dubling{
	vvll next;
	ll N, B;
	Dubling(): N(0), B(0) {}
	Dubling(const vll &nxt, ll nextTimesMax){ Init(nxt, nextTimesMax); }
	void Init(const vll &nxt, ll nextTimesMax){
		N = (ll)nxt.size();
		B = MSB(nextTimesMax) + 1;
		next.resize(N, vll(B, INF));
		REP(v, N) next[v][0] = nxt[v];
		REPS(i, 1, B-1) REP(v, N) next[v][i] = next_(next_(v, i-1), i-1);
	}
	ll Times(ll v, ll times){ //!< times個先  times=0も可
		DEPS(i, 0, MSB(times))  if ((times>>i)&1) v = next_(v, i);
		return v;
	}
	ll TimesPow2(ll v, ll i) { return next[v][i]; }  //!< 2^i個先
	ll next_(ll v, ll i){ return (v==INF) ? INF : next[v][i]; }
};

struct DepthOfTree{
	vvll &to; vll depth;
	DepthOfTree(vvll &to, ll root): to(to), depth(to.size()) { dfs(root, -1, 0); }
	void dfs(ll v, ll p, ll d){ EACH(u, to[v]) if (u!=p) dfs(u, v, depth[u]=d+1); }
	vll get(){ return move(depth); }
};

struct ParentOfTree{
	vvll &to; vll par;
	ParentOfTree(vvll &to, ll root, ll p=-1): to(to), par(to.size()) { dfs(root, p); }
	void dfs(ll v, ll p){ par[v] = p;  EACH(u, to[v]) if (u!=p) dfs(u, v); }
	vll get(){ return move(par); }
};

struct LCA{
	vvll &to; vll depth; Dubling pDbl;
	LCA(vvll &to, ll root): to(to), depth(move(DepthOfTree(to, root).get())),
		pDbl(ParentOfTree(to, root, INF).get(), (ll)to.size()) {}
	ll operator() (ll v, ll u){
		ll dv = this->depth[v], du = this->depth[u];
		if (dv < du) { swap(v, u); swap(dv, du); }
		if ((v = this->pDbl.Times(v, dv-du)) == u) return v;
		DEPS(i, 0, MSB(du)){
			ll vv = this->pDbl.TimesPow2(v, i), uu = this->pDbl.TimesPow2(u, i);
			if (vv != uu) { v = vv; u = uu; }
		}
		return this->pDbl.TimesPow2(v, 0);
	}
};


struct UVW{ ll u, v, w; };

void solve()
{
	ll n, m;  cin >> n >> m;
	ll x;  cin >> x;
	vector<UVW> uvw = cinv3<UVW>(m);
	each(e, uvw){
		e.u--; e.v--;
		if (e.u>e.v)swap(e.u, e.v);
	}
	//vector<vector<pair<ll, ll>>> to = cinWGraph<ll>(n, m, false);

	//辺を重み順にソート
#if 0
	vector<UVW> uvw;
	rep(v, 0, n-1){
		each(e, to[v]){
			ll u; ll w;  tie(u, w) = e;
			if (v>u)continue;
			uvw.push_back({u, v, w});
		}
	}
#endif
	sort(ALL(uvw), [](UVW&a, UVW&b){return a.w<b.w; });

	//(u,v)からwを引く
	map<pll, ll> uv2w;
	each(e, uvw){
		uv2w[{e.u, e.v}]=e.w;
	}

	//最小全域木求める
	UnionFindTree uft(n);
	vector<UVW> m_uvw;
	vector<UVW> r_uvw;
	ll wm=0;//最小全域木の重み
	{
		ll cnt=0;
		each(e, uvw){
			if (uft.Same(e.u, e.v)){
				r_uvw.push_back(e);
			}
			else{
				m_uvw.push_back(e);
				wm+=e.w;
				cnt++;
				uft.Unite(e.u, e.v);
			}
		}
	}

	if (x<wm){
		cout << 0 << '\n'; return;
	}

	vvll to(n);
	each(e,m_uvw){
		to[e.u].push_back(e.v);
		to[e.v].push_back(e.u);
	}

	LCA lca(to, 0);
	vll parent = ParentOfTree(to, 0).get();

	ll q=0;
	ll r=0;
	ll k=0;
	each(e, r_uvw){
		ll lc=lca(e.u, e.v);

		auto mxd=[&](ll v){
			ll mx=0;
			while (v!=lc){
				ll p=parent[v];
				ll u1=v;
				ll u2=p;
				if (u1>u2)swap(u1, u2);
				ll w=uv2w[{u1, u2}];
				chmax(mx, w);
				v=p;
			}
			return mx;
		};
		
		ll mxed=max(mxd(e.u), mxd(e.v));
		ll we=wm-mxed+e.w;

		if (we<x) q++;
		else if (we>x) r++;
		else k++;
	}

	if (wm<x){
		mll ans=POW(mll(2), k+1)-2;
		ans*=POW(mll(2), r);
		cout << ans << '\n';
	}
	else{
		mll ans=POW(mll(2), m);
		ans-=POW(mll(2), r+1);
		cout << ans << '\n';
	}
}

int main(){
#if 1
	solve();
#else
	ll t;  cin >> t;
	rep(i, 0, t-1){
		solve();
	}
#endif
	return 0;
}


Measured memory_bytes: 4084