Problem:
Score : 900 points

Problem Statement

We have an undirected weighted graph with N vertices and M edges.
The i-th edge in the graph connects Vertex U_i and Vertex V_i, and has a weight of W_i.
Additionally, you are given an integer X.

Find the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo 10^9 + 7:

The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of X.

Here, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.

Constraints

1 \leq N \leq 1 000

1 \leq M \leq 2 000

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq W_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)

The given graph is connected.

1 \leq X \leq 10^{12}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
X
U_1 V_1 W_1
U_2 V_2 W_2
:
U_M V_M W_M

Output

Print the answer.

Sample Input 1

3 3
2
1 2 1
2 3 1
3 1 1

Sample Output 1

6

Sample Input 2

3 3
3
1 2 1
2 3 1
3 1 2

Sample Output 2

2

Sample Input 3

5 4
1
1 2 3
1 3 3
2 4 6
2 5 8

Sample Output 3

0

Sample Input 4

8 10
49
4 6 10
8 4 11
5 8 9
1 8 10
3 8 128773450
7 8 10
4 2 4
3 4 1
3 1 13
5 2 2

Sample Output 4

4

Solution in Ruby (2.3.3) (judge verdict: Wrong Answer)

require 'pp'
# IS_DEBUG = true
IS_DEBUG = false
def dputs str
	if(IS_DEBUG)
		puts str
	end
end
def dpp str
	if(IS_DEBUG)
		pp str
	end
end

require 'pp'

def one2zero(g)
	g.map{|v1,v2,w| [v1-1,v2-1,w]}
end

w_i = 2

class Unionfind
	class Set
		attr_accessor :self, :parent, :rank
		def initialize(x)
			@self = x
			@parent = x
			@rank = 0
		end
	end
	def initialize(num)
		@sets = Array.new(num){|i| makeset(i)}
	end
	def makeset(i)
		Set.new(i)
	end
	# return tree number x.self
	def find(x)
		sx = @sets[x]
		if(sx.parent == x)
			return x
		else
			sx.parent = find(sx.parent)
			return sx.parent
		end
	end
	def union(x,y)
		xr = find(x)
		yr = find(y)
		if(@sets[xr].rank > @sets[yr].rank)
			@sets[yr].parent = xr
		elsif(@sets[xr].rank < @sets[yr].rank)
			@sets[xr].parent = yr
		elsif(xr != yr)
			@sets[yr].parent = xr
			@sets[xr].rank += 1
		end
	end
end

# answer the weight of min spanning tree of g containing e
# g is all edges of the graph, representating the graph
def kraskal(g_sorted, num_vertexes, e=[])
	def connect(v1,v2,v2t)
		# t2 merge to t1
		r1 = v2t.find(v1)
		r2 = v2t.find(v2)
		v2t.union(r1,r2)
	end
	
	# init
	# generate forest mapping vertex i to tree i
	# 0 indexed : vertex i+1 (1 indexed) and tree i+1 map to vartex[i]=i
	v2t = Unionfind.new(num_vertexes)
	num_rest = num_vertexes - 1
	w_tree = 0
	
	# optinal behavior
	# fixed edge to select
	e.each{|v1,v2,w|
		num_rest -= 1
		w_tree += w
		connect(v1,v2,v2t)
	}

	# start
	g_sorted.each{|v1,v2,w|
		if(num_rest == 0)
			break
		end
		dputs "compare tree of #{v1} #{v2} #{w} : #{v2t.find(v1)} #{v2t.find(v2)}"
		if(v2t.find(v1)!=v2t.find(v2))
			connect(v1,v2,v2t)
			num_rest -= 1
			w_tree += w
		end
	}
	return w_tree
end

def mod_pow(a,n)
	mod = 1000000007
	if(n==1)
		return a
	end
	m = n/2
	mpa = mod_pow(a,m)
	mpa2 = (mpa * mpa) % mod
	if(n%2 != 0)
		 mpa2 = (mpa2 * a) % mod
	end
	mpa2
end
def f(x,d_min,num_edge)
	num = d_min.inject(0){|sum,n| n <= x ? sum+1 : sum}
	dputs "num is #{num}"
	if(num == 0)
		mod_pow(2, num_edge)
	else
		mod_pow(2,num_edge-num+1)
	end
end

N,M=gets.chomp.split(' ').map{|n| n.to_i}
X,d=gets.chomp.split(' ').map{|n| n.to_i}
g = Array.new(M)
0.upto(M-1){|d|
	g[d]=gets.chomp.split(' ').map{|n| n.to_i}
}
g = one2zero(g)
g = g.sort{|e1,e2| e1[w_i] <=> e2[w_i]}

d_min = Array.new(M)
g.each_with_index{|e,i|
	d_min[i] = kraskal(g,N,[e])
}
dputs "dmin is "
dpp d_min

puts "#{f(X-1,d_min,M) - f(X,d_min,M)}"