from heapq import heappop, heappush

MOD = 10 ** 9 + 7



N, M = list(map(int, input().split()))

S, T = [int(x) - 1 for x in input().split()]



adjList = [[] for i in range(N)]

for i in range(M):

    U, V, D = list(map(int, input().split()))

    adjList[U - 1].append((V - 1, D))

    adjList[V - 1].append((U - 1, D))



# 頂点Sから各頂点への最短路の個数

numRouteS = [0] * N

numRouteS[S] = 1



cost = [float('inf')] * N

cost[S] = 0

prev = [None] * N

vs = []



pq = []

heappush(pq, (0, S))

while pq:

    c, vNow = heappop(pq)

    if c > cost[vNow]: continue

    if c > cost[T]: break

    vs += [vNow]



    for v2, wt in adjList[vNow]:

        c2 = c + wt

        if c2 < cost[v2]:

            cost[v2] = c2

            prev[v2] = [vNow]

            numRouteS[v2] = numRouteS[vNow]

            heappush(pq, (c2, v2))

        elif c2 == cost[v2]:

            prev[v2] += [vNow]

            numRouteS[v2] = (numRouteS[v2] + numRouteS[vNow]) % MOD



# 頂点Tから各頂点への最短路の個数

numRouteT = [0] * N

numRouteT[T] = 1

cST = cost[T]

for iV, v in enumerate(reversed(vs)):

    if cost[v] * 2 < cST:

        iVLim = len(vs) - iV

        break

    for v0 in prev[v]:

        numRouteT[v0] = (numRouteT[v0] + numRouteT[v]) % MOD



# 二人の最短路の選び方の組から、途中で出会うものを引く

ans = (numRouteS[T] ** 2) % MOD



for v in vs[:iVLim]:

    x = (numRouteS[v] ** 2) % MOD

    for v2, wt in adjList[v]:

        if (cost[v2] * 2 > cST) and cost[v] + wt == cost[v2]:

            y = (numRouteT[v2] ** 2) % MOD

            ans = (ans - x * y) % MOD



for v in vs[iVLim:]:

    if cost[v] * 2 > cST: break

    x = (numRouteS[v] ** 2) % MOD

    y = (numRouteT[v] ** 2) % MOD

    ans = (ans - x * y) % MOD



print(ans)

from heapq import heappush, heappop

import sys

input = sys.stdin.readline



INF = float('inf')

MOD = 10**9 + 7



def Dijkstra(adjList, vSt):

    numV = len(adjList)

    numUsed = 0

    costs = [INF] * numV

    costs[vSt] = 0

    nums = [0] * numV

    nums[vSt] = 1

    PQ = []

    heappush(PQ, (0, vSt))

    while PQ:

        cNow, vNow = heappop(PQ)

        if cNow > costs[vNow]: continue

        numUsed += 1

        if numUsed == numV: break

        for v2, wt in adjList[vNow]:

            c2 = cNow + wt

            if c2 < costs[v2]:

                costs[v2] = c2

                nums[v2] = nums[vNow]

                heappush(PQ, (c2, v2))

            elif c2 == costs[v2]:

                nums[v2] = (nums[v2]+nums[vNow]) % MOD

    return (costs, nums)



def solve():

    N, M = list(map(int, input().split()))

    S, T = list(map(int, input().split()))

    S, T = S-1, T-1

    edges = [tuple(map(int, input().split())) for _ in range(M)]

    adjL = [[] for _ in range(N)]

    for u, v, d in edges:

        adjL[u-1].append((v-1, d))

        adjL[v-1].append((u-1, d))



    costSs, numSs = Dijkstra(adjL, S)

    costTs, numTs = Dijkstra(adjL, T)

    minCost = costSs[T]



    ans = (numSs[T]**2) % MOD

    for v in range(N):

        if costSs[v] == costTs[v] == minCost/2:

            k = (numSs[v]*numTs[v]) % MOD

            ans -= (k**2) % MOD

            ans %= MOD

    for u, v, d in edges:

        u, v = u-1, v-1

        if costSs[u] > costSs[v]:

            u, v = v, u

        if costSs[u] < minCost/2 and costTs[v] < minCost/2 and costSs[u]+d+costTs[v] == minCost:

            k = (numSs[u]*numTs[v]) % MOD

            ans -= (k**2) % MOD

            ans %= MOD



    print(ans)



solve()

from heapq import heappop, heappush

MOD = 10**9 + 7
N, M = list(map(int, input().split()))
S, T = [int(x) - 1 for x in input().split()]
adjList = [[] for i in range(N)]
for i in range(M):
    U, V, D = list(map(int, input().split()))
    adjList[U - 1].append((V - 1, D))
    adjList[V - 1].append((U - 1, D))
# 頂点Sから各頂点への最短路の個数
numRouteS = [0] * N
numRouteS[S] = 1
cost = [float("inf")] * N
cost[S] = 0
prev = [None] * N
vs = []
pq = []
heappush(pq, (0, S))
while pq:
    c, vNow = heappop(pq)
    if c > cost[vNow]:
        continue
    if c > cost[T]:
        break
    vs += [vNow]
    for v2, wt in adjList[vNow]:
        c2 = c + wt
        if c2 < cost[v2]:
            cost[v2] = c2
            prev[v2] = [vNow]
            numRouteS[v2] = numRouteS[vNow]
            heappush(pq, (c2, v2))
        elif c2 == cost[v2]:
            prev[v2] += [vNow]
            numRouteS[v2] = (numRouteS[v2] + numRouteS[vNow]) % MOD
# 頂点Tから各頂点への最短路の個数
numRouteT = [0] * N
numRouteT[T] = 1
cST = cost[T]
for iV, v in enumerate(reversed(vs)):
    if cost[v] * 2 < cST:
        iVLim = len(vs) - iV
        break
    for v0 in prev[v]:
        numRouteT[v0] = (numRouteT[v0] + numRouteT[v]) % MOD
# 二人の最短路の選び方の組から、途中で出会うものを引く
ans = (numRouteS[T] ** 2) % MOD
for v in vs[:iVLim]:
    x = (numRouteS[v] ** 2) % MOD
    for v2, wt in adjList[v]:
        if (cost[v2] * 2 > cST) and cost[v] + wt == cost[v2]:
            y = (numRouteT[v2] ** 2) % MOD
            ans = (ans - x * y) % MOD
for v in vs[iVLim:]:
    if cost[v] * 2 > cST:
        break
    x = (numRouteS[v] ** 2) % MOD
    y = (numRouteT[v] ** 2) % MOD
    ans = (ans - x * y) % MOD
print(ans)

from heapq import heappush, heappop
import sys

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def Dijkstra(adjList, vSt):
    numV = len(adjList)
    numUsed = 0
    costs = [INF] * numV
    costs[vSt] = 0
    nums = [0] * numV
    nums[vSt] = 1
    PQ = []
    heappush(PQ, (0, vSt))
    while PQ:
        cNow, vNow = heappop(PQ)
        if cNow > costs[vNow]:
            continue
        numUsed += 1
        if numUsed == numV:
            break
        for v2, wt in adjList[vNow]:
            c2 = cNow + wt
            if c2 < costs[v2]:
                costs[v2] = c2
                nums[v2] = nums[vNow]
                heappush(PQ, (c2, v2))
            elif c2 == costs[v2]:
                nums[v2] = (nums[v2] + nums[vNow]) % MOD
    return (costs, nums)


def solve():
    N, M = list(map(int, input().split()))
    S, T = list(map(int, input().split()))
    S, T = S - 1, T - 1
    edges = [tuple(map(int, input().split())) for _ in range(M)]
    adjL = [[] for _ in range(N)]
    for u, v, d in edges:
        adjL[u - 1].append((v - 1, d))
        adjL[v - 1].append((u - 1, d))
    costSs, numSs = Dijkstra(adjL, S)
    costTs, numTs = Dijkstra(adjL, T)
    minCost = costSs[T]
    ans = (numSs[T] ** 2) % MOD
    for v in range(N):
        if costSs[v] == costTs[v] == minCost / 2:
            k = (numSs[v] * numTs[v]) % MOD
            ans -= (k**2) % MOD
            ans %= MOD
    for u, v, d in edges:
        u, v = u - 1, v - 1
        if costSs[u] > costSs[v]:
            u, v = v, u
        if (
            costSs[u] < minCost / 2
            and costTs[v] < minCost / 2
            and costSs[u] + d + costTs[v] == minCost
        ):
            k = (numSs[u] * numTs[v]) % MOD
            ans -= (k**2) % MOD
            ans %= MOD
    print(ans)


solve()

-from heapq import heappop, heappush +from heapq import heappush, heappop +import sys +input = sys.stdin.readline +INF = float("inf") -N, M = list(map(int, input().split())) -S, T = [int(x) - 1 for x in input().split()] -adjList = [[] for i in range(N)] -for i in range(M): -    U, V, D = list(map(int, input().split())) -    adjList[U - 1].append((V - 1, D)) -    adjList[V - 1].append((U - 1, D)) -# 頂点Sから各頂点への最短路の個数 -numRouteS = [0] * N -numRouteS[S] = 1 -cost = [float("inf")] * N -cost[S] = 0 -prev = [None] * N -vs = [] -pq = [] -heappush(pq, (0, S)) -while pq: -    c, vNow = heappop(pq) -    if c > cost[vNow]: -        continue -    if c > cost[T]: -        break -    vs += [vNow] -    for v2, wt in adjList[vNow]: -        c2 = c + wt -        if c2 < cost[v2]: -            cost[v2] = c2 -            prev[v2] = [vNow] -            numRouteS[v2] = numRouteS[vNow] -            heappush(pq, (c2, v2)) -        elif c2 == cost[v2]: -            prev[v2] += [vNow] -            numRouteS[v2] = (numRouteS[v2] + numRouteS[vNow]) % MOD -# 頂点Tから各頂点への最短路の個数 -numRouteT = [0] * N -numRouteT[T] = 1 -cST = cost[T] -for iV, v in enumerate(reversed(vs)): -    if cost[v] * 2 < cST: -        iVLim = len(vs) - iV -        break -    for v0 in prev[v]: -        numRouteT[v0] = (numRouteT[v0] + numRouteT[v]) % MOD -# 二人の最短路の選び方の組から、途中で出会うものを引く -ans = (numRouteS[T] ** 2) % MOD -for v in vs[:iVLim]: -    x = (numRouteS[v] ** 2) % MOD -    for v2, wt in adjList[v]: -        if (cost[v2] * 2 > cST) and cost[v] + wt == cost[v2]: -            y = (numRouteT[v2] ** 2) % MOD -            ans = (ans - x * y) % MOD -for v in vs[iVLim:]: -    if cost[v] * 2 > cST: -        break -    x = (numRouteS[v] ** 2) % MOD -    y = (numRouteT[v] ** 2) % MOD -    ans = (ans - x * y) % MOD -print(ans) + + +def Dijkstra(adjList, vSt): +    numV = len(adjList) +    numUsed = 0 +    costs = [INF] * numV +    costs[vSt] = 0 +    nums = [0] * numV +    nums[vSt] = 1 +    PQ = [] +    heappush(PQ, (0, vSt)) +    while PQ: +        cNow, vNow = heappop(PQ) +        if cNow > costs[vNow]: +            continue +        numUsed += 1 +        if numUsed == numV: +            break +        for v2, wt in adjList[vNow]: +            c2 = cNow + wt +            if c2 < costs[v2]: +                costs[v2] = c2 +                nums[v2] = nums[vNow] +                heappush(PQ, (c2, v2)) +            elif c2 == costs[v2]: +                nums[v2] = (nums[v2] + nums[vNow]) % MOD +    return (costs, nums) + + +def solve(): +    N, M = list(map(int, input().split())) +    S, T = list(map(int, input().split())) +    S, T = S - 1, T - 1 +    edges = [tuple(map(int, input().split())) for _ in range(M)] +    adjL = [[] for _ in range(N)] +    for u, v, d in edges: +        adjL[u - 1].append((v - 1, d)) +        adjL[v - 1].append((u - 1, d)) +    costSs, numSs = Dijkstra(adjL, S) +    costTs, numTs = Dijkstra(adjL, T) +    minCost = costSs[T] +    ans = (numSs[T] ** 2) % MOD +    for v in range(N): +        if costSs[v] == costTs[v] == minCost / 2: +            k = (numSs[v] * numTs[v]) % MOD +            ans -= (k**2) % MOD +            ans %= MOD +    for u, v, d in edges: +        u, v = u - 1, v - 1 +        if costSs[u] > costSs[v]: +            u, v = v, u +        if ( +            costSs[u] < minCost / 2 +            and costTs[v] < minCost / 2 +            and costSs[u] + d + costTs[v] == minCost +        ): +            k = (numSs[u] * numTs[v]) % MOD +            ans -= (k**2) % MOD +            ans %= MOD +    print(ans) + + +solve()