package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	s := getNextInt(scanner) - 1
	t := getNextInt(scanner) - 1
	g := newGraph(n)
	for i := 0; i < m; i++ {
		u := getNextInt(scanner) - 1
		v := getNextInt(scanner) - 1
		d := getNextInt64(scanner)
		g.appendEdge(u, v, i, d)
		g.appendEdge(v, u, i, d)
	}
	dd := &daikes{}
	heap.Push(dd, newDaike(s, s, 0))
	g.v[s].c = 1
	for i := 0; i < n; i++ {
		g.v[i].d = math.MaxInt64
		g.v[i].rd = math.MaxInt64
	}
	for dd.Len() > 0 {
		p := heap.Pop(dd).(daike)
		if g.v[p.i].d < p.d {
			continue
		}
		if g.v[p.i].d == p.d {
			g.v[p.i].c.addAs(g.v[p.f].c)
			continue
		}
		g.v[p.i].c = g.v[p.f].c
		g.v[p.i].d = p.d
		for _, e := range g.e[p.i] {
			heap.Push(dd, newDaike(p.i, e.to, p.d+e.d))
		}
	}
	heap.Push(dd, newDaike(t, t, 0))
	g.v[t].rc = 1
	for dd.Len() > 0 {
		p := heap.Pop(dd).(daike)
		if g.v[p.i].rd < p.d {
			continue
		}
		if g.v[p.i].rd == p.d {
			g.v[p.i].rc.addAs(g.v[p.f].rc)
			continue
		}
		g.v[p.i].rc = g.v[p.f].rc
		g.v[p.i].rd = p.d
		for _, e := range g.e[p.i] {
			heap.Push(dd, newDaike(p.i, e.to, p.d+e.d))
		}
	}
	ans := g.v[t].c.mul(g.v[t].c)
	for i := 0; i < n; i++ {
		if g.v[i].d*2 == g.v[t].d && g.v[i].rd*2 == g.v[s].rd {
			ans.subAs(g.v[i].c.mul(g.v[i].rc).pow(2))
		}
		for _, e := range g.e[i] {
			if g.v[i].d*2 < g.v[t].d && g.v[e.to].rd*2 < g.v[s].rd && g.v[i].d+g.v[e.to].rd+e.d == g.v[t].d {
				ans.subAs(g.v[i].c.mul(g.v[e.to].rc).pow(2))
			}
		}
	}
	fmt.Fprintln(writer, ans)
}

func newDaike(f, i int, d int64) daike {
	return daike{f: f, i: i, d: d}
}

type daike struct {
	f, i int
	d    int64
}

type daikes []daike

func (h daikes) Len() int { return len(h) }
func (h daikes) Less(i, j int) bool {
	return h[i].d < h[j].d
}
func (h daikes) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *daikes) Push(x interface{}) {
	*h = append(*h, x.(daike))
}
func (h *daikes) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

type vertex struct {
	d, rd int64
	c, rc mint
}
type edge struct {
	to, id int
	d      int64
}
type graph struct {
	v []vertex
	e [][]edge
}

func newGraph(n int) graph {
	return graph{
		v: make([]vertex, n),
		e: make([][]edge, n),
	}
}
func (g *graph) appendEdge(from, to, id int, d int64) {
	g.e[from] = append(g.e[from], edge{
		to: to,
		id: id,
		d:  d,
	})
}

type mint int64

func (mt mint) mod() mint {
	m := mint(1e9 + 7)
	mt %= m
	if mt < 0 {
		return mt + m
	}
	return mt
}
func (mt mint) inv() mint {
	return mt.pow(mint(0).sub(2))
}
func (mt mint) pow(n mint) mint {
	p := mint(1)
	for n > 0 {
		if n&1 == 1 {
			p.mulAs(mt)
		}
		mt.mulAs(mt)
		n >>= 1
	}
	return p
}
func (mt mint) add(x mint) mint {
	return (mt + x).mod()
}
func (mt mint) sub(x mint) mint {
	return (mt - x).mod()
}
func (mt mint) mul(x mint) mint {
	return (mt * x).mod()
}
func (mt mint) div(x mint) mint {
	return mt.mul(x.inv())
}
func (mt *mint) addAs(x mint) *mint {
	*mt = mt.add(x)
	return mt
}
func (mt *mint) subAs(x mint) *mint {
	*mt = mt.sub(x)
	return mt
}
func (mt *mint) mulAs(x mint) *mint {
	*mt = mt.mul(x)
	return mt
}
func (mt *mint) divAs(x mint) *mint {
	*mt = mt.div(x)
	return mt
}


Measured memory_bytes: 59852