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)

use std::collections::BinaryHeap;

const MOD: usize = 1_000_000_007;

#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
    v: usize,
    cost: usize,
}

impl Ord for State {
    fn cmp(&self, other: &State) -> std::cmp::Ordering {
        other.cost.cmp(&self.cost)
    }
}

impl PartialOrd for State {
    fn partial_cmp(&self, other: &State) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

fn dijkstra(graph: &Vec<Vec<(usize, usize)>>, s: usize, t: usize) -> (Vec<usize>, Vec<usize>) {
    let mut dist = vec![std::usize::MAX; graph.len()];
    let mut pattern = vec![0; graph.len()];
    let mut heap = BinaryHeap::new();
    dist[s] = 0;
    pattern[s] = 1;
    heap.push(State { cost: 0, v: s });
    while let Some(State { v, cost }) = heap.pop() {
        if v == t { break; }
        if cost > dist[v] {
            continue;
        }
        for edge in graph.get(v).unwrap().iter() {
            let (to, d) = *edge;
            if cost + d == dist[to] {
                pattern[to] = (pattern[to] + pattern[v]) % MOD;
            } else if cost + d < dist[to] {
                pattern[to] = pattern[v];
                dist[to] = cost + d;
                heap.push(State { v: to, cost: cost + d });
            }
        }
    }

    (dist, pattern)
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let m: usize = sc.read();
    let s: usize = sc.read::<usize>() - 1;
    let t: usize = sc.read::<usize>() - 1;
    let mut edges = Vec::new();
    let mut graph: Vec<Vec<(usize, usize)>> = vec![Vec::new(); n];
    for _ in 0..m {
        let u = sc.read::<usize>() - 1;
        let v = sc.read::<usize>() - 1;
        let d: usize = sc.read();
        graph[u].push((v, d));
        graph[v].push((u, d));
        edges.push((v, u, d));
    }

    let (s_dist, s_pattern) = dijkstra(&graph, s, t);
    let (t_dist, t_pattern) = dijkstra(&graph, t, s);

    let length = s_dist[t];

    let mut ans = (s_pattern[t] * s_pattern[t]) % MOD;
    for edge in &edges {
        let (from, to, d) = {
            let (v, u, d) = *edge;
            if s_dist[v] < s_dist[u] {
                (v, u, d)
            } else {
                (u, v, d)
            }
        };

        if s_dist[to] != s_dist[from] + d { continue; }
        if s_dist[from] + d + t_dist[to] != length { continue; }

        if s_dist[from] * 2 < length && length < s_dist[to] * 2 {
            let mut tmp = (s_pattern[from] * t_pattern[to]) % MOD;
            tmp = (tmp * tmp) % MOD;
            ans = (ans + MOD - tmp) % MOD;
        }
    }

    for v in 0..n {
        if s_dist[v] * 2 == length {
            let mut tmp = (s_pattern[v] * t_pattern[v]) % MOD;
            tmp = (tmp * tmp) % MOD;
            ans = (ans + MOD - tmp) % MOD;
        }
    }

    println!("{}", ans);
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner { ptr: 0, length: 0, buf: vec![0; 1024], small_cache: vec![0; 1024] }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool { b == b'\n' || b == b'\r' || b == b'\t' || b == b' ' }

    fn read<T>(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)]).parse().unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}