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 Perl (v5.18.2) (judge verdict: Wrong Answer)

$mod=1000000007;
@CC=();@UV=();
($N,$M) =split(" ",<STDIN>);
($S,$T) =split(" ",<STDIN>);
$S--;$T--;
for($i=$N-1;$i>=0;$i--) { @{$CC[$i]}=(); }
for($i=0;$i<$M;$i++) {
	$l=<STDIN>;
	$j=index($l," ");
	$U=int($l)-1;
	$V=int(substr($l,$j                ))-1;
	$D=int(substr($l,index($l," ",$j+1)))*2;
	
	push @UV,($U,$V);
	push @{$CC[$U]},($V,$D);
	push @{$CC[$V]},($U,$D);
}
for($i=$N-1;$i>=0;$i--) { $DS[$i]=-1;$DT[$i]=-1; }
@QS1=($S);@QS2=(0);$DS[$S]=0;
@QT1=($T);@QT2=(0);$DT[$T]=0;
@PK=();$PK[$N-1]=0;

while ($#QS1>=0 || $#QT1>=0) {
	#from S
	if ( $#QS1>=0 ){
		do {
			#binary heap deque >
			$p=$QS1[0];
			$pd=$QS2[0];
			$q=pop(@QS1);
			$d=pop(@QS2);
			if ($#QS1>=0) {
				$j=0;$i=1;
				while ( $i<=$#QS1 ) {
					$i=$i+1 if ($i<$#QS1 && ( $QS2[$i]>$QS2[$i+1] ));
					last if ( $QS2[$i] >= $d );
					$QS1[$j]=$QS1[$i];
					$QS2[$j]=$QS2[$i];
					$j=$i;
					$i=$j*2+1;
				}
				$QS1[$j]=$q;
				$QS2[$j]=$d;
			}
			#< binary heap deque
		} while ( $pd > $DS[$p] );
		next if ($DS[$p]<$pd);
		$a=$CC[$p];
		for($x=$#{$a}-1;$x>=0;$x-=2) {
			$q=${$a}[$x];
			next if ($PK[$q]!=0 );
			if ($DS[$q]<0 || $DS[$q]>$pd+${$a}[$x+1]) {
				$d=$DS[$q]=$pd+${$a}[$x+1];
				#binary heap enque >
				$j=$#QS1+1;
				while ( $j>0 ) {
					$i=int(($j-1)/2);
					last if ( $QS2[$i]<=$d );
					$QS1[$j]=$QS1[$i];
					$QS2[$j]=$QS2[$i];
					$j=$i;
				}
				$QS1[$j]=$q;
				$QS2[$j]=$d;
				#< binary heap enque
			}
		}
		last if ($PK[$p]!=0);
		$PK[$p]=1;
	}
	
	#from T
	#binary heap deque >
	if ( $#QT1>=0 ) {
		do {
			$r=$QT1[0];
			$rd=$QT2[0];
			$q=pop(@QT1);
			$d=pop(@QT2);
			if ($#QT1>=0) {
				$j=0;$i=1;
				while ( $i<=$#QT1 ) {
					$i=$i+1 if ($i<$#QT1 && ( $QT2[$i]>$QT2[$i+1] ));
					last if ( $QT2[$i] >= $d );
					$QT1[$j]=$QT1[$i];
					$QT2[$j]=$QT2[$i];
					$j=$i;
					$i=$j*2+1;
				}
				$QT1[$j]=$q;
				$QT2[$j]=$d;
			}
			#< binary heap deque
		} while ( $rd > $DT[$r] );
		$a=$CC[$r];
		for($x=$#{$a}-1;$x>=0;$x-=2) {
			$q=${$a}[$x];
			next if ($PK[$q]!=0 );
			if ($DT[$q]<0 || $DT[$q]>$rd+${$a}[$x+1]) {
				$d=$DT[$q]=$rd+${$a}[$x+1];
				#binary heap enque >
				$j=$#QT1+1;
				while ( $j>0 ) {
					$i=int(($j-1)/2);
					last if ( $QT2[$i]<=$d );
					$QT1[$j]=$QT1[$i];
					$QT2[$j]=$QT2[$i];
					$j=$i;
				}
				$QT1[$j]=$q;
				$QT2[$j]=$d;
				#< binary heap enque
			}
		}
		last if ($PK[$r]!=0);
		$PK[$r]=2;
	}
}
$pd2=$pd;$rd2=$rd;

while ($#QS1>=0) {
	#from S
	#binary heap deque >
	$p=$QS1[0];
	$pd=$QS2[0];
	last if ($pd>$pd2);
	$q=pop(@QS1);
	$d=pop(@QS2);
	if ($#QS1>=0) {
		$j=0;$i=1;
		while ( $i<=$#QS1 ) {
			$i=$i+1 if ($i<$#QS1 && ( $QS2[$i]>$QS2[$i+1] ));
			last if ( $QS2[$i] >= $d );
			$QS1[$j]=$QS1[$i];
			$QS2[$j]=$QS2[$i];
			$j=$i;
			$i=$j*2+1;
		}
		$QS1[$j]=$q;
		$QS2[$j]=$d;
	}
	#< binary heap deque
	$a=$CC[$p];
	for($x=$#{$a}-1;$x>=0;$x-=2) {
		$q=${$a}[$x];
		next if ($PK[$q]!=0 );
		if ($DS[$q]<0 || $DS[$q]>$pd+${$a}[$x+1]) {
			$DS[$q]=$pd+${$a}[$x+1];
		}
	}
	$PK[$p]=1;
}
while($#QT1>=0){
	#from T
	#binary heap deque >
	$r=$QT1[0];
	$rd=$QT2[0];
	last if ($rd>$rd2);
	$q=pop(@QT1);
	$d=pop(@QT2);
	if ($#QT1>=0) {
		$j=0;$i=1;
		while ( $i<=$#QT1 ) {
			$i=$i+1 if ($i<$#QT1 && ( $QT2[$i]>$QT2[$i+1] ));
			last if ( $QT2[$i] >= $d );
			$QT1[$j]=$QT1[$i];
			$QT2[$j]=$QT2[$i];
			$j=$i;
			$i=$j*2+1;
		}
		$QT1[$j]=$q;
		$QT2[$j]=$d;
	}
	#< binary heap deque
	$a=$CC[$r];
	for($x=$#{$a}-1;$x>=0;$x-=2) {
		$q=${$a}[$x];
		next if ($PK[$q]!=0 );
		if ($DT[$q]<0 || $DT[$q]>$rd+${$a}[$x+1]) {
			$DT[$q]=$rd+${$a}[$x+1];
		}
	}
	$PK[$r]=2;
}
$da=-1;$dq=0;
for($i=$N-1;$i>=0;$i--) {
	next if ($PK[$i]==0 || $DS[$i]<0 || $DT[$i]<0 );
	if ( $da<0 || $DS[$i]+$DT[$i] < $da ) {
		$da = $DS[$i]+$DT[$i];
		$dq = $DT[$i];
	}
}

@DD=();@O=();
for($i=$N-1;$i>=0;$i--) {
	next if ($PK[$i]==0);
	if ($DS[$i]>=0) {
		if ($DT[$i]>=0) {
			if ($DS[$i]+$DT[$i]==$da) {
				$DD[$i]=$DS[$i];
				push @O,$i;
			}
		} else {
			$DD[$i]=$DS[$i];
			push @O,$i;
		}
	} else {
		$DD[$i]=$da-$DT[$i];
		push @O,$i;
	}
}
$coltime=$DD[$T]/2;
@O=sort { $DD[$a]<=>$DD[$b]; } @O;

@FS=();$FS[$S]=1;
for ($o=0;$o<=$#O;$o++) {
	next if ( $FS[$O[$o]]==0 );
	$p=$O[$o];$dd=$DD[$p];$a=$CC[$p];
	for($x=$#{$a}-1;$x>=0;$x-=2) {
		$q=${$a}[$x];
		next if ($DD[$q]!=$dd+${$a}[$x+1]);
		$FS[$q]=($FS[$q]+$FS[$p])%$mod;
	}
}
@O = grep { $FS[$_]!=0; } @O;
@FT=();$FT[$T]=1;
for ($o=$#O;$o>=0;$o--) {
	next if ( $FT[$O[$o]]==0 );
	$p=$O[$o];$dd=$DD[$p];$a=$CC[$p];
	for($x=$#{$a}-1;$x>=0;$x-=2) {
		$q=${$a}[$x];
		next if ($FS[$q]==0);
		next if ($DD[$q]+${$a}[$x+1]!=$dd);
		$FT[$q]=($FT[$q]+$FT[$p])%$mod;
	}
}

@B=();
for ($o=0;$o<$#O && $DD[$O[$o]]< $coltime;$o++) {}
for (    ;$o<$#O && $DD[$O[$o]]==$coltime;$o++) {
	$p=$O[$o];
	next if ($FT[$p]==0 || $FS[$p]==0);
	push @B,($FT[$p]*$FS[$p])%$mod;
}

for ($i=$#UV-1;$i>=0;$i-=2) {
	$U=$UV[$i];
	next if($FS[$U]==0);
	$V=$UV[$i+1];
	next if($FS[$V]==0);
	
	if($DD[$U]>$coltime) {
		if ($DD[$V]<$coltime) {
			push @B,($FS[$V]*$FT[$U])%$mod;
		}
	} elsif($DD[$U]<$coltime && $DD[$V]>$coltime) {
		push @B,($FS[$U]*$FT[$V])%$mod;
	}
}

$t=0;
for($i=0;$i<$#B;$i++){
for($j=$i+1;$j<=$#B;$j++){
	$t=($t+$B[$i]*$B[$j]*2)%$mod;
}}
print $t;