#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N) : uf(N), sz(N, 1), nc(N)
	{
		for (int i = 0; i<N; ++i) uf[i] = i;
	}
	int size()
	{
		return nc;
	}
	int size(int a)
	{
		return sz[Find(a)];
	}
	int Find(int a)
	{
		return uf[a] == a ? a : uf[a] = Find(uf[a]);
	}
	// Semi-compression w.o. recursion.
	//{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; }
	bool Union(int a, int b)
	{
		a = Find(a);
		b = Find(b);
		if (a != b)
		{
			if (sz[a] >= sz[b]) swap(a, b);
			uf[a] = b;
			sz[b] += sz[a];
			--nc;
		}
		return (a != b);
	}
};

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint() :val(0) {}
	mint(int      x) :val(x%MODVAL) {}
	mint(unsigned x) :val(x%MODVAL) {}
	mint(LL       x) :val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val + y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x += y; }
mint operator-(mint x, mint y) { return x -= y; }
mint operator*(mint x, mint y) { return x *= y; }

mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; }

class TheSocialNetwork { public:
	int minimumCut(int n, int m, vector <int> u, vector <int> v, vector <int> l)
	{
		vector<tuple<int, int, int>> e;
		for (int i = 0; i < m; ++i)
			e.emplace_back(l[i], u[i]-1, v[i]-1);
		sort(e.begin(), e.end());

		vector<bool> use(m, false);
		for (int k = e.size() - 1; k >= 0; --k) {
			use[k] = true;
			if (is_connected(n, m, use, e))
				use[k] = false;
		}

		mint ans = 0;
		for (int i = 0; i < m; ++i) if (!use[i])
			ans += POW(2, get<0>(e[i]));
		return ans.val;
	}

	bool is_connected(int n, int m, const vector<bool>& use, const vector<tuple<int, int, int>>& e) {
		UnionFind uf(n);
		for (int i = 0; i < m; ++i) if (use[i])
			uf.Union(get<1>(e[i]), get<2>(e[i]));
		return uf.size() == 1;

	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TheSocialNetwork().minimumCut(n, m, u, v, l));}
int main(){

CASE(0)
	int n = 6; 
	int m = 6; 
	int u_[] = {1, 2, 3, 4, 5, 6}	;
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = {2, 3, 4, 5, 6, 1};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = {1, 7, 3, 4, 6, 12};
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = 10; 
END
CASE(1)
	int n = 5; 
	int m = 7; 
	int u_[] = {1, 1, 1, 2, 2, 3, 3};
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = {5, 3, 2, 5, 3, 5, 4};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = {1, 8, 2, 3, 4, 6, 9};
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = 28; 
END
CASE(2)
	int n = 7; 
	int m = 6; 
	int u_[] = {1, 1, 2, 2, 3, 3};
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = {2, 3, 4, 5, 6, 7};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = {7, 11, 6, 9, 20, 15};
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = 64; 
END
CASE(3)
	int n = 8; 
	int m = 11; 
	int u_[] = {1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 7};
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = {2, 8, 3, 5, 4, 6, 7, 5, 6, 8, 8};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = {2, 3, 1, 6, 11, 8, 9, 10, 7, 4, 5};
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = 12; 
END
CASE(4)
	int n = 13; 
	int m = 56; 
	int u_[] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12};
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = {3, 4, 5, 7, 9, 12, 13, 3, 5, 8, 9, 10, 12, 13, 5, 6, 8, 9, 10, 11, 12, 5, 6, 7, 9, 11, 13, 7, 8, 9, 11, 12, 7, 8, 9, 10, 13, 8, 9, 10, 11, 12, 13, 9, 11, 12, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = {82, 240, 395, 1041, 1165, 1274, 1540, 1650, 1904, 2306, 2508, 3162, 3380, 3637, 3778, 3913, 3971, 4101, 4148, 4218, 4394, 4434, 5107, 6147, 6280, 6337, 6461, 6490, 7056, 8024, 8373, 8924, 8961, 9058, 9304, 9359, 10899, 11049, 11090, 11174, 11269, 11356, 11547, 11808, 12566, 12591, 13322, 13447, 13667, 13672, 15013, 15319, 16153, 16447, 16454, 16470};
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = 504663883; 
END
/*
CASE(5)
	int n = ; 
	int m = ; 
	int u_[] = ;
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = ;
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = ;
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = ; 
END
CASE(6)
	int n = ; 
	int m = ; 
	int u_[] = ;
	  vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 
	int v_[] = ;
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int l_[] = ;
	  vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
