#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 5010;
const int MOD = 1e9 + 7;

long long inq(long long x, long long y)
{
    if (!y) return 1;
    long long l = inq(x, y / 2);
    l = l * l % MOD;
    if (y % 2) return l * x % MOD;
    return l;
}


struct Edge{
    int v, u, w;
};

vector<Edge> edges;
int n, m, k;

bool comp(Edge a, Edge b)
{
    return a.w < b.w;
}

int root[N], sz[N];

int Root(int x)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x]);
}

int Merge(Edge a)
{
    int v = a.v, u = a.u;
    v = Root(v), u = Root(u);
    if (v == u) return 0;
    if (sz[v] < sz[u]) root[v] = u, sz[u] += sz[v];
    else root[u] = v, sz[v] += sz[u];
    return 1;
}

vector<pair<int, int> > graph[N];

int len[N][N];

void dfs(int v, int par, int rt, int d)
{
    len[rt][v] = d;
    for (auto uw : graph[v]) if (uw.first != par) dfs(uw.first, v, rt, max(d, uw.second));
}

int s[N];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    for (int i = 0; i < N; i++) root[i] = i, sz[i] = 1;
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++)
    {
        int v, u, w;
        cin >> v >> u >> w;
        edges.push_back({v, u, w});
    }
    sort(all(edges), comp);
    int cnt = 0, weight = 0;
    for (int i = 0; i < m; i++)
    {
        if (Merge(edges[i])) cnt++, weight += edges[i].w, graph[edges[i].u].push_back({edges[i].v, edges[i].w}), graph[edges[i].v].push_back({edges[i].u, edges[i].w});
        else s[i] = 1;
    }
    if (cnt != n - 1 || weight > k) cout << 0, exit(0);
    int d = k - weight;
    for (int i = 1; i <= n; i++)
    {
        dfs(i, -1, i, 0);
    }
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < m; i++)
    {
        if (s[i] == 1)
        {
            int x = edges[i].w - len[edges[i].u][edges[i].v];
            if (x < d) a++;
            if (x == d) b++;
            if (x > d) c++;
        }
    }
    long long ans = 2 * inq(2, c) * (inq(2, b) - 1) % MOD;
    if (d == 0)
    {
        ans = (ans + (inq(2, n - 1) - 2) * inq(2, m - n + 1)) % MOD;
    }
    cout << ans;
}


Measured memory_bytes: 40192