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)

/// E: Bichrome Spanning Tree <https://beta.atcoder.jp/contests/arc093/tasks/arc093_c>
/// TLE: 2 sec
/// MLE: 256 MB

fn main() {
    let (n, m) = {
        let t: Vec<usize> = reads();
        (t[0], t[1])
    };

    let x: i64 = reads()[0];

    let mut es: Vec<(usize, usize, i64)> = (0..m)
        .map(|_| {
            let t: Vec<usize> = reads();
            (t[0] - 1, t[1] - 1, t[2] as i64)
        })
        .collect();

    if n < 3 {
        println!("0");
        return;
    }

    es.sort_by(|a, b| a.2.cmp(&b.2));

    use modulo::Int;
    let mut total = Int::new(0);

    for i in 1..m {
        let mut uf = union_find::UnionFind::new(n);
        let mut cost = 0;

        uf.union(es[i].0, es[i].1);
        cost += es[i].2;

        for j in 0..m {
            let f = uf.find(es[j].0);
            let t = uf.find(es[j].1);

            if f == t {
                continue;
            }

            uf.union(es[j].0, es[j].1);
            cost += es[j].2;
        }

        if cost != x {
            continue;
        }

        // let lt = es.iter().filter(|e| e.2 < es[i].2).count();
        // let ge = m - lt - 1;
        let ge = m - i - 1;

        total += Int::new(2).pow(ge as i64);
    }

    println!("{}", total * Int::new(2));
}

//----- Libraries -----

fn read_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_owned()
}

fn reads<T>() -> Vec<T>
where
    T: std::str::FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
{
    read_line()
        .split_whitespace()
        .map(|w| w.parse().unwrap())
        .collect()
}

pub mod modulo {
    use std::fmt;
    use std::ops;

    const M: i64 = 1000000007;

    #[derive(Debug, Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq)]
    pub struct Int(i64);

    impl fmt::Display for Int {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl Int {
        pub fn new(v: i64) -> Int {
            Int(v % M)
        }

        pub fn pow(self, mut r: i64) -> Int {
            let mut k = self;
            let mut ret = Int::new(1);

            while r > 0 {
                if r % 2 != 0 {
                    ret = ret * k;
                }
                r /= 2;
                k = k * k;
            }

            ret
        }

        // This requires M is prime
        pub fn recip(self) -> Int {
            self.pow(M - 2)
        }
    }

    impl ops::Add for Int {
        type Output = Int;
        fn add(self, rhs: Int) -> Int {
            Int::new(self.0 + rhs.0)
        }
    }
    impl ops::Sub for Int {
        type Output = Int;
        fn sub(self, rhs: Int) -> Int {
            Int::new(self.0 - rhs.0 + M)
        }
    }
    impl ops::Mul for Int {
        type Output = Int;
        fn mul(self, rhs: Int) -> Int {
            Int::new(self.0 * rhs.0)
        }
    }
    impl ops::Div for Int {
        type Output = Int;
        fn div(self, rhs: Int) -> Int {
            self * rhs.recip()
        }
    }

    impl ops::AddAssign for Int {
        fn add_assign(&mut self, rhs: Int) {
            *self = *self + rhs;
        }
    }
    impl ops::SubAssign for Int {
        fn sub_assign(&mut self, rhs: Int) {
            *self = *self - rhs;
        }
    }
    impl ops::MulAssign for Int {
        fn mul_assign(&mut self, rhs: Int) {
            *self = *self * rhs;
        }
    }
    impl ops::DivAssign for Int {
        fn div_assign(&mut self, rhs: Int) {
            *self = *self / rhs;
        }
    }

    impl ops::Neg for Int {
        type Output = Int;
        fn neg(self) -> Int {
            Int(0) - self
        }
    }
}

pub mod union_find {
    use std::iter::FromIterator;

    pub struct UnionFind(Vec<usize>);

    impl UnionFind {
        pub fn new(n: usize) -> UnionFind {
            UnionFind(Vec::from_iter(0..n))
        }

        pub fn find(&mut self, i: usize) -> usize {
            if self.0[i] == i {
                i
            } else {
                let p = self.0[i];
                self.0[i] = self.find(p);
                self.0[i]
            }
        }

        pub fn union(&mut self, i: usize, j: usize) {
            let ni = self.find(i);
            let nj = self.find(j);
            self.0[ni] = nj;
        }
    }
}

//----- Tests -----

#[cfg(test)]
mod test {
    use std::io::Write;
    use std::process::{Command, Stdio};

    fn do_test(sample_input: &str, sample_output: &str) {
        let mut child = Command::new("cargo")
            .args(&["run", "--bin", "arc093_c", "-q"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();

        write!(child.stdin.as_mut().unwrap(), "{}", sample_input.trim()).unwrap();
        let output = child.wait_with_output().unwrap();

        assert_eq!(
            sample_output.trim(),
            String::from_utf8(output.stdout).unwrap().trim()
        );
    }

    #[test]
    fn sample1() {
        let input = r#"
            3 3
2
1 2 1
2 3 1
3 1 1
        "#;
        let output = r#"
            6
        "#;
        do_test(input, output);
    }
    #[test]
    fn sample2() {
        let input = r#"
            3 3
3
1 2 1
2 3 1
3 1 2
        "#;
        let output = r#"
            2
        "#;
        do_test(input, output);
    }
    #[test]
    fn sample3() {
        let input = r#"
            5 4
1
1 2 3
1 3 3
2 4 6
2 5 8
        "#;
        let output = r#"
            0
        "#;
        do_test(input, output);
    }
    #[test]
    fn sample4() {
        let input = r#"
            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
        "#;
        let output = r#"
            4
        "#;
        do_test(input, output);
    }
}