Problem:
Score : 900 points

Problem Statement

We have an undirected weighted graph with N vertices and M edges.
The i-th edge in the graph connects Vertex U_i and Vertex V_i, and has a weight of W_i.
Additionally, you are given an integer X.

Find the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo 10^9 + 7:

The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of X.

Here, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.

Constraints

1 \leq N \leq 1 000

1 \leq M \leq 2 000

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq W_i \leq 10^9 (1 \leq i \leq M)

If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j).

U_i \neq V_i (1 \leq i \leq M)

The given graph is connected.

1 \leq X \leq 10^{12}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
X
U_1 V_1 W_1
U_2 V_2 W_2
:
U_M V_M W_M

Output

Print the answer.

Sample Input 1

3 3
2
1 2 1
2 3 1
3 1 1

Sample Output 1

6

Sample Input 2

3 3
3
1 2 1
2 3 1
3 1 2

Sample Output 2

2

Sample Input 3

5 4
1
1 2 3
1 3 3
2 4 6
2 5 8

Sample Output 3

0

Sample Input 4

8 10
49
4 6 10
8 4 11
5 8 9
1 8 10
3 8 128773450
7 8 10
4 2 4
3 4 1
3 1 13
5 2 2

Sample Output 4

4

Solution in Rust (1.15.1) (judge verdict: Accepted)

// 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);
    }
}