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

#define fs first
#define sc second

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


// std::vector Declaration
template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// std::vector Initialization
template<typename T>
void fill_all(T& arr, const T& v) { arr = v; }
template<typename T, typename U>
void fill_all(T& arr, const U& v) { for (auto& i : arr) fill_all(i, v); }

// std::vector Debug
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    bool a = 1;
    for (auto e : v) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::deque Debug
template<typename T>
ostream& operator<<(ostream& os, const deque<T>& d) {
    os << "[";
    bool a = 1;
    for (auto e : d) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::pair Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << " " << p.second << ")";
    return os;
}

// std::set Debug
template<typename T>
ostream& operator<<(ostream& os, const set<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::multiset Debug
template<typename T>
ostream& operator<<(ostream& os, const multiset<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::map Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& mp) {
    os << "{";
    bool a = 1;
    for (auto e : mp) {
        os << (a ? "" : " ");
        os << e.first << ":" << e.second;
        a = 0;
    }
    os << "}";
    return os;
}

// std::tuple Debug
template<int N, class Tuple>
void out(ostream& os, const Tuple& t){}
template<int N, class Tuple, class H, class ...Ts>
void out(ostream& os, const Tuple& t) {
    if (N) os << " ";
    os << get<N>(t);
    out<N+1,Tuple,Ts...>(os, t);
}
template<class ...Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) {
    os << "(";
    out<0,tuple<Ts...>,Ts...>(os, t);
    os << ")";
    return os;
}

// Debug
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl


//struct edge { int to, cost; };
//ostream& operator<<(ostream& os, const edge& e) {
//    os << "(" << e.to << ", " << e.cost << ")";
//    return os;
//}

const LL LINF = 1LL<<60;
const int IINF = 1<<30;
const LL MOD = 1e9+7;

struct edge { int from, to; int cost; };
ostream& operator<<(ostream& os, const edge& e) {
    os << "(" << e.from << "->" << e.to << ":" << e.cost << ")";
    return os;
}

bool operator<(const edge &edge1, const edge &edge2) { return edge1.cost < edge2.cost; };

struct UnionFind
{
    vector<int> par, sz;
    UnionFind(int n) {
        par.resize(n);
        sz.resize(n, 1);
        for (int i = 0; i < n; ++i) par[i] = i;
    }
    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        sz[y] = 0;
        return true;
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    int size(int x) {
        return sz[root(x)];
    }
};

template<typename T>
T kruskal(auto E, auto& used, int n)
{
    used.resize(E.size(), false);

    UnionFind uf(n);

    vector<int> ord(E.size());
    iota(ord.begin(), ord.end(), 0);

    sort(ord.begin(), ord.end(), [&](int i, int j) {
            return E[i].cost < E[j].cost; });

    T sum = 0;
    for(int i = 0; i < E.size(); ++i)
    {
        edge e = E[ord[i]];
        if (!uf.issame(e.from, e.to))
        {
            uf.merge(e.from, e.to);
            sum += e.cost;
            used[ord[i]] = true;
        }
    }

    return sum;
}

void dfs(const auto& G, auto& maxPath, int v, int p) {
    maxPath[v][v] = 0;
    for (const auto& e : G[v])
    {
        if (e.to == p) continue;

        dfs(G, maxPath, e.to, v);

        for (int i = 0; i < maxPath[e.to].size(); ++i)
        {
            if (maxPath[e.to][i] == -1) continue;
            chmax(maxPath[v][i], max(e.cost, maxPath[e.to][i]));
        }
    }
}

class LCA {
    private:
        int n, log2_n;
        vector<int> depth;
        vector<vector<int>> parent;

        void dfs(const auto& G, int v, int p, int d) {
            depth[v] = d;
            parent[0][v] = p;
            for (auto e : G[v]) {
                if (e.to != p) dfs(G, e.to, v, d+1);
            }
        }

    public:
        LCA(const vector<vector<edge>>& G, int root=0) :
            n(G.size()), log2_n(log2(n)), depth(n),
            parent(log2_n+1, vector<int>(n,-1)) {

                dfs(G, root, -1, 0);

                for (int k = 0; k < log2_n; ++k) {
                    for (int v = 0; v < n; ++v) {
                        if (parent[k][v] != -1) {
                            parent[k+1][v] = parent[k][parent[k][v]];
                        }
                    }
                }
            }

        int operator()(int u, int v) {
            if (depth[u] > depth[v]) swap(u, v);

            // align the depth of u and v
            for (int k = 0; k <= log2_n; ++k) {
                if ((depth[v] - depth[u]) >> k & 1) {
                    v = parent[k][v];
                }
            }
            if (u == v) return u;

            // go back until u and v's parents do not match
            for (int k = log2_n; k >= 0; --k) {
                if (parent[k][u] != parent[k][v]) {
                    u = parent[k][u];
                    v = parent[k][v];
                }
            }
            return parent[0][u];
        }
};

template<long long mod>
struct modint {
    using LL = long long;

    LL val;
    modint(LL val=0) : val(((val % mod) + mod) % mod) {}

    const modint operator+() const { return *this; }
    const modint operator-() const { return (-val + mod) % mod; }
    const modint reciprocal() const { return power(mod-2); }

    modint& operator+=(const modint& rhs) { (val += rhs.val) %= mod; return *this; }
    modint& operator-=(const modint& rhs) { return *this += -rhs; }
    modint& operator*=(const modint& rhs) { (val *= rhs.val) %= mod; return *this; }
    modint& operator/=(const modint& rhs) { return *this *= rhs.reciprocal(); }

    const modint operator+(const modint& rhs) const { return modint(*this) += rhs; }
    const modint operator-(const modint& rhs) const { return modint(*this) -= rhs; }
    const modint operator*(const modint& rhs) const { return modint(*this) *= rhs; }
    const modint operator/(const modint& rhs) const { return modint(*this) /= rhs; }

    const modint power(long long n) const {
        modint ret = 1, tmp = val;
        while (n > 0) {
            if (n & 1) ret *= tmp;
            tmp *= tmp; n >>= 1;
        }
        return ret;
    }

    bool operator==(const modint& rhs) const { return val == rhs.val; }
    bool operator!=(const modint& rhs) const { return !(*this == rhs); }

    friend const modint operator+(const LL& lhs, const modint& rhs) {
        return modint(lhs) + rhs;
    }
    friend const modint operator-(const LL& lhs, const modint& rhs) {
        return modint(lhs) - rhs;
    }
    friend const modint operator*(const LL& lhs, const modint& rhs) {
        return modint(lhs) * rhs;
    }
    friend const modint operator/(const LL& lhs, const modint& rhs) {
        return modint(lhs) / rhs;
    }

    friend bool operator==(const LL& lhs, const modint& rhs) {
        return modint(lhs) == rhs;
    }
    friend bool operator!=(const LL& lhs, const modint& rhs) {
        return modint(lhs) != rhs;
    }

    friend ostream& operator<<(ostream& os, const modint& a) { return os << a.val; }
    friend istream& operator>>(istream& is, modint& a) {
        LL tmp; is >> tmp;
        a = tmp;
        return is;
    }
};

int main()
{
    int N, M; cin >> N >> M;
    LL X; cin >> X;

    vector<edge> E(M);
    for (int i = 0; i < M; ++i) {
        int u, v, w; cin >> u >> v >> w;
        --u; --v;
        E[i] = edge{ u, v, w };
    }

    vector<bool> used;
    LL K = kruskal<LL>(E, used, N);

    vector<vector<edge>> G(N);
    for (int i = 0; i < M; ++i) {
        if (!used[i]) continue;
        const auto& e = E[i];
        G[e.from].push_back(edge{ e.from, e.to, e.cost });
        G[e.to].push_back(edge{ e.to, e.from, e.cost });
    }

    auto maxPath = make_v<int>(N, N);
    fill_all(maxPath, -1);
    dfs(G, maxPath, 0, -1);

    auto lca = LCA(G, 0);

    vector<int> diff(M);
    for (int i = 0; i < M; ++i) {
        int u = E[i].from, v = E[i].to, c = E[i].cost;
        int p = lca(u, v);
        diff[i] = c - max(maxPath[p][u], maxPath[p][v]);
    }

    LL D = X - K;

    int low = 0, eq = 0, up = 0;
    for (int i = 0; i < M; ++i) {
        if (used[i]) continue;
        int d = diff[i];
        if (d < D) {
            ++low;
        } else if (d == D) {
            ++eq;
        } else {
            ++up;
        }
    }

    using Int = modint<MOD>;
    if (D < 0) {
        cout << 0 << endl;
    } else if (D == 0) {
        cout << (Int(2).power(N-1) - 2) * Int(2).power(M-N+1) +
            (Int(2).power(eq) - 1) * 2 * Int(2).power(up) << endl;
    } else {
        cout << (Int(2).power(eq) - 1) * 2 * Int(2).power(up) << endl;
    }

    return 0;
}


Measured memory_bytes: 4352