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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

#!/usr/bin/env python3

import heapq

INF = 10 ** 18
MOD = 10 ** 9 + 7

def solve(n, m, s, t, edges):

    dist = [[INF] * n for _ in range(2)]
    dist[0][s] = 0
    dist[1][t] = 0

    dp = [[0] * n for _ in range(2)]
    dp[0][s] = 1
    dp[1][t] = 1

    target = [t, s]

    for i in range(2):
        target_i = target[i]
        dist_i = dist[i]
        dp_i = dp[i]
        q = []

        for v in range(n):
            heapq.heappush(q, (dist_i[v], v))

        end = False
        while 0 < len(q):
            du, u = heapq.heappop(q)
            if du != dist_i[u]:
                continue
            if end and dist_i[target_i] < du:
                break
            for v, d in edges[u]:
                alt = du + d
                if dist_i[v] > alt:
                    dist_i[v] = alt
                    dp_i[v] = dp_i[u]
                    heapq.heappush(q, (alt, v))
                elif dist_i[v] == alt:
                    dp_i[v] += dp_i[u]
                if v == target_i:
                    end = True

    spt = dist[0][t]

    c_point = set()
    n_point = set()
    f_point = set()
    c_edge = set()
    for v in range(n):
        if dist[0][v] + dist[1][v] == spt:
            if dist[0][v] == dist[1][v]:
                c_point.add(v)
            elif dist[0][v] < dist[1][v]:
                n_point.add(v)
            else:
                f_point.add(v)

    for u in n_point:
        for v, d in edges[u]:
            if v in f_point:
                if dist[0][u] + d + dist[1][v] == spt:
                    c_edge.add((u, v))


    ans = 0
    for u in c_point:
        dpu = dp[0][u] * dp[1][u]
        for v in c_point:
            if u == v: continue
            ans += dpu * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            ans += dpu * dp[0][vn] * dp[1][vf]
            ans %= MOD
    for un, uf in c_edge:
        dpu = dp[0][un] * dp[1][uf]
        for v in c_point:
            ans += dpu * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            if un == vn and uf == vf: continue
            ans += dpu * dp[0][vn] * dp[1][vf]
            ans %= MOD

    return ans


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    s, t = input().split()
    s = int(s) - 1
    t = int(t) - 1
    edges = [[] for _ in range(n)]
    for _ in range(m):
        u, v, d = input().split()
        u = int(u) - 1
        v = int(v) - 1
        d = int(d)
        edges[u].append((v, d))
        edges[v].append((u, d))

    print(solve(n, m, s, t, edges))


if __name__ == '__main__':
    main()