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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M;
int S,T;

vector<tuple<int,int,ll>> edges;
vector<pair<int,ll>> g[100010];
ll dist[2][100010];
const ll INF=1e18;

ll dp[2][100010];

void dijkstra(int c){
    for(int i=0;i<=N;i++) dist[c][i]=INF;
    priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> PQ;
    if(c==0){
        dist[c][S]=0;
        PQ.push(mkp(0,S));
    }else{
        dist[c][T]=0;
        PQ.push(mkp(0,T));
    }

    while(!PQ.empty()){
        int now;
        ll cost;
        tie(cost,now)=PQ.top();
        PQ.pop();

        if(dist[c][now]<cost) continue;
        for(int i=0;i<g[now].size();i++){
            int nex;
            ll neco;
            tie(nex,neco)=g[now][i];
            if(dist[c][nex]>dist[c][now]+neco){
                dist[c][nex]=dist[c][now]+neco;
                PQ.push(mkp(dist[c][nex],nex));
            }
        }
    }
}

void add(ll &a,ll b){
    a=(a+b)%MOD;
}

void setRoutes(int c){
    priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> PQ;
    vector<int> used(N+1,0);
    if(c==0){
        dp[c][S]=1;
        PQ.push(mkp(0,S));
        used[S]=1;
    }else{
        dp[c][T]=1;
        PQ.push(mkp(0,T));
        used[T]=1;
    }

    while(!PQ.empty()){
        int now;
        ll cost;
        tie(cost,now)=PQ.top();
        PQ.pop();

        for(int i=0;i<g[now].size();i++){
            int nex;
            ll neco;
            tie(nex,neco)=g[now][i];
            int tar=T;
            if(c==1) tar=S;
            if(dist[c][now]+neco+dist[1-c][nex]==dist[c][tar]){
                add(dp[c][nex],dp[c][now]);
                if(used[nex]==0){
                    used[nex]=1;
                    PQ.push(mkp(dist[c][nex],nex));
                }
            }
        }
    }
}

void mul(ll &a,ll b){
    a=a*b%MOD;
}

int main(){
  cin>>N>>M;
  cin>>S>>T;
  rep(i,M){
      ll a,b,c;
      cin>>a>>b>>c;
      edges.push_back(mkt(a,b,c));
      g[a].push_back(mkp(b,c));
      g[b].push_back(mkp(a,c));
  }

  dijkstra(0);
  dijkstra(1);

  setRoutes(0);
  setRoutes(1);

  ll ans=dp[0][T];
  mul(ans,dp[1][S]);
  for(int i=1;i<=N;i++){
      if(dist[0][i]+dist[1][i]==dist[0][T]&&dist[0][i]==dist[1][i]){
          ll res=dp[0][i];
          mul(res,dp[1][i]);
          mul(res,res);
          add(ans,MOD-res);
      }
  }

  for(int i=0;i<M;i++){
      ll a,b,c;
      a=get<0>(edges[i]);
      b=get<1>(edges[i]);
      c=get<2>(edges[i]);

      if(dist[0][a]>dist[0][b]) swap(a,b);
      if(dist[0][a]+c+dist[1][b]==dist[0][T]){
          if(abs(dist[0][a]-dist[1][b])<c){
              ll res=dp[0][a];
              mul(res,dp[1][b]);
              mul(res,res);
              add(ans,MOD-res);
          }
      }
  }
  
  add(ans,MOD);
 
  cout<<ans<<endl;


  return 0;
}