#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


vector<vector<pair<int,int> > >adj;
int n; // number of nodes
int power[2005];
const int mod=1e9+7;
vector<int> tin, low;
int timer;
int vis[1005];
bool mm[2005];
int ans[2005];

int parent[1005];
int sz[1005];
int find_parent(int ver)
{
    if(ver==parent[ver])
            return ver;
    return parent[ver]=find_parent(parent[ver]);
}

bool merge(int a,int b)
{
    a=find_parent(a);
    b=find_parent(b);
    if(a!=b)
    {
        if(sz[a]<sz[b])
                swap(a,b);
        sz[a]+=sz[b];
        parent[b]=a;
        return true;
    }
    return false;
}

void dfs(int ver,int dest)
{
    // cout<<ver<<" ";
    vis[ver]=1;
    parent[ver]=dest;
    for(auto i:adj[ver])
    {
        if(!vis[i.first])
        {
            dfs(i.first,dest);
        }
    }
}

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);  
cout.tie(0);   

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif  

    power[0]=1;
    for(int i=1;i<=2000;i++)
    {
        power[i]=(power[i-1]*2)%mod;
    }
    int m;
    cin>>n>>m;
    int spam;
    cin>>spam;
    adj.resize(n+1);
    int x,y,z;
    vector<pair<pair<int,int> ,pair<int,int> > >pp;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y>>z;
        adj[x].push_back({y,i});
        adj[y].push_back({x,i});
        pp.push_back({{z,i},{x,y}});
    }
    for(int i=1;i<=n;i++)
    {
        parent[i]=i;
        sz[i]=1;
    }
    sort(pp.begin(),pp.end());
    int sum=0;
    for(auto i:pp)
    {
        if(merge(i.second.first,i.second.second))
        {
            mm[i.first.second]=1;            
            sum+=i.first.first;
        }
    }

    if(spam<sum)
    {
        cout<<0;
        return 0;
    }

    spam-=sum;
    reverse(pp.begin(),pp.end());

    for(auto i:pp)
    {
        if(!mm[i.first.second])
            continue;
        fill_n(vis,n+1,0);
        adj.clear();
        adj.resize(n+1);
        for(auto j:pp)
        {
            if(mm[j.first.second])
                ans[j.first.second]=2;
            if(i.first.second!=j.first.second && mm[j.first.second])
            {
                adj[j.second.first].push_back({j.second.second,j.first.second});
                adj[j.second.second].push_back({j.second.first,j.first.second});
            }
        }
        for(int j=1;j<=n;j++)
        {
            if(!vis[j])
                dfs(j,j);
        }

        int z=spam+i.first.first;
        for(auto j:pp)
        {
            int x=j.second.first;
            int y=j.second.second;
            if(i.first.second==j.first.second)
                    continue;
            if(parent[x]!=parent[y])
            {
                if(ans[j.first.second]==-1)
                        continue;
                if(j.first.first<z)
                {
                    ans[j.first.second]=-1;
                }
                else if(j.first.first==z)
                {
                    ans[j.first.second]=1;
                }
            }
        }
    }
    x=0;
    y=0;
    z=0;
    for(int i=1;i<=m;i++)
    {
        if(ans[i]==-1)
                continue;
        else if(ans[i]==1)
                x++;
        else if(ans[i]==2)
                y++;
        else
                z++;
    }
    int res=1;
    if(spam==0)
    {
        if(x+y+z==m)
        {
            if(x+y<2)
            {
                cout<<0;
                return 0;
            }
            res=(power[x+y]-2+mod)%mod;
            res=(res*power[z])%mod;
        }
        else
        {            
            res=(res*(power[x+y]-1+mod))%mod;
            res=(res*power[z])%mod;
            res=(res*2)%mod;
        }
    }
    else
    {
        if(x+z==m)
        {
            if(x<2)
            {
                cout<<0;
                return 0;
            }
            res=(power[x]-2+mod)%mod;
            res=(res*power[z])%mod;
        }
        else
        {            
            res=(res*(power[x]-1+mod))%mod;
            res=(res*power[z])%mod;
            res=(res*2)%mod;
        }        
    }

    cout<<res;

}


Measured memory_bytes: 512