//Create Time: 2018-04-26 23:11:20
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <functional>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <ctime>
#include <utility>
#include <complex>
#include <cassert>
#include <climits>
#include <iterator>
#include <tuple>
using namespace std;
#define snuke(i,x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,n,a) for (int i=(n)-1;i>=(a);i--)
// #define OUT(x) std::cout<<(#x)<<":"<<(x)<<std::endl
// #define TST(x) std::cout<<(#x)<<":"<<(x)<<" "
#define OUT(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);OUT_TST("\n",_it,args);}
#define TST(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);OUT_TST("",_it,args);}
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define unq(x) sort(all(x)),(x).erase(unique(all(x)),(x).end())
#define mem(f,x) memset((f), (x), sizeof(f))
#define clr(f) mem(f,0)
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define eb emplace_back
#define DEBUG
void OUT_TST(string s,istream_iterator<string> it) {cerr<<s;}
template<typename T, typename... Args> void OUT_TST(string s,istream_iterator<string> it,T a,Args... args){cerr<<*it<<"="<<a<<" ";OUT_TST(s,++it,args...);}
template<class T> void rd(T&ret){ret=0;bool ok=0,u=0;for(;;){int c=getchar();if(c>='0'&&c<='9')ret=(ret<<3)+(ret<<1)+c-'0',ok=1;else if(c=='-')u=1;else if(ok){if(u)ret*=-1;return;}}}
long long powmod(long long p,long long n,long long mod){long long ret=1;for(;n;n>>=1){if(n&1)ret=ret*p%mod;p=p*p%mod;}return ret;}
template <class T> bool chmin(T& a, const T &b) {return b < a? a = b, 1: 0;}
template <class T> bool chmax(T& a, const T &b) {return b > a? a = b, 1: 0;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef tuple<int,int,int> tp;
/****head****/
const int M=2222;
const int N=1111;
const ll mod=(ll)1e9+7;
namespace Kruskal{
    const int N=::N;
    typedef ::tp tp;
    //typedef tuple<int,int,int> tp;      //s,e,w
    bool cmp(const tp &a, const tp &b){
        return get<2>(a)<get<2>(b);
    }
    int f[N];                   //并查集
    int find(int x){
        return x==f[x]?f[x]:f[x]=find(f[x]);
    }

    ll gao(int n, int m, tp *p, vector<pair<int,int> > *mst, vector<pair<int,int> > *other)       //n为点的数量，m为边的数量
    {
        sort (p, p+m, cmp);
        for (int i = 0; i < n; ++ i)
            f[i] = i;

        ll cost = 0;
        for (int i = 0; i < m; ++ i){
            int s,e,w; tie(s,e,w)=p[i];
            int t1 = find(s), t2 = find(e);
            if (t1 != t2){
                cost += w;
                f[t1] = t2;
                mst[s].emplace_back(e,w);
                mst[e].emplace_back(s,w);
            } else{
                other[s].emplace_back(e,w);
                other[e].emplace_back(s,w);
            }
        }

        int tmp;
        for (int i = 0; i < n; ++ i){
            if (!i) tmp = find(i);
            else if (tmp != find(i)) return -1;
        }
        return cost;
    }
}
tp pn[M];
vii other[N],mst[N];
int mx[N];
int n,m;
ll x;
void dfs(int u,int f,int val){
    mx[u]=val;
    for(auto i: mst[u])if(i.x!=f){
        dfs(i.x,u,max(val,i.y));
    }
}
int main(){
    cin>>n>>m>>x;
    rep(i,0,m){
        int u,v,w;scanf("%d%d%d",&u,&v,&w);--u;--v;
        // eg[u].eb(v,w);
        // eg[v].eb(u,w);
        pn[i]=tp{u,v,w};
    }
    ll cost=Kruskal::gao(n,m,pn,mst,other);
    // rep(i,0,n){
        // TST(i);
        // for(auto j:other[i])TST(j.x,j.y);
        // puts("");
    // }
    ll ans=0;
    OUT(x,cost);
    if(cost==x){
        ll a=0,add;
        rep(i,0,n){
            dfs(i,-1,0);
            for(auto j: other[i]){
                add=j.y-mx[j.x];
                if(add==0)++a;
            }
        }
        a=a/2+n-1;
        ans=(powmod(2,a,mod)+mod-2)*powmod(2,m-a,mod)%mod;
    } else if(cost<x){
        ll a=0,b=0,add;
        rep(i,0,n){
            dfs(i,-1,0);
            for(auto j: other[i]){
                add=j.y-mx[j.x];
                if(add<x-cost)++a;
                else if(add==x-cost)++b;
            }
        }
        b=b/2;
        a=a/2+n-1;
        // OUT(b);
        // OUT(a);
        // OUT(powmod(2,m-a-b,mod));
        if(b){
            ans=(powmod(2,b+1,mod)+mod-2) * powmod(2,m-a-b,mod) % mod;
        }
    }
    cout<<ans<<endl;
            
    return 0;
}


Measured memory_bytes: 512