#include <bits/stdc++.h>
#ifdef LOCAL
#include "print_utils.h"
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define forn(i, n) for(auto (i) = 0; (i) < (int)(n); ++(i))
#define for1(i, n) for(auto (i) = 1; (i) <= (int)(n); ++(i))
#define ford(i, n) for(auto (i) = (int)(n) - 1; (i) >= 0; --(i))
#define forab(i, a, b) for(auto (i) = (int)(a); (i) < (int)(b); ++(i))

int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

template <class T>
T remin(T &x, T val) {
    if (x > val) {x = val;}
    return x;
}

template <class T>
T remax(T &x, T val) {
    if (x < val) {x = val;}
    return x;
}

const ll mod = 1000 * 1000 * 1000 + 7;

ll pw(ll a, ll n) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ret;
}

ll gcd(ll a, ll b) {
    a = std::abs(a);
    b = std::abs(b);
    while (b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}

struct Q {
    ll n, d;
    Q(): n(0), d(1) {}
    Q(ll nn, ll dd) {
        n = nn;
        d = dd;
        ll g = gcd(n, d);
        n /= g;
        d /= g;
        if (d < 0) {
            n *= -1;
            d *= -1;
        }
    }

    bool operator < (const Q &r) const {
        return n * r.d < r.n * d;
    }
    bool operator <= (const Q &r) const {
        return n * r.d <= r.n * d;
    }
    bool operator == (const Q &r) const {
        return n * r.d == r.n * d;
    }
};

struct Edge {
    int a, b;
    ll w;
};

vector<Edge> e;

int get(int id, int from) {
    if (e[id].a == from) return e[id].b;
    return e[id].a;
}

struct DSU {
    vector<int> p;
    vector<int> sz;
    void init(int n) {
        p.resize(n);
        sz.resize(n);
        forn(i, n) {
            p[i] = i;
            sz[i] = 1;
        }
    }

    int get(int v) {
        if (p[v] == v) return v;
        return p[v] = get(p[v]);
    }

    bool unite(int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) return false;
        if (sz[a] < sz[b]) {
            swap(a, b);
        }
        p[b] = a;
        sz[a] += sz[b];
        return true;
    }
};
vector<vector<int> > g;

pair<ll, int> dfs(int v, int b, int p = -1) {
    pair<ll, int> ret;
    ret.x = 0;
    ret.y = 0;
    if (v == b) {
        ret.y = 1;
        return ret;
    }
    for (int id : g[v]) {
        int to = get(id, v);
        if (to == p) continue;
        auto pa = dfs(to, b, v);
        if (pa.y) {
            if (ret.y == 0) {
                ret.y = 1;
                ret.x = pa.x;
            } else {
                ret.x = max(ret.x, pa.x);
            }
            ret.x = max(ret.x, e[id].w);
        }
    }
    return ret;
}



void solve() {
    int n, m;
    ll x;
    cin >> n >> m >> x;
    e.resize(m);

    forn(i, m) {
        cin >> e[i].a >> e[i].b >> e[i].w;
        --e[i].a;
        --e[i].b;
    }

    DSU dsu;
    dsu.init(n);
    vector<int> ids(m);
    forn(i, m) ids[i] = i;
    sort(ids.begin(), ids.end(), [&](int l, int r) {
        return e[l].w < e[r].w;
    });

    vector<int> mst_ids;
    ll mst_weight = 0;
    for (int id : ids) {
        if (dsu.unite(e[id].a, e[id].b)) {
            mst_ids.push_back(id);
            mst_weight += e[id].w;
        }
    }
    if (mst_weight > x) {
        cout << "0\n";
        return;
    }
    g.assign(n, vector<int>());

    for (int id : mst_ids) {
        g[e[id].a].push_back(id);
        g[e[id].b].push_back(id);
    }
    ll ans = 0;
    if (mst_weight == x) {
        ans += pw(2, m - n + 1) * (pw(2, n - 1) - 2) % mod;
        ans %= mod;
        if (ans < 0) ans += mod;
    }
    vector<char> used_mst(m, 0);
    for (int id : mst_ids) {
        used_mst[id] = 1;
    }
    vector<ll> delta(m);
    forn(i, m) {
        if (used_mst[i]) {
            delta[i] = 0;
        } else {
            auto pa = dfs(e[i].a, e[i].b, -1);
            assert(pa.y > 0);
            delta[i] = e[i].w - pa.x;
            assert(delta[i] >= 0);
        }
    }
    int cnt = 0;
    int cnt2 = 0;
    for (int i = 0; i < m; ++i) {
        if (used_mst[i]) continue;
        if (delta[i] == x - mst_weight) {
            ++cnt;
        } else if (delta[i] < x - mst_weight) {
            ++cnt2;
        }
    }
    if (cnt == 0) {
        cout << ans << "\n";
        return;
    }
    ans += (pw(2, cnt) - 1) * pw(2, m - (n - 1) - cnt - cnt2) % mod * 2 % mod;
    ans %= mod;
    if (ans < 0) {
        ans += mod;
    }
    cout << ans << "\n";
}



int main(int argc, char **argv) {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    double cl0 = clock();
    int t = 1;
//    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }
    std::cerr << "time: " << (clock() - cl0) / CLOCKS_PER_SEC << std::endl;
    return 0;
}


Measured memory_bytes: 512