Problem:
Score : 700 points

Problem Statement

We have a graph with N vertices and M edges, and there are two people on the graph: Takahashi and Aoki.

The i-th edge connects Vertex U_i and Vertex V_i.
The time it takes to traverse this edge is D_i minutes, regardless of direction and who traverses the edge (Takahashi or Aoki).

Takahashi departs Vertex S and Aoki departs Vertex T at the same time. Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the shortest time possible.
Find the number of the pairs of ways for Takahashi and Aoki to choose their shortest paths such that they never meet (at a vertex or on an edge) during the travel, modulo 10^9 + 7.

Constraints

1 \leq N \leq 100 000

1 \leq M \leq 200 000

1 \leq S, T \leq N

S \neq T

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

1 \leq D_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)

D_i are integers.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M

Output

Print the answer.

Sample Input 1

4 4
1 3
1 2 1
2 3 1
3 4 1
4 1 1

Sample Output 1

2

There are two ways to choose shortest paths that satisfies the condition:

Takahashi chooses the path 1 \rightarrow 2 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 4 \rightarrow 1.

Takahashi chooses the path 1 \rightarrow 4 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 2 \rightarrow 1.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2

Sample Input 4

8 13
4 2
7 3 9
6 2 3
1 6 4
7 6 9
3 8 9
1 2 2
2 8 12
8 6 9
2 5 5
4 2 18
5 3 7
5 1 515371567
4 8 6

Sample Output 4

6

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

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

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(&mut i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Edge {
    p: (usize, usize),
    d: usize,
}

fn run<R: BufRead, W: Write>(i: &mut R, o: &mut W) {
    let mut i = AtRead::from(i);
    let (n, m) = i.read2::<usize, usize>();
    let (s, t) = i.read2::<usize, usize>();
    let (s, t) = (s - 1, t - 1);
    let mut es = Vec::new();
    for _ in 0..m {
        let (u, v, d) = i.read3::<usize, usize, usize>();
        let (u, v, d) = (u - 1, v - 1, d);
        es.push(Edge { p: (u, v), d: d });
    }
    writeln!(o, "{}", solve(n, s, t, es)).unwrap();
}
fn solve(n: usize, s: usize, g: usize, es: Vec<Edge>) -> usize {
    let mut ds = vec![Vec::new(); n];
    for e in es {
        ds[e.p.0].push((e.p.1, e.d));
        ds[e.p.1].push((e.p.0, e.d));
    }

    // 最短距離ルートの探索
    let mut t = vec![usize::max_value(); n]; // スタート地点からの距離
    t[s] = 0;

    let mut rs0 = vec![Vec::new(); n]; // rs[i]は点iからsへの最短距離でのリンク先
    let mut rs1 = vec![Vec::new(); n]; // rs[i]は点iからgへの最短距離でのリンク先
    let mut q = BTreeSet::new();
    q.insert((0, s));

    while let Some(&(t0, p0)) = q.iter().next() {
        q.remove(&(t0, p0));
        let t0 = t[p0];
        if t0 > t[g] {
            break;
        }
        for &(p1, d) in &ds[p0] {
            let t1_old = t[p1];
            let t1 = t0 + d;
            if t1 > t1_old {
                continue;
            }
            let r = &mut rs0[p1];
            if t1 < t1_old {
                r.clear();
                q.remove(&(t1_old, p1));
                q.insert((t1, p1));
                t[p1] = t1;
            }
            r.push(p0);
        }
    }
    let tg = t[g];
    if tg == usize::max_value() {
        return 0;
    }
    drop(ds);
    drop(q);

    // 中間地点の取得
    let mut ms0 = Vec::new(); // 中間地点リスト(sに近い点)
    let mut ms1 = Vec::new(); // 中間地点リスト(gに近い点)
    let mut vs = vec![false; n]; // 既に訪れた点はtrue
    let mut ps = Vec::new(); // 最短ルートまでに使用する点
    let mut q = Vec::new();
    q.push(g);
    vs[g] = true;
    while let Some(p1) = q.pop() {
        ps.push(p1);
        if t[p1] * 2 == tg {
            ms0.push(p1);
            ms1.push(p1);
        }
        for &p0 in &rs0[p1] {
            rs1[p0].push(p1);
            if t[p1] * 2 > tg && tg > t[p0] * 2 {
                ms0.push(p0);
                ms1.push(p1);
            }
            if vs[p0] {
                continue;
            }
            q.push(p0);
            vs[p0] = true;
        }
    }
    ps.sort_by_key(|&p| t[p]);

    // ルート数の計算
    let mod_val = 1000000007;

    let mut cs0 = vec![0; n];
    cs0[s] = 1;
    for &p1 in ps.iter().skip(1) {
        let mut c = 0;
        for &p0 in &rs0[p1] {
            c += cs0[p0];
        }
        cs0[p1] = c % mod_val;
    }

    let mut cs1 = vec![0; n];
    cs1[g] = 1;
    for &p0 in ps.iter().rev().skip(1) {
        let mut c = 0;
        for &p1 in &rs1[p0] {
            c += cs1[p1];
        }
        cs1[p0] = c % mod_val;
    }

    // 衝突パターン数の計算
    let mut sumc = 0;
    for i in 0..ms0.len() {
        let d0 = cs0[ms0[i]];
        let d1 = cs1[ms1[i]];
        sumc += d0 * d0 * d1 * d1;
        sumc %= mod_val;
    }
    let cs_all = cs0[g];
    (mod_val + cs_all * cs_all - sumc) % mod_val
}

struct AtRead<'a, R: BufRead + 'a> {
    r: &'a mut R,
    s: String,
}

macro_rules! fn_read {
        {$f:ident($($v:ident: $t:ident),*)} => {
            fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.next().split(' ');
                $(
                    let $v = next(i);
                )*
                ($($v),*)
            }
        };
    }

impl<'a, R: BufRead + 'a> AtRead<'a, R> {
    fn from(r: &'a mut R) -> Self {
        AtRead {
            r: r,
            s: String::new(),
        }
    }
    fn next(&mut self) -> &str {
        self.s.clear();
        self.r.read_line(&mut self.s).unwrap();
        self.s.trim()
    }

    fn_read! { read2(v1: T1, v2: T2) }
    fn_read! { read3(v1: T1, v2: T2, v3: T3) }
}

fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
    i.next().unwrap().parse().ok().unwrap()
}