Problem:
Score : 700 points

Problem Statement

We have a graph with N vertices and M edges, and there are two people on the graph: Takahashi and Aoki.

The i-th edge connects Vertex U_i and Vertex V_i.
The time it takes to traverse this edge is D_i minutes, regardless of direction and who traverses the edge (Takahashi or Aoki).

Takahashi departs Vertex S and Aoki departs Vertex T at the same time. Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the shortest time possible.
Find the number of the pairs of ways for Takahashi and Aoki to choose their shortest paths such that they never meet (at a vertex or on an edge) during the travel, modulo 10^9 + 7.

Constraints

1 \leq N \leq 100 000

1 \leq M \leq 200 000

1 \leq S, T \leq N

S \neq T

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq D_i \leq 10^9 (1 \leq i \leq M)

If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j).

U_i \neq V_i (1 \leq i \leq M)

D_i are integers.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M

Output

Print the answer.

Sample Input 1

4 4
1 3
1 2 1
2 3 1
3 4 1
4 1 1

Sample Output 1

2

There are two ways to choose shortest paths that satisfies the condition:

Takahashi chooses the path 1 \rightarrow 2 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 4 \rightarrow 1.

Takahashi chooses the path 1 \rightarrow 4 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 2 \rightarrow 1.

Sample Input 2

3 3
1 3
1 2 1
2 3 1
3 1 2

Sample Output 2

2

Sample Input 3

3 3
1 3
1 2 1
2 3 1
3 1 2

Sample Output 3

2

Sample Input 4

8 13
4 2
7 3 9
6 2 3
1 6 4
7 6 9
3 8 9
1 2 2
2 8 12
8 6 9
2 5 5
4 2 18
5 3 7
5 1 515371567
4 8 6

Sample Output 4

6

Solution in C++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007

//ここから編集する
class Dijkstra {
public:
    struct edge {
        ll to, cost;
    };
    typedef pair<ll, ll> P;//firstは最短距離、secondは頂点の番号

    int V;//超点数
    vector<vector<edge> > G;//グラフ,G[i]はiから出る辺の集合,G[i][j]はiから出る辺のj番目の辺
    vector<ll> d; //最短距離
    vector<ll> dp;

    //0origin
    Dijkstra(int N) {
        V = N;
        G = vector<vector<edge> >(N);
        d = vector<ll>(N);
        dp = vector<ll>(N);
    };

    void dijkstra(int s) {
        priority_queue<P, vector<P>, greater<P> > que;
        fill(d.begin(), d.end(), INFl);
        d[s] = 0;
        dp[s] = 1;
        que.push(P(0, s));

        while (!que.empty()) {
            P p = que.top();
            que.pop();
            ll v = p.second;
            if (d[v] < p.first) continue;
            for (int i = 0; i < G[v].size(); i++) {
                edge e = G[v][i];
                if (d[e.to] == d[v] + e.cost && d[v] != INFl) {
                    dp[e.to] += dp[v];
                    dp[e.to] %= MOD;
                }
                if (d[e.to] > d[v] + e.cost && !(d[e.to] == INFl && d[v] == INFl)) {
                    d[e.to] = d[v] + e.cost;
                    dp[e.to] = dp[v];
                    que.push(P(d[e.to], e.to));
                }
            }
        }

    }


    /* 頂点fromに頂点toに達するコストcostの辺を貼る */
    void addEdge(int from, int to, int cost) {
        edge e;
        e.to = to;
        e.cost = cost;
        G[from].push_back(e);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    int S, T;
    cin >> S >> T;
    S--;
    T--;
    Dijkstra dijA(N), dijB(N);

    rep(i, 0, M) {
        int U, V, D;
        cin >> U >> V >> D;
        U--;
        V--;
//        int mid = i + N;
        dijA.addEdge(U, V, D);
//        dijA.addEdge(mid, U, D);
//        dijA.addEdge(mid, V, D);
        dijA.addEdge(V, U, D);

        dijB.addEdge(U, V, D);
        dijB.addEdge(V, U, D);
//        dijB.addEdge(mid, V, D);
//        dijB.addEdge(V, mid, D);
    }
    dijA.dijkstra(S);
    dijB.dijkstra(T);
    ll dist = dijA.d[T];
    ll ans = (dijA.dp[T] * dijB.dp[S]) % MOD;
    rep(i, 0, dijA.d.size()) {
        if (dijA.d[i] * 2 == dist) {
            ans = (ans - dijA.dp[i] * dijB.dp[i] * dijA.dp[i] * dijB.dp[i]) % MOD;
            if (ans < 0) ans += MOD;
        }
    }
//    ans -= dijA.dp[T];
//    ans %= MOD;
//    if (ans < 0) ans += MOD;
    rep(i, 0, dijA.G.size()) {
        rep(j, 0, dijA.G[i].size()) {
            if (dijA.d[i] * 2 < dist &&
                dijB.d[dijA.G[i][j].to] * 2 < dist &&
                dijA.d[i] + dijB.d[dijA.G[i][j].to] + dijA.G[i][j].cost == dist) {
                ll tmp = dijA.dp[i] * dijB.dp[dijA.G[i][j].to]  % MOD;
                tmp = tmp * tmp % MOD;
//                ans -= (dijA.dp[i] * dijB.dp[dijA.G[i][j].to] * dijA.dp[i] * dijB.dp[dijA.G[i][j].to]) % MOD;
                ans -= tmp;
                ans %= MOD;
                if (ans < 0) ans += MOD;
            }
        }
    }

    cout << ans << endl;
    return 0;
}