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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<utility>
#include<bitset>
#include<complex>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define FOR(i,a,b) for(int i=a; i<b; ++i)
#define MP make_pair
#define PB push_back
#define fi first
#define se second
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 1e5, maxm = 2e5;
const int md = 1e9 + 7;

struct Edge {
    int id, to, dd;
    Edge() {}
    Edge(int _id, int _to, int _dd) { id = _id, to = _to, dd = _dd; }
};

int n, S, T;
vector<Edge> G[maxn + 5];
LL dist1[maxn + 5], dist2[maxn + 5], cnt1[maxn + 5], cnt2[maxn + 5];
bool nd[maxn + 5], eg[maxm + 5];

void Dij(int ss, LL* dist) {
    for(int i = 1; i <= n; ++i) dist[i] = 1LL << 50;
    dist[ss] = 0;
    priority_queue<pair<LL,int>> que;
    que.push(MP(0LL,ss));
    while(!que.empty()) {
        pair<LL,int> x = que.top(); que.pop();
        if(-x.fi != dist[x.se]) continue;
        for(auto it : G[x.se])
            if(-x.fi + it.dd < dist[it.to]) {
                dist[it.to] = -x.fi + it.dd;
                que.push(MP(-dist[it.to],it.to));
            }
    }
}

void Dfs1(int x, LL d) {
    if(cnt1[x] >= 0) return;
    if(x == S) { cnt1[x] = 1; return; }
    cnt1[x] = 0;
    for(auto it : G[x]) {
        if(d + it.dd + dist1[it.to] != dist1[T]) continue;
        if((d + it.dd) * 2 == dist1[T])
            nd[it.to] = 1;
        else if(abs(d-dist1[it.to]) <= it.dd)
            eg[it.id] = 1;
        Dfs1(it.to, d + it.dd);
        cnt1[x] += cnt1[it.to];
        if(cnt1[x] >= md) cnt1[x] -= md;
    }
}

void Dfs2(int x, LL d) {
    if(cnt2[x] >= 0) return;
    if(x == T) { cnt2[x] = 1; return; }
    cnt2[x] = 0;
    for(auto it : G[x]) {
        if(d + it.dd + dist2[it.to] != dist2[S]) continue;
        Dfs2(it.to, d + it.dd);
        if(!nd[it.to] && !eg[it.id]) {
            cnt2[x] += cnt2[it.to];
            if(cnt2[x] >= md) cnt2[x] -= md;
        }
    }
}

LL ans = 0;
void Dfs3(int x, LL d) {
    for(auto it : G[x]) {
        if(d + it.dd + dist1[it.to] != dist1[T]) continue;
        if(nd[x])
            ans = (ans - cnt1[x] * cnt2[x] % md + md) % md;
        else if(eg[it.id])
            ans = (ans - cnt1[it.to] * cnt2[x] % md + md) % md;
        else Dfs3(it.to, d + it.dd);
    }
}

int main() {
    freopen("in.txt", "r", stdin);
    int m;
    scanf("%d%d%d%d", &n, &m, &S, &T);
    for(int i = 1, u, v, d; i <= m; ++i) {
        scanf("%d%d%d", &u, &v, &d);
        G[u].PB( Edge(i,v,d) );
        G[v].PB( Edge(i,u,d) );
    }
    Dij(S, dist1);
    Dij(T, dist2);
    mem(cnt1, 0x80), mem(cnt2, 0x80);
    Dfs1(T, 0);
    ans = cnt1[T] * (cnt1[T] - 1) % md;
    Dfs2(S, 0);
    printf("%lld\n", ans);
    return 0;
}