# 参考: https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1

from typing import List, Tuple
from heapq import heappush, heappop


import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

# 多次元配列
# dp = [[0] * 3 for _ in range(5)


class Edge:
    def __init__(self, to: int, cost: int):
        self._to = to
        self._cost = cost

    @property
    def to(self):
        return self._to

    @property
    def cost(self):
        return self._cost


class Edges(list):
    def __init__(self):
        list.__init__(self)


INF = 1 << 28
MOD = int(1e9) + 7


def dijkstra(graph: List[Edges], s: int) -> Tuple[List[int], List[int]]:
    n = len(graph)
    dist = [INF for _ in range(n)]
    cnts = [0 for _ in range(n)]

    pq: List[Tuple[int, int]] = []
    heappush(pq, (0, s))
    dist[s] = 0
    cnts[s] = 1

    while pq:
        (cost, cur) = heappop(pq)

        if dist[cur] != cost:
            continue

        for next in graph[cur]:
            if dist[next.to] > cost + next.cost:
                heappush(pq, (cost + next.cost, next.to))
                dist[next.to] = cost + next.cost
                cnts[next.to] = cnts[cur]
            elif dist[next.to] == cost + next.cost:
                cnts[next.to] = (cnts[next.to] + cnts[cur]) % MOD

    return dist, cnts


# 入力を受け取る
n, m = map(int, input().split())

s, t = map(lambda i: int(i) - 1, input().split())

g = [Edges() for _ in range(n)]

for _ in range(m):
    u, v, d = map(int, input().split())
    g[u - 1].append(Edge(v - 1, d))
    g[v - 1].append(Edge(u - 1, d))

dst1, cnt1 = dijkstra(g, s)
dst2, cnt2 = dijkstra(g, t)

ret = cnt1[t] * cnt1[t] % MOD

for i in range(n):
    if 2 * dst1[i] == dst1[t]:
        sub = cnt1[i] * cnt1[i] % MOD
        sub = sub * cnt2[i] % MOD * cnt2[i] % MOD
        ret = (ret + MOD - sub) % MOD

sum_sub = 0
for u in range(n):
    for e in g[u]:
        v, d = e.to, e.cost
        if 2 * dst1[u] < dst1[t] < 2 * dst1[v] and dst1[u] + d == dst1[v] and dst2[v] + d == dst2[u]:
            sub = cnt1[u] * cnt1[u] % MOD
            sub = sub * cnt2[v] % MOD * cnt2[v] % MOD
            sum_sub = (sum_sub + sub) % MOD

ret = (ret + MOD - sum_sub) % MOD
print(ret)