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: Accepted)

#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, u32, u64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);
parse_tuple!(A, B, C, D);
parse_tuple!(A, B, C, D, E);

// ===

type Graph = Vec<Vec<(usize, i64, bool)>>;

const INF: i64 = 1e18 as i64;
const MOD: i64 = 1_000_000_007;

fn main() {
    let (n, m): (usize, usize) = read();
    let (s, t): (usize, usize) = read();
    let (s, t) = (s-1, t-1);


    let mut edges = vec![];
    let mut graph: Graph = vec![vec![]; n];
    for _ in 0..m {
        let (u, v, d): (usize, usize, i64) = read();
        graph[u-1].push((v-1, d, true));
        graph[v-1].push((u-1, d, true));
        edges.push((u-1, v-1));
    }

    let (dist_from_s, _) = dist_from(&graph, s);
    let (dist_from_t, _) = dist_from(&graph, t);

    for i in 0..n {
        let deg = graph[i].len();
        for j in 0..deg {
            let e = graph[i][j];
            if !valid(i, e.0, e.1, s, t, &dist_from_s, &dist_from_t) {
                graph[i][j].2 = false;
            }
        }
    }

    let (dist_from_s, ways_from_s) = dist_from(&graph, s);
    let (dist_from_t, ways_from_t) = dist_from(&graph, t);
    let mut total = ways_from_s[t] * ways_from_s[t] % MOD;
    for i in 0..n {
        if dist_from_s[i] == dist_from_t[i] {
            let ws = ways_from_s[i];
            let wt = ways_from_t[i];
            total += MOD - ws * ws % MOD * wt % MOD * wt % MOD;
            total %= MOD;
        }
    }
    for i in 0..n {
        let deg = graph[i].len();
        for j in 0..deg {
            if !graph[i][j].2 || dist_from_s[i] > dist_from_s[graph[i][j].0] {
                continue;
            }
            let (u, v, d) = (i, graph[i][j].0, graph[i][j].1);
            if max(dist_from_s[u], dist_from_t[v]) < min(dist_from_s[u] + d, dist_from_t[v] + d) {
                let ws = ways_from_s[u];
                let wt = ways_from_t[v];
                total += MOD - ws * ws % MOD * wt % MOD * wt % MOD;
                total %= MOD;
            }
        }
    }

    println!("{}", total % MOD);
}

fn valid(u: usize, v: usize, d: i64, s: usize, t: usize, dist_s: &Vec<i64>, dist_t: &Vec<i64>) -> bool {
    if dist_s[v] < dist_s[u] {
        return valid(v, u, d, s, t, dist_s, dist_t);
    }
    let total = dist_s[t];
    dist_s[u] + d + dist_t[v] == total && dist_s[u] + d == dist_s[v] && dist_t[v] + d == dist_t[u]
}

fn dist_from(graph: &Graph, from: usize) -> (Vec<i64>, Vec<i64>) {
    let n = graph.len();
    let mut ret = vec![INF; n];
    let mut ways = vec![0; n];
    ret[from] = 0;
    ways[from] = 1;

    let mut heap = BinaryHeap::new();
    heap.push((0, from));

    while heap.len() >= 1 {
        let (dist, idx) = heap.pop().unwrap();
        let dist = -dist;
        if ret[idx] < dist {
            continue;
        }

        let wa = ways[idx];
        for &(to, d, valid) in &graph[idx] {
            if ret[to] > dist + d {
                ret[to] = dist + d;
                ways[to] = wa;
                heap.push((-ret[to], to))
            } else if ret[to] == dist + d {
                ways[to] += wa;
                ways[to] %= MOD;
            }
        }
    }
    (ret, ways)
}