This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int Q = 1e5 + 10;
const int N = 1005;
const int B = 7;
int n, q, a[N], pw2[Q];
int one[B][N], both[B][B][N][N];
int add(int x, int y) {
	if (x + y >= mod) {
		return x + y - mod;
	}
	return x + y;
}
int mul(int x, int y) {
	return (long long) x * y % mod;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	pw2[0] = 1;
	for (int i = 1; i < Q; i++) {
		pw2[i] = add(pw2[i - 1], pw2[i - 1]);
	}
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cin >> q;
	int l, r, x;
	for (int i = 0; i < q; i++) {
		cin >> l >> r >> x;
		for (int b = 0; b < B; b++) {
			if (!(x & (1 << b))) continue;
			one[b][l]++;
			one[b][r + 1]--;
		}
		for (int b1 = 0; b1 < B; b1++) {
			if (!(x & (1 << b1))) continue;
			for (int b2 = 0; b2 < B; b2++) {
				if (!(x & (1 << b2))) continue;
				both[b1][b2][l][r]++;
			}
		}
	}
	for (int b = 0; b < B; b++) {
		for (int i = 1; i <= n; i++) {
			one[b][i] += one[b][i - 1];
		}
	}
	for (int b1 = 0; b1 < B; b1++) {
		for (int b2 = 0; b2 < B; b2++) {
			for (int len = n; len >= 2; len--) {
				for (int i = 1; i + len - 1 <= n; i++) {
					int j = i + len - 1;
					both[b1][b2][i][j - 1] += both[b1][b2][i][j];
					both[b1][b2][i + 1][j] += both[b1][b2][i][j];
					both[b1][b2][i + 1][j - 1] -= both[b1][b2][i][j];
				}
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = i; j <= n; j++) {
			int times = mul(i, n - j + 1);
			if (i != j) times = mul(times, 2);
			for (int b1 = 0; b1 < B; b1++) {
				for (int b2 = 0; b2 < B; b2++) {
					int cij = both[b1][b2][i][j];
					int coi = one[b1][i] - cij;
					int coj = one[b2][j] - cij;
					int cn = q - (cij + coi + coj);
					int be = cij ? pw2[cij - 1] : 1;
					int bo = cij ? pw2[cij - 1] : 0;
					int le = coi ? pw2[coi - 1] : 1;
					int lo = coi ? pw2[coi - 1] : 0;
					int re = coj ? pw2[coj - 1] : 1;
					int ro = coj ? pw2[coj - 1] : 0;
					int cont;
					if (!(a[i] & (1 << b1)) && !(a[j] & (1 << b2))) {
						cont = add(mul(bo, mul(le, re)), mul(be, mul(lo, ro)));
					}
					if ((a[i] & (1 << b1)) && !(a[j] & (1 << b2))) {
						cont = add(mul(be, mul(le, ro)), mul(bo, mul(lo, re)));
					}
					if (!(a[i] & (1 << b1)) && (a[j] & (1 << b2))) {
						cont = add(mul(be, mul(lo, re)), mul(bo, mul(le, ro)));
					}
					if ((a[i] & (1 << b1)) && (a[j] & (1 << b2))) {
						cont = add(mul(be, mul(le, re)), mul(bo, mul(lo, ro)));
					}
					cont = mul(cont, pw2[cn]);
					cont = mul(cont, mul(times, pw2[b1 + b2]));
					ans = add(ans, cont);
				}
			}
		}
	}
	cout << ans << '\n';
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |