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: Time Limit Exceeded)

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

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 = Vec::new();
    q.push(s);

    while let Some(p0) = q.pop() {
        let t0 = t[p0];
        if t0 >= t[g] {
            continue;
        }
        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.push(p1);
                t[p1] = t1;
            }
            r.push(p0);
        }
    }
    let tg = t[g];
    if tg == usize::max_value() {
        return 0;
    }
    drop(ds);

    // 中間地点の取得
    let mut ms0 = Vec::new(); // 中間地点リスト(sに近い点)
    let mut ms1 = Vec::new(); // 中間地点リスト(gに近い点)
    let mut vs = vec![false; n]; // 既に訪れた点はtrue
    q.push(g);
    vs[g] = true;
    while let Some(p1) = q.pop() {
        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;
        }
    }

    // 中間からs,gへの最短ルート数の計算
    struct HalfGraph<'a> {
        ms: &'a [usize],      // 中間地点（開始地点）
        rs: &'a [Vec<usize>], // 最短ルートのリンク
        cs: Vec<usize>,       // ルート数
        g: usize,             // ゴール地点
    }
    let ms_len = ms0.len();
    let mut hs = [
        HalfGraph {
            ms: &mut ms0,
            rs: &mut rs0,
            cs: Vec::with_capacity(ms_len),
            g: s,
        },
        HalfGraph {
            ms: &mut ms1,
            rs: &mut rs1,
            cs: Vec::with_capacity(ms_len),
            g: g,
        },
    ];
    for h in &mut hs {
        let mut cs: Vec<Option<usize>> = vec![None; n];
        cs[h.g] = Some(1);
        q.extend(h.ms);
        while let Some(&p0) = q.last() {
            if let Some(_) = cs[p0] {
                q.pop();
                continue;
            }
            let q_len = q.len();
            let mut c0 = 0;
            for &p1 in &h.rs[p0] {
                if let Some(c1) = cs[p1] {
                    c0 += c1;
                } else {
                    q.push(p1);
                }
            }
            if q.len() == q_len {
                cs[p0] = Some(c0);
                q.pop();
            }
        }
        for &m in h.ms {
            h.cs.push(cs[m].unwrap());
        }
    }
    // パターン数の積を合計
    let mut sum = 0;
    let mod_val = 1000000007;
    for i0 in 0..ms_len {
        for i1 in 0..ms_len {
            if i0 == i1 {
                continue;
            }
            sum += hs[0].cs[i0] * hs[1].cs[i1];
            sum %= mod_val;
        }
    }
    sum
}

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()
}