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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair


ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }

const ll MOD=1000000007LL;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }

const int INF = 999999;

template <typename T>
void make_adjacent(int nV, vector<pair<ii,T>>& arcs, vector<vector<pair<int,T>>>& adj) {
	adj.resize(nV);
	for (pair<ii, T> p: arcs) {
		int u = p.first.first, v = p.first.second;
		assert(u != v);
		T distance = p.second;
		adj[u].push_back(make_pair(v, distance));
		adj[v].push_back(make_pair(u, distance));
	}
}

template <typename T, typename C>
void dij1c(int nV, vector<vector<pair<int,T>>>& adjacent, vector<pair<T,C>>& d, int start) {
	d.assign(nV, make_pair(INF, 0));
	d[start] = make_pair(0, 1);
	vector<int> prev(nV, -1);

	priority_queue<pair<T, pair<int,C>>> Q;
	Q.push(make_pair(0, make_pair(start,1)));

	vector<bool> visited(nV, false);

	while (!Q.empty()) {
		T du = -Q.top().first;
		int u = Q.top().second.first;
        C cnt = Q.top().second.second;
        Q.pop();
		if (visited[u]) continue;
		visited[u] = true;

		int c = adjacent[u].size();
		for (int i=0; i<c; ++i) {
			int v = adjacent[u][i].first;
			T distance = adjacent[u][i].second;
			T alt = du + distance;
			if (d[v].first >= alt) {
				d[v].first = alt;
                d[v].second = ADD(d[v].second, cnt);
				prev[v] = u;
				Q.push(make_pair(-alt, make_pair(v, d[v].second)));
			}
		}
	}
}



class Solver {
    int N, M;
    int S, T;
    vector<int> u, v;
    vector<ll> d;
    ll answer;

 public:
    Solver() {}

 public:
    void input() {
        scanf("%d %d", &N, &M);
        scanf("%d %d", &S, &T);
        u.resize(M);
        v.resize(M);
        d.resize(M);
        rep(i,M) {
            scanf("%d %d %lld", &u[i], &v[i], &d[i]);
        }
        assert(check());
        normalize();
    }
    void output() {
        printf("%lld\n", answer);
    }
    bool check() {
        bool good = true;
        if (!IN(N, 1, 100000)) {
            good = false;
        }
        if (!IN(M, 1, 200000)) {
            good = false;
        }
        if (!IN(S, 1, N)) {
            good = false;
        }
        if (!IN(T, 1, N)) {
            good = false;
        }
        if (S == T) {
            good = false;
        }
        rep(i,M){
            if (!IN(u[i], 1, N)) good = false;
            if (!IN(v[i], 1, N)) good = false;
            if (!IN(d[i], 1, 1e9)) good = false;
        }
        return good;
    }
    void normalize() {
        --S;
        --T;
        rep(i,M){
            --u[i];
            --v[i];
        }
    }
    void solve() {
        vector<pair<ii,ll>> arcs(M);
        rep(i,M){
            arcs[i] = make_pair(ii(u[i],v[i]), d[i]);
        }
        vector<vector<pair<int,ll>>> adj;
        make_adjacent(N, arcs, adj);
        vector<pair<ll,ll>> dS, dT;
        dij1c(N, adj, dS, S);
        dij1c(N, adj, dT, T);

        ll best_time = dS[T].first;
        assert(dS[T] == dT[S]);
        answer = POW(dS[T].second, 2);

        rep(x,N){
            if (x == S || x == T) continue;
            ll sx = dS[x].first, tx = dT[x].first;
            if (sx == tx && sx + tx == best_time) {
                ll cnt = MUL(dS[x].second, dT[x].second);
                answer = SUB(answer, cnt);
            }
        }

        rep(i,M){
            ll su = dS[u[i]].first, tv = dT[v[i]].first;
            if (su + d[i] + tv == best_time
                && su*2 < best_time && best_time < (su+d[i])*2) {
                ll cnt = MUL(dS[u[i]].second, dT[v[i]].second);
                answer = SUB(answer, cnt);
            }
            ll sv = dS[v[i]].first, tu = dT[u[i]].first;
            if (sv + d[i] + tu == best_time
                && sv*2 < best_time && best_time < (sv+d[i])*2) {
                ll cnt = MUL(dS[v[i]].second, dT[u[i]].second);
                answer = SUB(answer, cnt);
            }
        }
    }

};

int main() {
    Solver *s = new Solver();
    s->input();
    s->solve();
    s->output();
    delete s;
    return 0;
}