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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using Debug;
//using static System.Globalization.CultureInfo;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static int[] nm;static int[] st;
    static ModInt[] memo1;
    static ModInt[] memo2;
    static long[] dd;
    static P[] ps;
    static void Solve()
    {
        nm = Input.ar;
        st = Input.ar;
        ps = P.Make(nm[0], nm[1]);
        memo1 = new ModInt[nm[0]];
        memo2 = new ModInt[nm[0]]; 
        dp(memo1,st[0]-1);
        dd = new long[nm[0]];
        for (var i = 0; i < nm[0]; i++)
        {
            dd[i] = ps[i].Weight;
            ps[i].Weight = Input.Inf;
        }
        dp(memo2,st[1]-1);
        var res = memo1[st[1] - 1] * memo1[st[1] - 1];
        WriteLine(res - calc());
    }
    static ModInt calc()
    {
        var t = new ModInt();
        foreach (var p in ps)
        {
            if (p.Weight * 2 == ps[st[0] - 1].Weight)
                t += memo1[p.index] * memo1[p.index] * memo2[p.index] * memo2[p.index];
            foreach (var ad in p.adj)
                if (dd[p.index] + p.Weight == ps[st[0] - 1].Weight &&
                    dd[ad] + ps[ad].Weight == ps[st[0] - 1].Weight &&
                    2 * p.Weight < ps[st[0] - 1].Weight &&
                    2 * ps[ad].Weight > ps[st[0] - 1].Weight &&
                    ps[ad].Weight - p.Weight == p.dist[ad])
                    t += memo2[p.index] * memo2[p.index] * memo1[ad] * memo1[ad];
        }
        return t;
    }
    static void dp(ModInt[] memo,int init)
    {
        var pq = new PriorityQueue<Pair<long, int>>(new Pair<long, int>(long.MaxValue, -1), false);
        ps[init].Weight = 0;
        pq.Enqueue(new Pair<long, int>(0, init));
        memo[init] = 1;
        while (!pq.isEmpty)
        {
            var p = pq.Dequeue();
            if (p.v1 > ps[p.v2].Weight) continue;
            foreach (var ad in ps[p.v2].adj)
                if (ps[ad].Weight > ps[p.v2].Weight + ps[p.v2].dist[ad])
                {
                    ps[ad].Weight = ps[p.v2].Weight + ps[p.v2].dist[ad];
                    memo[ad] = memo[p.v2];
                    pq.Enqueue(new Pair<long, int>(ps[ad].Weight, ad));
                }
                else if (ps[ad].Weight == ps[p.v2].Weight + ps[p.v2].dist[ad])
                    memo[ad] += memo[p.v2];
        }
    }
}

public class Input
{
    public static string read => ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public  static int num => ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(_ => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(_ => arL).ToArray();
    public static T getValue<T>(string g)
    {
        var t = typeof(T);
        if (t == typeof(int))
            return (T)(object)int.Parse(g);
        if (t == typeof(long))
            return (T)(object)long.Parse(g);
        if (t == typeof(string))
            return (T)(object)g;
        if (t == typeof(char))
            return (T)(object)char.Parse(g);
        if (t == typeof(double))
            return (T)(object)double.Parse(g);
        if (t == typeof(bool))
            return (T)(object)bool.Parse(g);
        return default(T);
    }
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}
public class P : IComparable<P>
{
    public int index;
    public List<int> adj;
    public List<int> rev;
    public Dictionary<int,long> dist;
    public int parent;
    public long Weight { get; set; }
    public P(int index,long Weight=Input.Inf)
    {
        this.index = index;
        adj = new List<int>();
        rev = new List<int>();
        dist = new Dictionary<int,long>();
        this.Weight = Weight;
    }
    public static P[] Make(int num, int edge = 0, bool isNotDirected = true, bool isWeighted = true)
    {
        var ps = Enumerable.Range(0, num).Select(v => new P(v)).ToArray();
        for (var i = 0; i < edge; i++)
        {
            var inp = Input.ar;
            inp[0]--;
            inp[1]--;
            for (var j = 0; j <= ToInt32(isNotDirected); j++)
            {
                ps[inp[j]].adj.Add(inp[1 - j]);
                if (!isNotDirected)
                    ps[inp[1 - j]].rev.Add(inp[j]);
                if (isWeighted)
                    ps[inp[j]].dist[inp[1 - j]] = inp[2];
            }
        }
        return ps;
    }
    public override string ToString()
        => $"{index + 1} {Weight}";
    public override int GetHashCode()
        => index.GetHashCode();
    public int CompareTo(P p)
        => Weight.CompareTo(p.Weight);
}

public class PriorityQueue<T> where T : IComparable<T>
{
    private readonly List<T> items;
    private T MinValue { get; }//一番優先度が低い値

    public bool IsMax { get; }
    public int Size { get { return items.Count; } }
    public bool isEmpty { get { return items.Count == 0; } }
    
    public PriorityQueue(T MinValue,bool IsMax=true)
    {
        this.items = new List<T>();
        this.IsMax = IsMax;
        this.MinValue = MinValue;
    }

    private int Parent(int num)
    {
        return (num + 1) / 2 - 1;
    }
    private int Left(int num)
    { 
        return 2 * num + 1;
    }
    private int Right(int num)
    { 
        return 2 * (num + 1);
    }

    private void MaxHeapify(int num)
    {
        var left = Left(num);
        var right = Right(num);
        var largest = 0;
        if (Size >= left + 1 && items[left].CompareTo(items[num]) == 1)
            largest = left;
        else largest = num;
        if (Size >= right + 1 && items[right].CompareTo(items[largest]) == 1)
            largest = right;
        if (largest != num)
        {
            var value = items[largest];
            items[largest] = items[num];
            items[num] = value;
            MaxHeapify(largest);
        }
    }

    private void MinHeapify(int num)
    {
        var left = Left(num);
        var right = Right(num);
        var smallest = 0;
        if (Size >= left + 1 && items[num].CompareTo(items[left]) == 1) smallest = left;
        else smallest = num;
        if (Size >= right + 1 && items[smallest].CompareTo(items[right]) == 1) smallest = right;
        if (smallest != num)
        {
            var value = items[smallest];
            items[smallest] = items[num];
            items[num] = value;
            MinHeapify(smallest);
        }
    }

    private bool ChangeKey(int index, T reValue)
    {
        if (IsMax && items[index].CompareTo(reValue) == 1)
            return false;
        if (!IsMax && items[index].CompareTo(reValue) == -1)
            return false;
        items[index] = reValue;
        if (IsMax)
        {
            while (index > 0 && items[Parent(index)].CompareTo(items[index]) == -1)
            {
                var value = items[Parent(index)];
                items[Parent(index)] = items[index];
                items[index] = value;
                index = Parent(index);
            }
        }
        else
        {
            while (index > 0 && items[Parent(index)].CompareTo(items[index]) == 1)
            {
                var value = items[Parent(index)];
                items[Parent(index)] = items[index];
                items[index] = value;
                index = Parent(index);
            }
        }
        return true;
    }

    public T Peek { get { return items[0]; } }

    public T Dequeue()
    {
        if (Size <= 0) return default(T);
        var value = items[0];
        items[0] = items[Size - 1];
        items.RemoveAt(Size - 1);
        if (IsMax) MaxHeapify(0);
        else MinHeapify(0);
        return value;
    }

    public bool Enqueue(T key)
    {
        items.Add(MinValue);
        return ChangeKey(Size-1, key);
    }
}

public struct ModInt
{
    public const long MOD = (int)1e9 + 7;
    public long num { get; set; }
    public ModInt(long n = 0) { num = n; }
    public override string ToString()
        => num.ToString();
    public static ModInt operator +(ModInt l, ModInt r)
    {
        l.num += r.num;
        if (l.num >= MOD) l.num -= MOD;
        return l;
    }
    public static ModInt operator -(ModInt l, ModInt r)
    {
        l.num -= r.num;
        if (l.num < 0) l.num += MOD;
        return l;
    }
    public static ModInt operator *(ModInt l, ModInt r)
        => new ModInt(l.num * r.num % MOD);
    public static implicit operator ModInt(long n)
    {
        n %= MOD; if (n < 0) n += MOD;
        return new ModInt(n);
    }
    public static ModInt Pow(ModInt v, long k)
        => Pow(v.num, k);

    public static ModInt Pow(long v, long k)
    {
        var tm = 1L;
        for (k %= MOD - 1; k > 0; k >>= 1, v = v * v % MOD)
            if ((k & 1) == 1) tm = tm * v % MOD;
        return new ModInt(tm);
    }
    public static ModInt Inverse(ModInt v)
        => Pow(v, MOD - 2);
}

public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1 { get; set; }
    public T2 v2 { get; set; }
    public Pair() : this(default(T1), default(T2)) { }
    public Pair(T1 v1, T2 v2)
    { SetValue(v1, v2); }

    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public void SetValue(T1 v1,T2 v2) { this.v1 = v1;this.v2 = v2; }
    public static Pair<T1, T2> makePair()
    {
        var r = ReadLine().Split(' ');
        return new Pair<T1, T2>(Input.getValue<T1>(r[0]), Input.getValue<T2>(r[1]));
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
}