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

#include<bits/stdc++.h>

using namespace std;

#define allof(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5 + 5;
const ll mod = 1e9 + 7;

int N, M;
int S, T;

ll best[2][MAXN];
vector<pll> adj[MAXN];
vector<int> pth[2][MAXN];

void dijkstra(int tp){
    memset(best[tp], 0x3f, sizeof best[tp]);
    best[tp][tp == 0 ? S : T] = 0;

    priority_queue<pll, vector<pll>, greater<pll>> pq;
    pq.push({0, tp == 0 ? S : T});
    while(!pq.empty()){
        pll top = pq.top();
        pq.pop();

        for(pll p : adj[top.second]){
            if(top.first + p.second < best[tp][p.first]){
                best[tp][p.first] = top.first + p.second;
                pq.push({best[tp][p.first], p.first});
            }
        }
    }
}

int incoming[2][MAXN];
ll pcnt[2][MAXN];

void count_path(int tp){
    pcnt[tp][tp == 0 ? S : T] = 1;
    queue<int> q;
    q.push(tp == 0 ? S : T);
    while(!q.empty()){
        int fr = q.front();
        q.pop();
        for(int i : pth[tp][fr]){
            pcnt[tp][i] += pcnt[tp][fr];
            pcnt[tp][i] %= mod;

            incoming[tp][i]--;
            if(!incoming[tp][i]){
                q.push(i);
            }
        }
    }
}

int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);

    cin >> N >> M;
    cin >> S >> T;
    for(int a = 0; a < M; a++){
        int u, v, c;
        cin >> u >> v >> c;
        adj[u].push_back(pll(v, c));
        adj[v].push_back(pll(u, c));
    }

    dijkstra(0);
    dijkstra(1);

    for(int a = 1; a <= N; a++){
        if(best[0][a] + best[1][a] != best[0][T]){
            continue;
        }
        for(pll p : adj[a]){
            if(best[0][a] + p.second == best[0][p.first] && best[0][p.first] + best[1][p.first] == best[0][T]){
                pth[0][a].push_back(p.first);

                incoming[0][p.first]++;
            }
            if(best[1][a] + p.second == best[1][p.first] && best[0][p.first] + best[1][p.first] == best[0][T]){
                pth[1][a].push_back(p.first);

                incoming[1][p.first]++;
            }
        }
    }
    count_path(0);
    count_path(1);

    ll ans = pcnt[0][T] * pcnt[1][S] % mod;
    //cout << "Initially " << ans << endl;
    for(int a = 1; a <= N; a++){
        //cout << "at " << a << endl;
        if(best[0][a] == best[1][a]){
            ll val = pcnt[0][a] * pcnt[1][a] % mod;
            ans -= val * val;
            ans %= mod;
        }
        for(int i : pth[0][a]){
            //cout << "going to " << i << endl;
            if(best[0][a] * 2 < best[0][T] && best[0][T] < best[0][i] * 2){
                //cout << "B -= " << pcnt[1][i] << " * " << pcnt[0][a] << endl;
                ll val = pcnt[0][a] * pcnt[1][i] % mod;
                ans -= val * val % mod;
                ans %= mod;
            }
        }
        //cout << endl;
    }
    ans %= mod;
    if(ans < 0){
        ans += mod;
    }
    cout << ans % mod;
}