#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100 * 1000 + 3, INF = 1000ll * 1000 * 1000 * MAXN + 3,
                D = 1000 * 1000 * 1000 + 7;
long long n, m, s, t, d[2 + 1][MAXN], cnt[2 + 1][MAXN], w[MAXN], e[MAXN], ans;
vector<long long> g[MAXN];
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    p;
bitset<MAXN> mark;
void dij(long long x, long long y) {
  mark.reset();
  fill(d[y], d[y] + n, INF);
  p.push({0, x});
  d[y][x] = 0;
  cnt[y][x] = 1;
  while (!p.empty()) {
    long long f = p.top().second;
    p.pop();
    if (mark[f]) continue;
    mark[f] = true;
    for (auto i : g[f]) {
      long long z = f ^ e[i];
      if (d[y][f] + w[i] < d[y][z]) {
        d[y][z] = d[y][f] + w[i];
        p.push((pair<long long, long long>){d[y][z], z});
        cnt[y][z] = cnt[y][f];
      } else if (d[y][f] + w[i] == d[y][z])
        cnt[y][z] += cnt[y][f];
    }
  }
}
int32_t main() {
  cin >> n >> m >> s >> t;
  for (long long i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v >> w[i];
    g[--u].push_back(i), g[--v].push_back(i);
    e[i] = u ^ v;
  }
  s--, t--;
  dij(s, 0), dij(t, 1);
  ans = cnt[0][t] * cnt[1][s] % D;
  long long q = d[0][t];
  for (long long i = 0; i < n; i++) {
    long long a = cnt[0][i], x = d[0][i], o = cnt[1][i];
    for (auto j : g[i]) {
      long long zz = e[j] ^ i;
      long long y = d[1][zz], b = cnt[1][zz];
      if (x + w[j] != d[0][zz] || y + w[j] != d[1][i] || x + y + w[j] != q ||
          abs(x - y) >= w[j])
        continue;
      ans -= a * b % D * a % D * b % D;
      ans %= D;
    }
    if (q == d[0][i] + d[1][i] && d[0][i] == d[1][i])
      ans -= a * o % D * a % D * o % D;
  }
  cout << (ans + D) % D;
  return 0;
}
