Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
graph = [[] for _ in range(n)]
for i in range(m):
    u, v, d = map(int, input().split())
    graph[u - 1] += [(v - 1, d)]
    graph[v - 1] += [(u - 1, d)]

vst = s - 1
v_g = t - 1
checked = [0] * n
dist = [INF] * n
route_cnt = [0] * n
route_cnt[vst] = 1
dist[vst] = 0
for _ in range(n):
    min_dist = INF
    for i in range(n):
        if checked[i] == 0 and dist[i] < min_dist:
            target_v = i
            min_dist = dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v]:
            continue
        if dist[target_v] + cost < dist[to_v]:
            dist[to_v] = dist[target_v] + cost
            route_cnt[to_v] = route_cnt[target_v]
        elif dist[target_v] + cost == dist[to_v]:
            route_cnt[to_v] = (route_cnt[to_v] + route_cnt[target_v]) % mod
    if target_v == v_g:
        break

total_dist = dist[v_g]
half_dist = dist[v_g] / 2
enc_li = []
checked = [0] * n
reverse_dist = [INF] * n
reverse_route_cnt = [0] * n
reverse_route_cnt[v_g] = 1
reverse_dist[v_g] = 0
for _ in range(n):
    min_dist = INF
    for i in range(n):
        if checked[i] == 0 and reverse_dist[i] < min_dist:
            target_v = i
            min_dist = reverse_dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v] or reverse_dist[target_v] + cost != total_dist - dist[to_v]:
            continue
        if reverse_dist[target_v] + cost < reverse_dist[to_v]:
            reverse_dist[to_v] = reverse_dist[target_v] + cost
            reverse_route_cnt[to_v] = reverse_route_cnt[target_v]
        elif reverse_dist[target_v] + cost == reverse_dist[to_v]:
            reverse_route_cnt[to_v] = (reverse_route_cnt[target_v] + reverse_route_cnt[to_v]) % mod
        if reverse_dist[target_v] <= half_dist < reverse_dist[to_v]:
            enc_li += [(to_v, target_v)]

ret = 0
for from_v, to_v, in enc_li:
    ret += route_cnt[from_v] * reverse_route_cnt[to_v] % mod
    ret %= mod

print((route_cnt[v_g] ** 2 % mod - ret) % mod)


Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
graph = [[] for _ in range(n)]
for i in range(m):
    u, v, d = map(int, input().split())
    graph[u - 1] += [(v - 1, d)]
    graph[v - 1] += [(u - 1, d)]

vst = s - 1
v_g = t - 1
checked = [0] * n
dist = [float('inf')] * n
route_cnt = [0] * n
route_cnt[vst] = 1
dist[vst] = 0
for _ in range(n):
    min_dist = INF
    for i in range(n):
        if checked[i] == 0 and dist[i] < min_dist:
            target_v = i
            min_dist = dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v]:
            continue
        if dist[target_v] + cost < dist[to_v]:
            dist[to_v] = dist[target_v] + cost
            route_cnt[to_v] = route_cnt[target_v]
        elif dist[target_v] + cost == dist[to_v]:
            route_cnt[to_v] = (route_cnt[to_v] + route_cnt[target_v]) % mod
    if target_v == v_g:
        break

total_dist = dist[v_g]
half_dist = dist[v_g] / 2
enc_li = []
checked = [0] * n
reverse_dist = [INF] * n
reverse_route_cnt = [0] * n
reverse_route_cnt[v_g] = 1
reverse_dist[v_g] = 0
for _ in range(n):
    min_dist = INF
    for i in range(n):
        if checked[i] == 0 and reverse_dist[i] < min_dist:
            target_v = i
            min_dist = reverse_dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v] or reverse_dist[target_v] + cost != total_dist - dist[to_v]:
            continue
        if reverse_dist[target_v] + cost < reverse_dist[to_v]:
            reverse_dist[to_v] = reverse_dist[target_v] + cost
            reverse_route_cnt[to_v] = reverse_route_cnt[target_v]
        elif reverse_dist[target_v] + cost == reverse_dist[to_v]:
            reverse_route_cnt[to_v] = (reverse_route_cnt[target_v] + reverse_route_cnt[to_v]) % mod
        if reverse_dist[target_v] <= half_dist < reverse_dist[to_v]:
            enc_li += [(to_v, target_v)]

ret = 0
for from_v, to_v, in enc_li:
    ret += route_cnt[from_v] * reverse_route_cnt[to_v] % mod
    ret %= mod

print((route_cnt[v_g] ** 2 % mod - ret) % mod)


Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10 ** 9 + 7


n, m = map(int, input().split())
s, t = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(m):
    u, v, d = map(int, input().split())
    graph[u - 1] += [(v - 1, d)]
    graph[v - 1] += [(u - 1, d)]

vst = s - 1
v_g = t - 1
checked = [0] * n
dist = [float('inf')] * n
route_cnt = [0] * n
route_cnt[vst] = 1
dist[vst] = 0
for _ in range(n):
    min_dist = float('inf')
    for i in range(n):
        if checked[i] == 0 and dist[i] < min_dist:
            target_v = i
            min_dist = dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v]:
            continue
        if dist[target_v] + cost < dist[to_v]:
            dist[to_v] = dist[target_v] + cost
            route_cnt[to_v] = route_cnt[target_v]
        elif dist[target_v] + cost == dist[to_v]:
            route_cnt[to_v] = (route_cnt[to_v] + route_cnt[target_v]) % mod
    if target_v == v_g:
        break

total_dist = dist[v_g]
half_dist = dist[v_g] / 2
enc_li = []
checked = [0] * n
reverse_dist = [float('inf')] * n
reverse_route_cnt = [0] * n
reverse_route_cnt[v_g] = 1
reverse_dist[v_g] = 0
for _ in range(n):
    min_dist = float('inf')
    for i in range(n):
        if checked[i] == 0 and reverse_dist[i] < min_dist:
            target_v = i
            min_dist = reverse_dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v] or reverse_dist[target_v] + cost != total_dist - dist[to_v]:
            continue
        if reverse_dist[target_v] + cost < reverse_dist[to_v]:
            reverse_dist[to_v] = reverse_dist[target_v] + cost
            reverse_route_cnt[to_v] = reverse_route_cnt[target_v]
        elif reverse_dist[target_v] + cost == reverse_dist[to_v]:
            reverse_route_cnt[to_v] = (reverse_route_cnt[target_v] + reverse_route_cnt[to_v]) % mod
        if reverse_dist[target_v] <= half_dist < reverse_dist[to_v]:
            enc_li += [(to_v, target_v)]

ret = 0
for from_v, to_v, in enc_li:
    ret += route_cnt[from_v] * reverse_route_cnt[to_v] % mod
    ret %= mod

print((route_cnt[v_g] ** 2 % mod - ret) % mod)

Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
s, t = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(m):
    u, v, d = map(int, input().split())
    graph[u - 1] += [(v - 1, d)]
    graph[v - 1] += [(u - 1, d)]


vst = s - 1
v_g = t - 1
checked = [0] * n
dist = [float('inf')] * n
route_cnt = [0] * n
route_cnt[vst] = 1
dist[vst] = 0
for _ in range(n):
    min_dist = float('inf')
    for i in range(n):
        if checked[i] == 0 and dist[i] < min_dist:
            target_v = i
            min_dist = dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v]:
            continue
        if dist[target_v] + cost < dist[to_v]:
            dist[to_v] = dist[target_v] + cost
            route_cnt[to_v] = route_cnt[target_v]
        elif dist[target_v] + cost == dist[to_v]:
            route_cnt[to_v] += route_cnt[target_v]
    if target_v == v_g:
        break


total_dist = dist[v_g]
half_dist = dist[v_g] / 2
enc_li = []
checked = [0] * n
reverse_dist = [float('inf')] * n
reverse_route_cnt = [0] * n
reverse_route_cnt[v_g] = 1
reverse_dist[v_g] = 0
for _ in range(n):
    min_dist = float('inf')
    for i in range(n):
        if checked[i] == 0 and reverse_dist[i] < min_dist:
            target_v = i
            min_dist = reverse_dist[i]
    checked[target_v] = 1
    for to_v, cost in graph[target_v]:
        if checked[to_v] or reverse_dist[target_v] + cost != total_dist - dist[to_v]:
            continue
        if reverse_dist[target_v] + cost < reverse_dist[to_v]:
            reverse_dist[to_v] = reverse_dist[target_v] + cost
            reverse_route_cnt[to_v] = reverse_route_cnt[target_v]
        elif reverse_dist[target_v] + cost == reverse_dist[to_v]:
            reverse_route_cnt[to_v] += reverse_route_cnt[target_v]
        if reverse_dist[target_v] <= half_dist < reverse_dist[to_v]:
            enc_li += [(to_v, target_v)]


ret = 0
for from_v, to_v, in enc_li:
    ret += route_cnt[from_v] * reverse_route_cnt[to_v]




print(route_cnt[v_g] ** 2 - ret)

Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)