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: Wrong Answer)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
struct UnionFind {
    parent: Vec<i32>,
}

#[allow(dead_code)]
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind { parent: vec![-1; n] }
    }
    fn root(&mut self, x: usize) -> usize {
        let p = self.parent[x];
        return if p < 0 {
            x
        } else {
            self.parent[x] = self.root(p as usize) as i32;
            self.parent[x] as usize
        }
    }
    fn union_set(&mut self, x: usize, y: usize) -> bool {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return false; }
        if self.parent[y] < self.parent[x] { std::mem::swap(&mut x, &mut y); }
        self.parent[x] += self.parent[y];
        self.parent[y] = x as i32;
        true
    }
    fn find_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn size(&mut self, x: usize) -> usize {
        let r = self.root(x);
        -self.parent[r] as usize
    }
}

#[test]
fn union_find() {
    let mut ufind = UnionFind::new(10);
    assert!(ufind.root(0) == 0);
    assert!(ufind.size(3) == 1);
    assert!(ufind.union_set(1, 2));
    assert!(!ufind.union_set(1, 2));
    ufind.union_set(2, 3);
    let r = ufind.root(3);
    assert!(r == 1 || r == 2 || r == 3);
    assert!(ufind.size(3) == 3);
}

type Weight = i64;
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    graph[dest].push(Edge::new(*index, dest, src, weight));
    *index += 1;
}
#[allow(dead_code)]
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
}

const MOD: i64 = 1e+9 as i64 + 7;
fn main() {
    let n: usize = read();
    let m: usize = read();
    let x: i64 = read();
    let mut edges: Edges = vec![];
    let mut g: Graph = graph_new(n);
    let mut e = 0;
    for _ in 0..m {
        let mut f: usize = read();
        let mut t: usize = read();
        let w: i64 = read();
        f -= 1; t -= 1;
        edges.push(Edge::new(e, f, t, w));
        add_bi_edge(&mut g, &mut e, f, t, w);
    }
    edges.sort();
    println!("{:?}", edges);
    let mut ufind = UnionFind::new(n);
    let mut min_weights = vec![vec![1e+15 as i64; n]; n];
    let mut min_plus = vec![0 as i64; m];
    let mut sum = 0;
    for edge in &edges {
        if ufind.find_set(edge.src, edge.dest) {
            min_plus[edge.index] = edge.weight - min_weights[edge.src][edge.dest];
        } else {
            sum += edge.weight;
            let mut left = vec![];
            let mut right = vec![];
            for i in 0..n {
                if ufind.find_set(edge.src, i) { left.push(i); }
                if ufind.find_set(edge.dest, i) { right.push(i); }
            }
            for l in &left {
                for r in &right {
                    min_weights[*l][*r] = edge.weight;
                    min_weights[*r][*l] = edge.weight;
                }
            }
            ufind.union_set(edge.src, edge.dest);
        }
    }
    min_plus.sort();
    let mut ans = 1;
    if x == sum {
        let cnt = min_plus.iter().filter(|&v| *v == 0 ).count();
        for _ in 0..cnt { ans = (ans * 2) % MOD; }
        ans = (ans - 2 + MOD) % MOD;
    } else {
        let min_cnt = min_plus.iter().filter(|&v| *v == x - sum ).count();
        let cnt = min_plus.iter().filter(|&v| *v > x - sum ).count();
        for _ in 0..min_cnt { ans = (ans * 2) % MOD; }
        ans = (ans * 2 - 2 + MOD) % MOD;
        for _ in 0..cnt { ans = (ans * 2) % MOD; }
    }
    println!("{}", ans);
}