#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

template <long long mod>
struct modular {
    long long value;
    modular(long long x = 0) {
        value = x % mod;
        if (value < 0) value += mod;
    }
    modular& operator+=(modular other) {
        if ((value += other.value) >= mod) value -= mod;
        return *this;
    }
    modular& operator-=(modular other) {
        if ((value -= other.value) < 0) value += mod;
        return *this;
    }
    modular& operator*=(modular other) {
        value = value * other.value % mod;
        return *this;
    }
    modular& operator/=(modular other) {
        long long a = 0, b = 1, c = other.value, m = mod;
        while (c != 0) {
            long long t = m / c;
            m -= t * c;
            swap(c, m);
            a -= t * b;
            swap(a, b);
        }
        a %= mod;
        if (a < 0) a += mod;
        value = value * a % mod;
        return *this;
    }
    modular operator-() { return modular(-value); }
    modular operator+(modular rhs) { return modular(*this) += rhs; }
    modular operator-(modular rhs) { return modular(*this) -= rhs; }
    modular operator*(modular rhs) { return modular(*this) *= rhs; }
    modular operator/(modular rhs) { return modular(*this) /= rhs; }
    bool operator==(modular rhs) { return value == rhs.value; }
    bool operator!=(modular rhs) { return value != rhs.value; }
    bool operator<(modular rhs) { return value < rhs.value; }
};
template <long long mod>
string to_string(modular<mod> x) {
    return to_string(x.value);
}
template <long long mod>
ostream& operator<<(ostream& stream, modular<mod> x) {
    x.value %= mod;
    if (x.value < 0) x.value += mod;
    return stream << x.value;
}
template <long long mod>
istream& operator>>(istream& stream, modular<mod>& x) {
    stream >> x.value;
    x.value %= mod;
    if (x.value < 0) x.value += mod;
    return stream;
};
const long long mod = (long long)1e9 + 7;
using mint = modular<mod>;

inline mint pw(mint a, long long n) {
    mint res = 1;
    while (n > 0) {
        if (n & 1) {
            res *= a;
        }
        a *= a;
        n >>= 1;
    }
    return res;
}
vector<mint> fact, finv;
inline void cinit(int n) {
    fact.resize(n, 1);
    finv.resize(n, 1);
    for (int i = 2; i < n; i++) {
        fact[i] = fact[i - 1] * i;
    }
    finv[n - 1] /= fact[n - 1];
    for (int i = n - 2; i >= 2; i--) {
        finv[i] = finv[i + 1] * (i + 1);
    }
}
inline mint C(int n, int k) {
    if (n < k || k < 0 || n < 0) return 0;
    return fact[n] * finv[k] * finv[n - k];
}

struct edge {
    int from;
    int to;
    long long cost;
};

struct dsu {
    vector<int> p;
    vector<int> sz;
    int n;
    dsu(int _n)
        : n(_n) {
        p.resize(n);
        iota(p.begin(), p.end(), 0);
        sz.assign(n, 1);
    }

    inline int get(int x) {
        if (p[x] == x) {
            return x;
        } else {
            return p[x] = get(p[x]);
        }
    }
    inline bool unite(int x, int y) {
        x = get(x);
        y = get(y);
        if (x == y) {
            return false;
        }
        if (sz[x] < sz[y]) {
            swap(x, y);
        }
        p[x] = y;
        sz[y] += sz[x];
        return true;
    }
};

vector<int> min_span(vector<edge> edges, int n, long long& ans) {
    vector<int> order(edges.size());
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int a, int b) {
        return edges[a].cost < edges[b].cost;
    });
    vector<int> res;
    ans = 0;
    dsu d(n);
    for (int id : order) {
        edge e = edges[id];
        if (d.get(e.from) != d.get(e.to)) {
            d.unite(e.from, e.to);
            res.emplace_back(id);
            ans += e.cost;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    vector<edge> edges(m);
    ll x;
    cin >> x;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        edges[i] = {u, v, w};
        g[u].emplace_back(i);
        g[v].emplace_back(i);
    }
    vector<int> mst;
    vector<bool> ok(m);
    ll mn;
    mst = min_span(edges, n, mn);
    for (int i : mst) {
        ok[i] = true;
    }
    vector<ll> c(m);
    for (int i = 0; i < m; i++) {
        vector<ll> mx(n);
        function<void(int, int)> dfs = [&](int v, int p) {
            for (int id : g[v]) {
                if (!ok[id]) {
                    continue;
                }
                int to = edges[id].from ^ edges[id].to ^ v;
                if (to == p) {
                    continue;
                }
                mx[to] = max(mx[v], edges[id].cost);
                dfs(to, v);
            }
        };
        dfs(edges[i].from, -1);
        c[i] = mn + edges[i].cost - mx[edges[i].to];
    }
    debug(mn, c);
    ll lo = 0, same = 0, hi = 0;
    for (int i = 0; i < m; i++) {
        if (c[i] < x) {
            lo++;
        } else if (c[i] == x) {
            same++;
        } else {
            hi++;
        }
    }
    if (same == 0) {
        cout << 0 << '\n';
        return 0;
    }
    mint ans;
    if (lo == 0) {
        ans = pw(2, same) - 2;
        ans *= pw(2, hi);
    } else {
        ans = pw(2, same) - 1;
        ans *= pw(2, hi + 1);
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 512