using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    long[] DPS;
    long[] DPT;
    Dijkstra DS;
    Dijkstra DT;
    List<int>[] G;
    List<long>[] C;
    int[] IN;
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = re.i();
        int M = re.i();
        int S = re.i()-1;
        int T = re.i()-1;
        int[] F = new int[2*M];
        int[] E = new int[2*M];
        long[] COST = new long[2*M];
        G = new List<int>[N];
        C = new List<long>[N];
        for(int j=0;j<N;j++){
            G[j] = new List<int>();
            C[j] = new List<long>();
        }
        for(int j=0;j<M;j++){
            int f = re.i()-1;
            int t = re.i()-1;
            G[f].Add(t);
            G[t].Add(f);
            long c = re.i();
            C[f].Add(c);
            C[t].Add(c);
            F[j*2] = f;
            E[j*2] = t;
            COST[j*2] = c;
            F[j*2+1] = t;
            E[j*2+1] = f;
            COST[j*2+1] = c;
        }
        DS = new Dijkstra(F,E,COST,N,S);
        DT = new Dijkstra(F,E,COST,N,T);
        DPS = new long[N];
        DPT = new long[N];
        DPS[S] = 1;
        DPT[T] = 1;
        IN = new int[N];
        for(int i=0;i<2*M;i++){
            if(DS.GetCost(F[i]) + COST[i] == DS.GetCost(E[i])){
                IN[E[i]]++;
            }
        }
        dfsS(S);
        for(int i=0;i<N;i++){
            IN[i] = 0;
        }
        for(int i=0;i<2*M;i++){
            if(DT.GetCost(F[i]) + COST[i] == DT.GetCost(E[i])){
                IN[E[i]]++;
            }
        }
        dfsT(T);
        long count = DPS[T]*DPT[S]%Define.mod;
        long Time = DS.GetCost(T);
        for(int i=0;i<2*M;i++){
            if(DS.GetCost(F[i])*2 < Time && DT.GetCost(E[i])*2 < Time && DS.GetCost(F[i]) + DT.GetCost(E[i]) + COST[i] == Time){
                long Temp = DPS[F[i]]*DPT[E[i]]%Define.mod*DPS[F[i]]%Define.mod*DPT[E[i]]%Define.mod;
                count = (count + Define.mod - Temp) % Define.mod;
            }
        }
        for(int i=0;i<N;i++){
            if(DS.GetCost(i) == DT.GetCost(i) && DS.GetCost(i) + DT.GetCost(i) == Time){
                long Temp = DPS[i]*DPT[i]%Define.mod*DPS[i]%Define.mod*DPT[i]%Define.mod;
                count = (count + Define.mod - Temp) % Define.mod;
            }
        }
        sb.Append(count+"\n");
    }
    void dfsS(int v){
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(DS.GetCost(t) == DS.GetCost(v)+C[v][i]){
                DPS[t] = (DPS[t] + DPS[v]) % Define.mod;
                IN[t]--;
                if(IN[t] == 0){
                    dfsS(t);
                }
            }
        }
    }
    void dfsT(int v){
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(DT.GetCost(t) == DT.GetCost(v)+C[v][i]){
                DPT[t] = (DPT[t] + DPT[v]) % Define.mod;
                IN[t]--;
                if(IN[t] == 0){
                    dfsT(t);
                }
            }
        }
    }
}
struct Edge{
    public Vertex v;
    public long cost;
    public Edge(Vertex v0,long c){
        v = v0;
        cost = c;
    }
}
class Vertex{
    public bool finished;
    public List<Vertex> edges;
    public List<long> costs;
    public long cost;
    public void AddEdge(Vertex v,long c){
        edges.Add(v);
        costs.Add(c);
    }
    public Vertex(){
        edges = new List<Vertex>();
        costs = new List<long>();
        cost = -1;
        finished = false;
    }
}
class Dijkstra{
	int E;
	Vertex[] point;
    Vertex S;
    public long GetCost(int i){
        return point[i].cost;
    }
    public Dijkstra(int[] f,int[] t,long[] cost,int n,int s){
        point = new Vertex[n];
        for(int i=0;i<n;i++){
            point[i] = new Vertex();
        }
        E = f.Length;
        for(int i=0;i<E;i++){
            point[f[i]].AddEdge(point[t[i]],cost[i]);
        }
        S = point[s];
        calc();
    }
    void calc(){
        Heap H = new Heap(2*E+1);
        S.cost = 0;
        H.push(new Edge(S,0));
        while(!H.Empty()){
            Vertex v = H.pop().v;
            if(v.finished){
                continue;
            }
            v.finished = true;
            for(int i=0;i<v.edges.Count;i++){
                Vertex vi = v.edges[i];
                if(vi.cost == -1 || vi.cost > v.costs[i]+v.cost){
                    vi.cost = v.costs[i]+v.cost;
                    H.push(new Edge(vi,vi.cost));
                }
            }
        }
    }
}
class Heap{
    public int size;
    Edge[] obj;
    public Heap(int maxsize){
        size = 0;
        obj = new Edge[maxsize];
    }
    public void push(Edge a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(obj[p].cost <= a.cost){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public bool Empty(){
        return size == 0;
    }
    public Edge pop(){
        Edge t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && obj[p+1].cost < obj[p].cost){
                p++;
            }
            if(obj[p].cost < obj[size].cost){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}


Measured memory_bytes: 114760