program NDK1331;
const
maxn=10000;
maxm=50000;
type
atp=record
y,next,dis:longint;
end;
var
map:array [0..maxm] of atp;
d,w,que,into,first:array [0..maxn] of longint;
v:array [0..maxn] of boolean;
tot,head,tail,n,m,st,ed,time:longint;
procedure init;
var
i,x,y,dis:longint;
begin
readln(n,m,st,ed,time);
for i:=1 to m do
begin
readln(x,y,dis);
inc(into[y]);
inc(tot);
map[tot].y:=y;
map[tot].dis:=dis;
map[tot].next:=first[x];
first[x]:=tot;
end;
end;
procedure TP;
var
t:longint;
begin
fillchar(v,sizeof(v),false);
head:=1;
tail:=1;
v[st]:=true;
que[1]:=st;
w[st]:=1;
while head<=tail do
begin
t:=first[que[head]];
if que[head]=ed then break;
while t>0 do
begin
if not v[map[t].y] then
begin
dec(into[map[t].y]);
if into[map[t].y]=0 then
begin
inc(tail);
que[tail]:=map[t].y;
v[map[t].y]:=true;{必须要等到入度为0才能标记,否则有些路径就遍历不到了。}
end;
d[map[t].y]:=(d[map[t].y] mod 10000+d[que[head]]+((w[que[head]] mod 10000)*(map[t].dis mod 10000)) mod 10000) mod 10000; {d表示到这一点的路径和。}
w[map[t].y]:=(w[map[t].y]+w[que[head]]) mod 10000;{w表示有几条路径通向这个点。}
end;
t:=map[t].next;
end;
inc(head);
end;
end;
begin
assign(input,'NDK1331.in'); reset(input);
assign(output,'NDK1331.out'); rewrite(output);
init;
TP;
writeln((((w[ed]-1)*time mod 10000)+d[ed]) mod 10000);
close(input); close(output);
end.