You are given an undirected graph, constisting of n vertices and m edges. Each edge of the graph has some non-negative integer written on it.
Let's call a triple (u, v, s) interesting, if 1 ≤ u < v ≤ n and there is a path (possibly non-simple, i.e. it can visit the same vertices and edges multiple times) between vertices u and v such that xor of all numbers written on the edges of this path is equal to s. When we compute the value s for some path, each edge is counted in xor as many times, as it appear on this path. It's not hard to prove that there are finite number of such triples.
Calculate the sum over modulo 109 + 7 of the values of s over all interestingtriples.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ 200 000) — numbers of vertices and edges in the given graph.
The follow m lines contain three integers ui, vi and ti (1 ≤ ui, vi ≤ n, 0 ≤ ti ≤ 1018, ui ≠ vi) — vertices connected by the edge and integer written on it. It is guaranteed that graph doesn't contain self-loops and multiple edges.
Output
Print the single integer, equal to the described sum over modulo 109 + 7.
Examples
Input
4 4 1 2 1 1 3 2 2 3 3 3 4 1
Output
12
Input
4 4 1 2 1 2 3 2 3 4 4 4 1 8
Output
90
Input
8 6 1 2 2 2 3 1 2 4 4 4 5 5 4 6 3 7 8 5
Output
62
https://www.cnblogs.com/PinkRabbit/p/10326681.html
具体解释看上面这位巨佬的播客爬。。看湿了~;
#include<bits/stdc++.h>
using namespace std;
int n, m;
#define ll long long
const int mod = 1e9 + 7;
class BASE {
static const int DEG = 62;
public:
int tot = 0, n = 0, cnt = 0;
ll d[63], nd[63];
BASE() {
memset(d, 0, sizeof d);
tot = 0; n = 0; cnt = 0;
}
~BASE() {}
void Init() {
memset(d, 0, sizeof d);
tot = 0; n = 0; cnt = 0;
}
void Ins(ll x) {
n++;
for (int i = DEG; i >= 0; i--) {
if (!(x&(1ll << i)))continue;
if (!d[i]) { d[i] = x; cnt++; break; }
x ^= d[i];
}
}
ll Max(ll x) {
ll res = x;
for (int i = DEG; i >= 0; i--) {
res = max(res, res^d[i]);
}
return res;
}
ll Min() {
for (int i = 0; i <= DEG; i++)
if (d[i])
return d[i];
return 0;
}
void Rebuild() {
for (int i = DEG; i >= 0; i--) {
if (d[i] == 0)continue;
for (int j = i - 1; j >= 0; j--) {
if (d[j] == 0)continue;
if (d[i] & (1ll << j)) d[i] ^= d[j];
}
}
for (int i = 0; i <= DEG; i++)
if (d[i]) nd[tot++] = d[i];
}
ll Kth(ll k) {
if (k == 1ll && tot < n)return 0;
if (tot < n)k--;
if (k >= (1ll << tot)) return -1;
ll res = 0;
for (int i = DEG; i >= 0; i--)
if (k&(1ll << i))
res ^= nd[i];
return res;
}
void merge(BASE T) {//不一定对~~hh还没题目实践
for (int i = DEG; i >= 0; i--) {
if (T.d[i] == 0)continue;
Ins(T.d[i]);
}
}
};
BASE T;
struct edge {
int u, v; ll w;
};
vector<edge>G[100010]; ll vis[100010];
vector<int>q;
void dfs(int u, int fa) {
q.push_back(u);
for (auto v : G[u]) {
if (v.v == fa)continue;
if (vis[v.v] == -1) {
vis[v.v] = 1ll * vis[u] ^ v.w;
dfs(v.v, u);
}
else T.Ins(1ll * vis[u] ^ vis[v.v] ^ v.w);
}
}
ll ans = 0;
void cal() {
for (int j = 0; j < 60; ++j) {
ll c = (1ll << j) % mod;
bool ok = 0;
for (int k = 0; k < 60; ++k) if (T.d[k] >> j & 1) ok = 1;
int t = q.size(); int C = T.cnt;
if (ok) ans = (ans + (ll)t * (t - 1) / 2 % mod * ((1ll << C - 1) % mod) % mod * c) % mod;
else {
int x = 0;
for (auto v : q) if (vis[v] >> j & 1) ++x;
ans = (ans + (ll)x * (t - x) % mod * ((1ll << C) % mod) % mod * c) % mod;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i <= n; i++)vis[i] = -1;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back(edge{ a,b,c });
G[b].push_back(edge{ b,a,c });
}
for (int i = 1; i <= n; i++) {
if (vis[i] == -1) {
T.Init();
vis[i] = 0;
q.clear();
dfs(i, 0);
cal();
}
}
cout << ans << "\n";
return 0;
}