对着模板一顿乱敲(很无奈)
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;typedef long long ll;
const int maxn=105;
const int maxm=1005;struct node{int set[maxn];void init(int n){for(int i=0;i<=n;i++) set[i]=i;}int Find(int x){return x==set[x]?set[x]:set[x]=Find(set[x]);}int Union(int x,int y){int xx=Find(x);int yy=Find(y);if(xx==yy) return -1;set[xx]=yy;return 1;}
}a,b,c;struct Edge{int u,v,len;
}edge[maxm];
int edgenum;void Add(int u,int v,int len){Edge E={u,v,len};edge[++edgenum]=E;
}bool vis[maxn];
vector<int>g[maxn];
ll p[maxn][maxn],deg[maxn][maxn];bool cmp(Edge e1,Edge e2){return e1.len<e2.len;
}ll EDT(ll a[][maxn],int n,ll Mod){ll temp=1;ll t;for(int i=0;i<n;i++){for(int j=0;j<n;j++) a[i][j]%=Mod;}for(int i=1;i<n;i++){for(int j=i+1;j<n;j++){while(a[j][i]){t=a[i][i]/a[j][i];for(int k=i;k<n;k++){a[i][k]-=a[j][k]*t;a[i][k]%=Mod;}for(int k=i;k<n;k++) swap(a[i][k],a[j][k]);temp=-temp;}}temp=temp*a[i][i]%Mod;}return (temp+Mod)%Mod;
}ll cal_MST_count(int n,ll Mod){sort(edge+1,edge+edgenum+1,cmp);int pre=edge[1].len;ll ans=1;a.init(n);b.init(n);memset(vis,false,sizeof(vis));memset(deg,0,sizeof(deg));for(int i=0;i<=n;i++) g[i].clear();for(int t=1;t<=edgenum+1;t++){if(edge[t].len!=pre || t==edgenum+1){for(int i=1;i<=n;i++){if(vis[i]){int k=b.Find(i);g[k].push_back(i);vis[i]=0;}}for(int i=1;i<=n;i++){if(g[i].size()){memset(p,0,sizeof(p));for(int j=0;j<g[i].size();j++){for(int k=j+1;k<g[i].size();k++){int x=g[i][j];int y=g[i][k];p[j][k]=p[k][j]=-deg[x][y];p[j][j]+=deg[x][y];p[k][k]+=deg[x][y];}}ans=ans*EDT(p,g[i].size(),Mod)%Mod;for(int j=0;j<g[i].size();j++) a.set[g[i][j]]=i;}}memset(deg,0,sizeof(deg));for(int i=1;i<=n;i++){b.set[i]=a.Find(i);g[i].clear();}if(t==edgenum+1) break;pre=edge[t].len;}int x=a.Find(edge[t].u);int y=a.Find(edge[t].v);if(x==y) continue;vis[x]=vis[y]=1;b.Union(x,y);deg[x][y]++;deg[y][x]++; }if(!edgenum) return 0;for(int i=2;i<=n;i++){if(b.Find(i)!=b.Find(1)) return 0;}return ans;
}void init(){edgenum=0;
}int n,m,u,v,len;
ll Mod=31011;int main(){cin>>n>>m;init();while(m--){scanf("%d%d%d",&u,&v,&len);Add(u,v,len);}printf("%lld\n",cal_MST_count(n,Mod));
}