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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "container/heap"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatVec(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}
func Abs(x int) int {
  if x < 0 { x = -x }
  return x
}

type Item struct {
  Priority int
  Entry interface{}
}
type Heap []*Item
func (H Heap) Len() int { return len(H) }
func (H Heap) Less(i, j int) bool { return H[i].Priority < H[j].Priority }
func (H Heap) Swap(i, j int) { H[i], H[j] = H[j], H[i] }
func (H *Heap) Pop() interface{} {
  Old := *H
  n := len(Old)
  item := Old[n - 1]
  *H = Old[0:n - 1]
  return item
}
func (H *Heap) Push(x interface{}) {
  item := x.(*Item)
  *H = append(*H, item)
}
type PriorityQueue struct {
  Comparator func(e interface{}) int
  Items Heap
}
func (PQ *PriorityQueue) Push(e interface{}) {
  item := Item{PQ.Comparator(e), e}
  heap.Push(&PQ.Items, &item)
}
func (PQ *PriorityQueue) Pop() interface{} {
  return heap.Pop(&PQ.Items).(*Item).Entry
}
func InitPQ(F func(interface{}) int) PriorityQueue {
  var H Heap
  return PriorityQueue{F, H}
}

const p = 1000000007

func Cut(H *[][]int, D *[]int, u, S, T int) {
  if u == S || u == T { return }
  if (*H)[u][0] != 1 { return }
  (*H)[u][0] = 0
  for _, v := range (*H)[u][1:] {
    (*H)[v][0]--
    if (*H)[v][0] != 1 { continue }
    Cut(H, D, v, S, T)
  }
}

func Solve() {
  var N, M int
  NextInt(&N, &M)
  G := make([][][]int, N)
  for i := range G {
    G[i] = make([][]int, 0)
  }
  var S, T int
  NextInt(&S, &T)
  S--
  T--
  E := make([][]int, M)
  var u, v, d int
  for i := range E {
    NextInt(&u, &v, &d)
    u--
    v--
    G[u] = append(G[u], []int{v, d})
    G[v] = append(G[v], []int{u, d})
    E[i] = []int{u, v, d}
  }
  D := make([]int, N)
  for i := range D {
    D[i] = -1
  }
  F := func(e interface{}) int { return e.([]int)[1] }
  PQ := InitPQ(F)
  PQ.Push([]int{S, 0})
  for 0 < len(PQ.Items) {
    pq := PQ.Pop().([]int)
    q, dist := pq[0], pq[1]
    if 0 <= D[q] { continue }
    D[q] = dist
    for _, Adj := range G[q] {
      adj, d := Adj[0], Adj[1]
      if 0 <= D[adj] { continue }
      PQ.Push([]int{adj, D[q] + d})
    }
  }
  H := make([][]int, N)
  for i := range H {
    H[i] = make([]int, 0)
    H[i] = append(H[i], 0)
  }
  for _, e := range E {
    u, v, d := e[0], e[1], e[2]
    if Abs(D[u] - D[v]) != d { continue }
    if (u != T && D[T] <= D[u]) || (v != T && D[T] <= D[v]) { continue }
    H[u][0]++
    H[u] = append(H[u], v)
    H[v][0]++
    H[v] = append(H[v], u)
  }
  for u := range H {
    Cut(&H, &D, u, S, T)
  }
  cnt := 0
  for i, d := range D {
    if 0 < H[i][0] && d * 2 == D[T] {
      cnt++
    }
  }
  for _, e := range E {
    u, v := e[0], e[1]
    if H[u][0] <= 0 || H[v][0] <= 0 { continue }
    if 2 * MinInt(D[u], D[v]) < D[T] && D[T] < 2 * MaxInt(D[u], D[v]) {
      cnt++
    }
  }
  Write(cnt * (cnt - 1) % p)
  return
}

func main() {
  Solve()
  Output()
}