#include<bits/stdc++.h>
using namespace std;


#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1


#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;

vector<PI> graph[1010];
vector<PI> graph2[1010];

PI cost_table[1010][1010];

vector<pair<li, PI>> edges;

li n, m, x;

li ni[3010];
bool used[2020];

#define UF_NUM 5050

li parent[UF_NUM];
inline void init_uf() {
    rep(i, UF_NUM)parent[i] = i;
}

inline li find(li x) {
    if (parent[x] == x)return x;
    else return (parent[x] = find(parent[x]));
}

inline void union_(li x, li y) {
    li parent_x = find(x);
    li parent_y = find(y);
    parent[parent_x] = parent_y;
}

inline void dfs(li root, li now, li from, li now_dist, li min_cost) {
    cost_table[root][now] = {now_dist, min_cost};
    rep(i, sz(graph2[now])) {
        li next = graph2[now][i].F;
        if (next == from)continue;
        li next_cost = graph2[now][i].S;
        dfs(root, next, now, now_dist + next_cost, max(min_cost, next_cost));
    }
}

int main() {
    init_uf();
    cin >> n >> m >> x;

    rep(i, m) {
        li u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        graph[u].pb({v, w});
        graph[v].pb({u, w});
        edges.pb({w, {u, v}});
    }
    sort(allof(edges));

    li mst_cost = 0;
    rep(i, sz(edges)) {
        li u = edges[i].S.F;
        li v = edges[i].S.S;
        li w = edges[i].F;
        used[i] = false;
        if (find(u) != find(v)) {
            union_(u, v);
            mst_cost += w;
            graph2[u].pb({v, w});
            graph2[v].pb({u, w});
            used[i] = true;
        }
    }

    rin{
        dfs(i, i, -1, 0, -1);
    }

    if (mst_cost > x) {
        puts("0");
        return 0;
    }

    ni[0] = 1;
    repp(i, 1, m + 2) {
        ni[i] = ni[i - 1] * 2 % MOD;
    }

    li res = 0;
    if (mst_cost == x) {
        li mst_color_num = (ni[n - 1] - 2 + MOD) % MOD;
        li rest_color_num = ni[m - n + 1];
        res = (res + mst_color_num * rest_color_num) % MOD;
    }

    li rest_good_edge_num = 0;
    li rest_bad_edge_num = 0;
    rep(i, m) {
        if (used[i])continue;
        li u = edges[i].S.F;
        li v = edges[i].S.S;
        li w = edges[i].F;
        li new_cost = mst_cost - cost_table[u][v].S + w;
        if (new_cost == x) {
            rest_good_edge_num++;
        } else if (new_cost > x) {
            rest_bad_edge_num++;
        }
    }
    res = (res + ((2 * (ni[rest_good_edge_num] - 1) % MOD) * ni[rest_bad_edge_num] % MOD)) % MOD;
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 16508