#include <bits/stdc++.h>
using namespace std;
int s, t;
int vertex, path;
vector<vector<pair<long long, int> > > connection;
void shortest_path_from(int start, vector<long long>* dists) {
  vector<long long>& dist_table = *dists;
  dist_table.resize(vertex, -1);
  multimap<long long, int> q;
  q.emplace(0LL, start);
  while (!q.empty()) {
    auto iter = q.begin();
    long long d_m = iter->first;
    int m = iter->second;
    q.erase(iter);
    if (dist_table[m] != -1) continue;
    dist_table[m] = d_m;
    for (auto& p : connection[m]) {
      q.emplace(d_m + p.first, p.second);
    }
  }
}
vector<int> path_count1(const vector<long long>& dist_table) {
  vector<int> count_table(vertex, 0);
  count_table[s] = 1;
  multimap<long long, int> q;
  for (int i = 0; i < dist_table.size(); i++) {
    if (i != s) q.emplace(dist_table[i], i);
  }
  for (auto& p : q) {
    long long d_m = p.first;
    int m = p.second;
    int num_paths = 0;
    for (auto& p2 : connection[m]) {
      long long d_mn = p2.first;
      int n = p2.second;
      long long d_n = dist_table[n];
      if (0 < count_table[n] && d_m == d_n + d_mn)
        num_paths = (num_paths + count_table[n]) % 1000000007;
    }
    count_table[m] = num_paths;
  }
  return count_table;
}
vector<int> path_count2(const vector<long long>& dist_table) {
  vector<int> count_table(vertex, 0);
  count_table[t] = 1;
  multimap<long long, int> q;
  for (int i = 0; i < dist_table.size(); i++) {
    if (i != t) q.emplace(-dist_table[i], i);
  }
  for (auto& p : q) {
    long long d_m = -p.first;
    int m = p.second;
    int num_paths = 0;
    for (auto& p2 : connection[m]) {
      long long d_mn = p2.first;
      int n = p2.second;
      long long d_n = dist_table[n];
      if (0 < count_table[n] && d_m + d_mn == d_n)
        num_paths = (num_paths + count_table[n]) % 1000000007;
    }
    count_table[m] = num_paths;
  }
  return count_table;
}
int solve() {
  vector<long long> dist_from_s;
  shortest_path_from(s, &dist_from_s);
  const vector<int>& count_from_s = path_count1(dist_from_s);
  const vector<int>& count_from_t = path_count2(dist_from_s);
  long long d_t = dist_from_s[t];
  long long cnt_t = (long long)count_from_s[t];
  long long patterns = (cnt_t * cnt_t) % 1000000007;
  long long patterns_to_remove = 0;
  for (int u = 0; u < vertex; u++) {
    long long d_u = dist_from_s[u];
    if (2 * d_u == d_t) {
      long long cnt_su = (long long)count_from_s[u];
      long long cnt_tu = (long long)count_from_t[u];
      patterns_to_remove += (cnt_su * cnt_tu % 1000000007) *
                            (cnt_su * cnt_tu % 1000000007) % 1000000007;
      patterns_to_remove = patterns_to_remove % 1000000007;
    } else if (2 * d_u < d_t) {
      long long cnt_su = (long long)count_from_s[u];
      for (auto& p : connection[u]) {
        long long d_uv = p.first;
        int v = p.second;
        long long d_v = dist_from_s[v];
        if (d_u + d_uv == d_v && d_t < 2 * d_v) {
          long long cnt_tv = (long long)count_from_t[v];
          patterns_to_remove += (cnt_su * cnt_tv % 1000000007) *
                                (cnt_su * cnt_tv % 1000000007) % 1000000007;
          patterns_to_remove = patterns_to_remove % 1000000007;
        }
      }
    }
  }
  return (patterns - patterns_to_remove + 1000000007) % 1000000007;
}
int main() {
  cin >> vertex >> path;
  cin >> s >> t;
  s--;
  t--;
  connection.resize(vertex);
  int n, m, d;
  for (int i = 0; i < path; i++) {
    cin >> n >> m >> d;
    n--;
    m--;
    connection[n].push_back(make_pair(d, m));
    connection[m].push_back(make_pair(d, n));
  }
  cout << solve() << endl;
}
