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: Time Limit Exceeded)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
ll MOD=1e+9+7;
int N,M;
vpint adj[100000];
ll d[100000];
int color[100000];
vint p[100000],to[100000];
bool keirojo[100000]={0};
void dijkstra(int s){
	priority_queue<pint> PQ;
	rep(i,N){
		d[i]=INFl;
		color[i]=0;
	}
	d[s]=0;
	color[s]=1;
	p[s].push_back(-1);
	PQ.push(mp(0,s));
	while(!PQ.empty()){
		pint f=PQ.top();
		PQ.pop();
		int u=f.second;
		color[u]=2;
		if(d[u]< -f.first) continue;
		rep(j,adj[u].size()){
			int v=adj[u][j].first;
			if(color[v]==2) continue;
			if(d[v]>d[u]+adj[u][j].second){
				d[v]=d[u]+adj[u][j].second;
				PQ.push(mp(-d[v],v));
				color[v]=1;
				p[v].clear();
				p[v].push_back(u);
			}
			else if(d[v]==d[u]+adj[u][j].second){
				//PQ.push(mp(-d[v],v));
				color[v]=1;
				p[v].push_back(u);
			}
		}
	}
}
ll mind;
ll ikipattern[100000]={0};
ll gyakupattern[100000]={0};
map<int,int> tenchohuku;
map<pint,int> henchohuku;
map<pint,int> tochohuku;
vint tendeau;
vpint hendeau;
void gyaku(int v,int s){
	keirojo[v]=true;
	if(v==s){
		return;
	}
	if(2*d[v]==mind&&tenchohuku[v]==0){
		tenchohuku[v]=1;
		tendeau.push_back(v);
	}
	rep(i,p[v].size()){
		int nextv=p[v][i];
		if(tochohuku[mp(nextv,v)]==0){
			to[nextv].push_back(v);
			tochohuku[mp(nextv,v)]=1;
		}
		if(2*d[nextv]<mind&&2*d[v]>mind&&henchohuku[mp(nextv,v)]==0){
			hendeau.push_back(mp(nextv,v));
			henchohuku[mp(nextv,v)]=1;
		}
		gyaku(nextv,s);
	}
}
vector< pair<ll,int> >dv;
void tooricalc(){
	rep(i,dv.size()){
		int v=dv[i].second;
		if(i==0) ikipattern[v]=1;
		else{
			ikipattern[v]=0;
			rep(j,p[v].size()){
				ikipattern[v]+=ikipattern[p[v][j]];
			}
		}
	}
	for(int i=dv.size()-1;i>=0;i--){
		int v=dv[i].second;
		if(i==dv.size()-1) gyakupattern[v]=1;
		else{
			gyakupattern[v]=0;
			rep(j,to[v].size()){
				gyakupattern[v]+=gyakupattern[to[v][j]];
			}
		}
	}
}
int main(){
	int s,t;
	scan(N);scan(M);scan(s);scan(t);
	s--; t--;
	rep(i,M){
		int u,v,d;
		scan(u);scan(v);scan(d);
		u--; v--;
		adj[u].push_back(mp(v,d));
		adj[v].push_back(mp(u,d));
	}
	dijkstra(s);
	mind=d[t];
	gyaku(t,s);
	rep(i,N){
		if(keirojo[i]&&d[i]<INFl) dv.push_back(mp(d[i],i));
	}
	sort(dv.begin(),dv.end());
	tooricalc();
	/*rep(i,N){
		printf("i:%d  p: ",i+1);
		rep(j,p[i].size()){
			printf("%d ",p[i][j]+1);
		}
		kaigyo;
	}
	rep(i,N){
		printf("i:%d  to: ",i+1);
		rep(j,to[i].size()){
			printf("%d ",to[i][j]+1);
		}
		kaigyo;
	}
	rep(i,N){
		printf("i:%d iki:%lld gyaku:%lld\n",i+1,ikipattern[i],gyakupattern[i]);
	}*/
	ll toori=(gyakupattern[s]%MOD)*(gyakupattern[s]%MOD)%MOD;
	ll tensum=0;
	ll hensum=0;
	rep(i,tendeau.size()){
		tensum+=((ikipattern[tendeau[i]]%MOD)*(ikipattern[tendeau[i]]%MOD)%MOD)*((gyakupattern[tendeau[i]]%MOD)*(gyakupattern[tendeau[i]]%MOD)%MOD);
		tensum%=MOD;
	}
	rep(i,hendeau.size()){
		hensum+=(ikipattern[hendeau[i].first]%MOD)*(ikipattern[hendeau[i].first]%MOD)*(gyakupattern[hendeau[i].second]%MOD)*(gyakupattern[hendeau[i].second]%MOD);
		hensum%=MOD;
	}
	//printf("sosu:%lld tensum:%lld hensum:%lld\n",toori,tensum,hensum);
	toori-=(tensum+hensum)%MOD;
	if(toori<0) toori+=MOD;
	prin(toori);
	return 0;
}