#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"vector" usingnamespace std; #define P 31011 #define MAXN 106 int n , m; intpower( int a , int x ){ int cur = a % P , ans = 1; while( x ) { if( x & 1 ) ans = ans * cur % P; cur = cur * cur % P , x >>= 1; } return ans; } structed { int u , v , w; } E[1006] , T[MAXN] ; int cn = 0; boolcmp( ed a , ed b ){ return a.w < b.w; } int fa[MAXN]; vector<int> w; intfind( int x ){ return x == fa[x] ? x : fa[x] = find( fa[x] ); } int to[MAXN]; #define pii pair<int,int> #define fi first #define se second #define mp make_pair intmain(){ cin >> n >> m; for( int i = 1 ; i <= m ; ++ i ) scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w); sort( E + 1 , E + 1 + m , cmp ); for( int i = 1 ; i <= n ; ++ i ) fa[i] = i; for( int i = 1 ; i <= m ; ++ i ) { int u = find( E[i].u ) , v = find( E[i].v ) ; if( u == v ) continue; fa[u] = v; T[++ cn] = E[i]; w.push_back( E[i].w ); } w.erase( unique( w.begin() , w.end() ) , w.end() ); vector<pii> eds; int ans = 1; for( int a : w ) { int cur = 0; int sz = 0; for( int i = 1 ; i <= n ; ++ i ) fa[i] = i; for( int i = 1 ; i <= cn ; ++ i ) if( T[i].w != a ) fa[find( T[i].u )] = find( T[i].v ); for( int i = 1 ; i <= n ; ++ i ) if( find( i ) == i ) to[i] = ++ sz; eds.clear(); for( int i = 1 ; i <= m ; ++ i ) { int u = find( E[i].u ) , v = find( E[i].v ); if( u == v || E[i].w != a ) continue; eds.emplace_back( mp( to[u] , to[v] ) ); } int S = eds.size(); for( int i = 0 ; i < ( 1 << S ) ; ++ i ) { int ok = 0; for( int j = 1 ; j <= sz ; ++ j ) fa[j] = j; for( int j = 0 ; j < S ; ++ j ) if( i & ( 1 << j ) ) { if( find( eds[j].fi ) != find( eds[j].se ) ) fa[find(eds[j].fi)] = find(eds[j].se); else { ok = 1; break; } } for( int j = 1 ; j <= sz ; ++ j ) if( find( j ) != find( 1 ) ) { ok = 1 ; break; } if( !ok ) ++ cur; } ans = ans * cur % P; } cout << ans << endl; }