#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
typedef vector<vector<ll>> mat;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<60;
double pi=3.141592653589793238462643383279L;
double eps=1e-14;
#define rep(i,m,n) for(int i=m;i<n;i++)
#define rrep(i,n,m) for(int i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;
        
    }
    return ret;
}
ll ne;

vector<pll> vn[800010];
ll par[60][800010];
ll dep[800010];
void lcadfs(ll v,ll p,ll d){
  par[0][v]=p;
  dep[v]=d;
  for(ll i=0;i<vn[v].size();i++){
    if(vn[v][i].first!=p)lcadfs(vn[v][i].first,v,d+1);
  }
}
void lcainit(ll n){
  lcadfs(ne,-1,0);
  for(ll i=0;i<59;i++){
    for(ll j=1;j<=n;j++){
      if(par[i][j]<0)par[i+1][j]=-1;
      else par[i+1][j]=par[i][par[i][j]];
    }
  }
}
ll lca(ll u,ll v){
  if(dep[u]>dep[v])swap(u,v);
  for(ll i=0;i<60;i++){
    if((dep[v]-dep[u])>>i&1)v=par[i][v];
  }
  if(u==v)return u;
  for(ll i=59;i>=0;i--){
    if(par[i][u]!=par[i][v]){
      u=par[i][u];
      v=par[i][v];
    }
  }
  return par[0][u];
}
//neの指定！！
ll mae[60][4010];
void maedfs(ll v,ll p,ll dis){
  mae[0][v]=dis;
  for(ll i=0;i<vn[v].size();i++){
    if(vn[v][i].first!=p)maedfs(vn[v][i].first,v,vn[v][i].second);
  }
}
void maeinit(ll n){
  maedfs(ne,-1,-1);
  for(ll i=0;i<59;i++){
    for(ll j=1;j<=n;j++){
      if(par[i][j]<0)mae[i+1][j]=-1;
      mae[i+1][j]=max(mae[i][j],mae[i][par[i][j]]);
    }
  }
}
ll maxedge(ll u,ll v){
  ll r=lca(u,v);
  ll ret=0;
  for(ll i=59;i>=0;i--){
    if((dep[u]-dep[r])>>i&1){
      ret=max(ret,mae[i][u]);
      u=par[i][u];
    }
  }
  for(ll i=59;i>=0;i--){
    if((dep[v]-dep[r])>>i&1){
      ret=max(ret,mae[i][v]);
      v=par[i][v];
    }
  }
  return ret;
}
ll par2[800010];
ll len[800010];
ll you[800010];
void init(ll n){
  for(int i=0;i<n;i++){
    par2[i]=i;
    len[i]=0;
    you[i]=1;
  }
}
ll root(ll n){
  if(par2[n]==n)return n;
  return par2[n]=root(par2[n]);
}
void unit(ll a,ll b){
  a=root(a);
  b=root(b);
  if(a==b)return;
  if(len[a]<len[b]){
    par2[a]=b;
    you[b]+=you[a];
  }
  else{
    par2[b]=a;
    you[a]+=you[b];
    if(len[a]==len[b])len[a]++;
  }
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m;cin>>n>>m;
    ll x;cin>>x;
    vector<pll> v;
    ll a[m],b[m],c[m];
    rep(i,0,m){
        cin>>a[i]>>b[i]>>c[i];
        a[i]--;
        b[i]--;
        v.push_back({c[i],i});
    }
    init(n);
    ll sum=0;
    bool used[m];
    fill(used,used+m,false);
    ll cnt=0;
    sort(v.begin(),v.end());
    rep(i,0,v.size()){
        ll num=v[i].second;
        ll f=a[num],g=b[num];
        if(root(f)==root(g))continue;
        used[num]=true;
        sum+=c[num];
        cnt++;
        unit(f,g);
        if(cnt==n-1)break;
    }
    //cout<<sum<<endl;
    //rep(i,0,m)if(used[i])cout<<i<<" ";
    //cout<<endl;
    ll ans=0;
    if(sum==x){
        ans=(beki(2,n-1,mod)-2+mod)%mod;
        (ans*=beki(2,m-(n-1),mod))%=mod;
    }
    rep(i,0,m){
        if(used[i]){
            vn[a[i]].push_back({b[i],c[i]});
            vn[b[i]].push_back({a[i],c[i]});
        }
    }
    ne=0;
    lcainit(n);
    maeinit(n);
    //cout<<ans<<endl;
    vector<ll> ww;
    rep(i,0,m){
        //cout<<ans<<endl;
        ll num=v[i].second;
        if(used[num])continue;
        ll f=a[num],g=b[num];
        ll h=maxedge(f,g);
        ll ns=sum-h+c[num];
        ww.push_back(ns);
    }
    sort(ww.begin(),ww.end());
    ll zan=m-(n-1);
    rep(i,0,ww.size()){
        if(ww[i]<x){
            zan--;
        }
        else if(ww[i]==x){
            (ans+=beki(2,zan,mod))%=mod;
            zan--;
        }
        else break;
    }
    cout<<ans<<endl;
}







Measured memory_bytes: 150784