//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <stdio.h>
#include <bits/stdc++.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 1000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int p[N], r[N];

void build(int n)
{
    rep(i, 0, n)
    {
        p[i] = i;
        r[i] = 0;
    }
}

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

bool uni(int x, int y)
{
    x = get(x); y = get(y);
    if (x == y)
        return false;
    if (r[x] >= r[y])
        p[y] = x;
    else
        p[x] = y;
    if (r[x] == r[y])
        r[x]++;
    return true;
}

map<pair<int, int>, int> mapa;
vector<pair<int, int> > g[N];
bool used[N];
vector<int> order;
vector<int> order_id;

bool dfs(int u, int t)
{
    used[u] = true;
    if (u == t)
        return true;
    for (auto v : g[u])
        if (!used[v.fi])
        {
            order.push_back(v.se);
            order_id.push_back(mapa[{u, v.fi}]);
            if (dfs(v.fi, t))
                return true;
            order.pop_back();
            order_id.pop_back();
        }
    return false;
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    ll x;
    cin >> n >> m >> x;
    if (n == 1)
    {
        cout << 0 << "\n";
        return 0;
    }
    vector<pair<int, pair<int, int> > > edges(m), rem;
    rep(i, 0, m)
    {
        int u, v, w;
        cin >> u >> v >> w; u--; v--;
        edges[i] = {w, {u, v}};
        mapa[{u, v}] = i;
        mapa[{v, u}] = i;
    }
    build(n);
    sort(edges.begin(), edges.end());
    ll mst = 0;
    rep(i, 0, m)
    {
        int u = edges[i].se.fi, v = edges[i].se.se, w = edges[i].fi;
        if (uni(u, v))
        {
            g[u].push_back({v, w});
            g[v].push_back({u, w});
            //cout << u + 1 << " " << v + 1 << "\n";
            mst += w;
        }
        else
        {
            rem.push_back({w, {u, v}});
        }
    }
    if (mst > x)
    {
        cout << 0 << "\n";
        return 0;
    }
    int cnt_long = 0;
    vector<int> good;
    for (auto it : rem)
    {
        int u = it.se.fi, v = it.se.se, w = it.fi;
        order.clear();
        order_id.clear();
        fill(used, used + n, false);
        dfs(u, v);
        int max_v = order[max_element(order.begin(), order.end()) - order.begin()];
        int cnt_max = 0;
        vector<int> max_ids;
        rep(i, 0, sz(order))
            if (order[i] == max_v)
            {
                cnt_max++;
                max_ids.push_back(order_id[i]);
            }
        if (mst - max_v + w < x)
            continue;
        else if (mst - max_v + w > x)
            cnt_long++;
        else
        {
            good.push_back(mapa[{u, v}]);
        }
    }
    /*for (int i : good)
        cout << i << " ";
    cout << "\n";*/
    ll ans = 1;
    if (mst == x)
    {
        int cnt_ok = sz(good) + (n - 1);
        rep(i, 0, cnt_ok)
            ans = ans * 2 % MOD;
        ans = (ans - 2 + MOD) % MOD;
        rep(i, 0, m - cnt_ok)
            ans = ans * 2 % MOD;
        cout << ans << "\n";
    }
    else
    {
        rep(i, 0, sz(good))
            ans = ans * 2 % MOD;
        ans = (ans - 1 + MOD) % MOD;
        rep(i, 0, cnt_long)
            ans = ans * 2 % MOD;
        cout << ans * 2 % MOD << "\n";
    }
    return 0;
}


Measured memory_bytes: 768