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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        var mod = 1000000007L;
        var maxD = mod * 100000L;
        var nm = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var st = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var N = nm[0];
        var M = nm[1];
        var S = st[0] - 1;
        var T = st[1] - 1;

        var uv = new List<int>[N];
        var ud = new List<int>[N];
        for (int i = 0; i < N; i++) { uv[i] = new List<int>(); ud[i] = new List<int>(); }

        for (var i = 0; i < M; i++) {
            var uvdl = Console.ReadLine().Split(new char[] { ' ' }).Select(word => int.Parse(word)).ToList();
            var u = uvdl[0] - 1;
            var v = uvdl[1] - 1;
            var d = uvdl[2] * 2;
            uv[u].Add(v);
            ud[u].Add(d);
            uv[v].Add(u);
            ud[v].Add(d);
        }

        var DS = new long[N];
        var DT = new long[N];
        for (var i = 0; i < N; i++) {
            DS[i] = maxD;
            DT[i] = maxD;
        }
        DS[S] = 0;
        DT[T] = 0;

        var OS = new List<int>();
        var OT = new List<int>();
        var PK = new int[N];
        var FS = new long[N]; FS[S] = 1;
        var FT = new long[N]; FT[T] = 1;

        var QS1 = new int[M]; QS1[0] = S;
        var QS2 = new long[M]; QS2[0] = 0;
        int QSC = 0;
        var QT1 = new int[M]; QT1[0] = T;
        var QT2 = new long[M]; QT2[0] = 0;
        int QTC = 0;


        var pd = QS2[0];
        var rd = QT2[0];
        var f = true;

        while (f && QSC >= 0 && QTC >= 0) {
            if (pd <= rd) {
                while (QSC >= 0 && pd == QS2[0]) {
                    var p = QS1[0];
                    var q = QS1[QSC];
                    var d = QS2[QSC];
                    QSC--;
                    if (QSC >= 0) {
                        var j = 0; var i = 1;
                        while (i <= QSC) {
                            if (i < QSC && (QS2[i] > QS2[i + 1])) i++;
                            if (QS2[i] >= d) break;
                            QS1[j] = QS1[i];
                            QS2[j] = QS2[i];
                            j = i;
                            i = j * 2 + 1;
                        }
                        QS1[j] = q;
                        QS2[j] = d;
                    }
                    if (PK[p] == 2) { f = false; continue; }
                    if (pd > DS[p]) { continue; }
                    for (var x = 0; x < uv[p].Count; x++) {
                        q = uv[p][x];
                        d = ud[p][x] + pd;
                        if (DS[q] > d) {
                            DS[q] = d;
                            FS[q] = FS[p];
                            var j = QSC + 1;
                            while (j > 0) {
                                var i = (j - 1) / 2;
                                if (QS2[i] <= d) break;
                                QS1[j] = QS1[i];
                                QS2[j] = QS2[i];
                                j = i;
                            }
                            QS1[j] = q;
                            QS2[j] = d;
                            QSC++;
                        } else if (DS[q] == d) {
                            FS[q] = (FS[q] + FS[p]) % mod;
                        }
                    }
                    PK[p] = 1;
                    OS.Add(p);
                }
                pd = QS2[0];
            } else {
                while (QTC >= 0 && rd == QT2[0]) {
                    var r = QT1[0];
                    var q = QT1[QTC];
                    var d = QT2[QTC];
                    QTC--;
                    if (QTC >= 0) {
                        var j = 0; var i = 1;
                        while (i <= QTC) {
                            if (i < QTC && (QT2[i] > QT2[i + 1])) i++;
                            if (QT2[i] >= d) break;
                            QT1[j] = QT1[i];
                            QT2[j] = QT2[i];
                            j = i;
                            i = j * 2 + 1;
                        }
                        QT1[j] = q;
                        QT2[j] = d;
                    }
                    if (PK[r] == 1) { f = false; continue; }
                    if (rd > DT[r]) { continue; }
                    for (var x = 0; x < uv[r].Count; x++) {
                        q = uv[r][x];
                        d = ud[r][x] + rd;
                        if (DT[q] > d) {
                            DT[q] = d;
                            FT[q] = FT[r];
                            var j = QTC + 1;
                            while (j > 0) {
                                var i = (int)((j - 1) / 2);
                                if (QT2[i] <= d) { break; }
                                QT1[j] = QT1[i];
                                QT2[j] = QT2[i];
                                j = i;
                            }
                            QT1[j] = q;
                            QT2[j] = d;
                            QTC++;
                        } else if (DT[q] == d) {
                            FT[q] = (FT[q] + FT[r]) % mod;
                        }
                    }
                    PK[r] = 2;
                    OT.Add(r);
                }
                rd = QT2[0];
            }
        }

        var OS2 = new List<int>();
        long da = pd + rd;
        foreach (var p in OS) {
            if (DS[p] + DT[p] < da) {
                da = DS[p] + DT[p];
                OS2.Clear(); OS2.Add(p);
            } else if (DS[p] + DT[p] == da) {
                OS2.Add(p);
            }
        }
        var coltime = da / 2;

        long t = 0; long s = 0;
        foreach (var p in OS2) {
            if (DS[p] == coltime) {
                var v = (FS[p] * FT[p]) % mod;
                s = (s + v) % mod;
                t = (t + v * v) % mod;
            } else {
                for (var x = 0; x < uv[p].Count; x++) {
                    var q = uv[p][x];
                    if (PK[q] != 2) continue;
                    if (DS[q] + DT[q] != da) continue;
                    if (DS[p] + ud[p][x] != DS[q]) continue;
                    var v = (FS[p] * FT[q]) % mod;
                    s = (s + v) % mod;
                    t = (t + v * v) % mod;
                }
            }
        }

        t = (s * s + mod - t) % mod;
        Console.Write(t);
    }
}