local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local mod = 1000000007
local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local s, t = io.read("*n", "*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b, d = io.read("*n", "*n", "*n")
  edge[a][b], edge[b][a] = d, d
end

local inf = 1000000000 * 99999 + 1
local slen, tlen = {}, {}
local sway = {}
local asked = {}
for i = 1, n do
  slen[i], tlen[i] = inf, inf
  asked[i] = false
  sway[i] = 0
end
slen[s] = 0
sway[s] = 1
asked[n + 1] = true

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return slen[x] < slen[y] and x or y
  end
end

local st = SegTree.new(n, mergefunc, n + 1)

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    if slen[src] + cost < slen[dst] then
      slen[dst] = slen[src] + cost
      sway[dst] = sway[src]
      st:update(dst)
    elseif slen[src] + cost == slen[dst] then
      sway[dst] = badd(sway[dst], sway[src])
    end
  end
end

local stlen = slen[t]
local ret = bmul(sway[t], sway[t])
if inf <= stlen then
  print(0)
  os.exit()
end

local function mergefunc2(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return tlen[x] < tlen[y] and x or y
  end
end
st.func = mergefunc2
local tway = {}
for i = 1, n do
  asked[i] = false
  tway[i] = 0
end
tlen[t] = 0
tway[t] = 1
st:updateAll()

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  if tlen[src] * 2 == stlen then
    local z = bmul(sway[src], tway[src])
    z = bmul(z, z)
    ret = bsub(ret, z)
  end
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    local tlsrc = tlen[src]
    if tlsrc * 2 < stlen and stlen < (tlsrc + cost) * 2
    and tlsrc + cost + slen[dst] == stlen then
      local z = bmul(tway[src], sway[dst])
      z = bmul(z, z)
      ret = bsub(ret, z)
    end
    if tlen[src] + cost < tlen[dst] then
      tlen[dst] = tlen[src] + cost
      tway[dst] = tway[src]
      st:update(dst)
    elseif tlen[src] + cost == tlen[dst] then
      tway[dst] = badd(tway[dst], tway[src])
    end
  end
end
print(ret)


Measured memory_bytes: 28944