#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const long long mod = 1e9 + 7;
const long long inf = 1e17;
struct edge {
  int to, nxt, val;
} e[N << 2];
int cnt = 0, head[N], n, m, tot[N << 1], tot1[N << 1], s, t, in[N];
long long dis[N << 1], dis1[N << 1], mindis, ans = 0;
queue<int> q, qtop;
vector<int> v[N];
bool vis[N];
void addedge(int x, int y, int v) {
  ++cnt;
  e[cnt].to = y;
  e[cnt].nxt = head[x];
  e[cnt].val = v;
  head[x] = cnt;
}
void spfa(int x, long long *dis) {
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i) dis[i] = inf;
  dis[x] = 0;
  while (!q.empty()) q.pop();
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = e[i].nxt) {
      int v = e[i].to;
      if ((dis[u] + e[i].val) < dis[v]) {
        dis[v] = dis[u] + e[i].val;
        if (!vis[v]) {
          q.push(v);
          vis[v] = 1;
        }
      }
    }
  }
  return;
}
void top(int x, long long *dis, int *tot) {
  tot[x] = 1;
  for (int i = 1; i <= n; ++i) v[i].clear();
  for (int i = 1; i <= n; ++i) {
    for (int j = head[i]; ~j; j = e[j].nxt) {
      int v1 = e[j].to;
      if ((dis[i] + e[j].val) == dis[v1]) {
        v[i].push_back(v1);
        ++in[v1];
      }
    }
  }
  while (!qtop.empty()) qtop.pop();
  qtop.push(x);
  while (!qtop.empty()) {
    int u = qtop.front();
    qtop.pop();
    for (int i = 0; i < v[u].size(); ++i) {
      int x = v[u][i];
      if (!(--in[x])) {
        qtop.push(x);
      }
      tot[v[u][i]] = (tot[v[u][i]] + tot[u]) % mod;
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  scanf("%d%d", &s, &t);
  for (int i = 1; i <= m; ++i) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    addedge(x, y, v);
    addedge(y, x, v);
  }
  spfa(s, dis);
  top(s, dis, tot);
  spfa(t, dis1);
  top(t, dis1, tot1);
  mindis = dis[t];
  ans = 1ll * tot[t] * tot1[s] % mod;
  for (int i = 1; i <= n; ++i) {
    if ((dis[i] == dis1[i]) && (dis[i] + dis1[i] == mindis)) {
      ans =
          (ans - 1ll * tot[i] * tot[i] % mod * tot1[i] * tot1[i] % mod + mod) %
          mod;
    }
  }
  for (int x = 1; x <= n; ++x) {
    for (int j = head[x]; ~j; j = e[j].nxt) {
      int v = e[j].to;
      if (((dis[x] + dis1[v] + e[j].val) == mindis) && (dis[x] * 2 < mindis) &&
          (dis1[v] * 2 < mindis)) {
        ans = (ans - 1ll * tot[x] * tot[x] % mod * tot1[v] * tot1[v] % mod +
               mod) %
              mod;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
