// https://atcoder.jp/contests/arc093/tasks/arc093_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

struct UnionFind {
      n: usize,
      parent: Vec<usize>,
      count: Vec<usize>
}

impl UnionFind {
     fn new(n: usize) -> Self {
           let mut p = vec![0; n];
           for i in 0..n {
                 p[i] = i;
           }
           UnionFind { n: n, parent: p, count: vec![1; n] }
     }

     fn find(&mut self, a: usize) -> usize {
           if self.parent[a] == a {
                 return a;
           }
           let par = self.parent[a];
           self.parent[a] = self.find(par);
           self.parent[a]
     }

     fn unite(&mut self, a: usize, b: usize) {
           let a = self.find(a);
           let b = self.find(b);
           if a == b {
                 return;
           }
           let ca = self.count[a];
           let cb = self.count[b];
           let total = ca + cb;
           self.count[b] = total;
           self.count[a] = total;
           if ca < cb {
                 self.parent[a] = b;
           } else {
                 self.parent[b] = a;
           }
     }

     fn same(&mut self, a: usize, b: usize) -> bool {
           self.find(a) == self.find(b)
     }

     fn count(&mut self, a: usize) -> usize {
           let a = self.find(a);
           self.count[a]
     }
}


use std::rc::Rc;
type Merger<T> = Rc<Fn(T, T) -> T>;
type Graph<T> = Vec<Vec<(usize, T)>>;
struct Lca<T> {
    default: T,
    tree: Graph<T>,
    depth: Vec<usize>,
    parents: Vec<Vec<(usize, T)>>,
    merger: Merger<T>,
}

impl<T: Copy> Lca<T> {
    fn new(tree: Graph<T>, default: T, merger: Merger<T>) -> Self {
        let n = tree.len();
        let pn = (64 - (n as u64).leading_zeros()) as usize;

        let mut parents = vec![vec![(0, default); n]; pn];
        let mut depth = vec![0; n];

        Lca::dfs(&tree, &mut parents, &mut depth, 0, n, default);

        for i in 1..pn {
            for j in 0..n {
                let (p, t) = parents[i-1][j];
                let (pp, tt) = parents[i-1][p];
                parents[i][j] = (pp, merger(t, tt));
            }
        }

        Lca {
            default: default,
            tree: tree,
            depth: depth,
            parents: parents,
            merger: merger
        }
    }

    fn dfs(tree: &Graph<T>, parents: &mut Vec<Vec<(usize, T)>>, depth: &mut Vec<usize>, now: usize, par: usize, val: T) {
        if par != tree.len() {
            parents[0][now] = (par, val);
            depth[now] = depth[par] + 1;
        }
        for &t in &tree[now] {
            if par == t.0 {
                continue;
            }
            Lca::dfs(tree, parents, depth, t.0, now, t.1);
        }
    }

    fn lca(&self, u: usize, v: usize) -> (usize, T) {
        if u == v {
            return (u, self.default);
        }
        if self.depth[u] > self.depth[v] {
            self.lca(v, u)
        } else if self.depth[u] < self.depth[v] {
            let mut diff = self.depth[v] - self.depth[u];
            let mut v = v;
            let mut pi = 0;
            let mut ret = self.default;
            while diff >= 1 {
                if diff & 1 == 1 {
                    ret = (self.merger)(ret, self.parents[pi][v].1);
                    v = self.parents[pi][v].0;
                }
                pi += 1;
                diff >>= 1;
            }
            let (lca, val) = self.lca(u, v);
            (lca, (self.merger)(val, ret))
        } else {
            let mut pi = self.parents.len();
            let mut u = u;
            let mut v = v;
            let mut ret = self.default;
            while pi > 0 {
                pi -= 1;
                if self.parents[pi][u].0 != self.parents[pi][v].0 {
                    ret = (self.merger)(ret, self.parents[pi][u].1);
                    ret = (self.merger)(ret, self.parents[pi][v].1);
                    u = self.parents[pi][u].0;
                    v = self.parents[pi][v].0;
                }
            }
            ret = (self.merger)(ret, self.parents[0][u].1);
            ret = (self.merger)(ret, self.parents[0][v].1);
            assert!(self.parents[0][u].0 == self.parents[0][v].0);
            (self.parents[0][u].0, ret)
        }
    }
}

const MOD: i64 = 1e9 as i64 + 7;

fn main() {
    input! {
        n: usize, m: usize,
        x: i64,
        edges: [(usize1, usize1, i64); m]
    };

    let mut edges = edges;
    edges.sort_by_key(|&(_, _, w)| w);

    let mut uf = UnionFind::new(n);
    let mut tree = vec![vec![]; n];

    let mut other_edges = vec![];
    let mut total = 0;
    for &(u, v, w) in &edges {
        if uf.same(u, v) {
            other_edges.push((u, v, w));
            continue;
        }
        uf.unite(u, v);
        tree[u].push((v, w));
        tree[v].push((u, w));
        total += w;
    }

    let mut lca = Lca::new(tree, 0, Rc::new(|i, j| max(i, j)));
    let mut p2 = vec![0; n+m+1];
    p2[0] = 1;
    for i in 1..n+m+1 {
        p2[i] = p2[i-1] * 2 % MOD;
    }

    if x < total {
        println!("0");
        return;
    } else if x == total {
        let mut possible = n-1;
        let mut larger = 0;
        for &(u, v, w) in &other_edges {
            let (_, w2) = lca.lca(u, v);
            assert!(w >= w2);
            if w == w2 {
                possible += 1;
            } else {
                larger += 1;
            }
        }
        // debug!("eq", possible, larger);
        let total_ways = (p2[possible]+MOD-2)%MOD*p2[larger]%MOD;
        println!("{}", total_ways);
    } else {
        let mut possible = 0;
        let mut larger = 0;
        let d = x - total;
        for &(u, v, neww) in &other_edges {
            let (_, oldw) = lca.lca(u, v);
            // debug!(neww, oldw);
            assert!(neww >= oldw);
            let diff = neww - oldw;
            if diff < d {
                // should be same color
            } else if diff == d {
                possible += 1;
            } else {
                larger += 1;
            }
        }
        // debug!("lg", d, possible, larger);
        let total_ways = 2 * (p2[possible]+MOD-1) % MOD * p2[larger] % MOD;
        println!("{}", total_ways);
    }
}


Measured memory_bytes: 4352