#include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define sz(x) (int)x.size() #define cat(x) cerr << #x << " = " << x << endl #define IOS cin.tie(0); ios_base::sync_with_stdio(0) using ll = long long; using namespace std; const int MOD = 1e9 + 7; const int N = 1 << 21; int n, q, cnt[N], sz[N], tab[N], a , b, c; int mul(int a, int b) { return ll(a) * b % MOD; } int add(int a, int b) { if (a + b < MOD) return a + b; return a + b - MOD; } int sub(int a, int b) { if (a - b >= 0) return a - b; return a - b + MOD; } int dis(int u, int v) { return abs(u - v); } void go(int start, int dis, int par) { int v = start; tab[dis] ^= 1; if (cnt[v] % 2 != par) return; do { ++v; tab[dis + 2 * (v - start)] ^= 1; } while (cnt[v] % 2 == 1); } int main() { scanf("%d%d", &n, &q); for (int i = 1; i < n; ++i) scanf("%d", cnt + i); for (int i = n; 1 <= i; --i) sz[i] = (1LL * sz[i + 1] * cnt[i] + 1) % MOD; while (q--) { scanf("%d%d%d", &a, &b, &c); int ans1 = 0, ans2 = 0; for (int i = 0; i <= 4 * n; ++i) tab[i] = 0; if (a == c || b == c) { int x = min(a, b), y = max(a, b); for (int i = x; i < y; ++i) ans1 = add(ans1, mul(sub(dis(i, a), dis(i, b)), sub(sz[i], sz[i + 1]))); ans1 = add(ans1, mul(sub(dis(y, a), dis(y, b)), sz[x])); ans1 = add(ans1, mul(sub(dis(x, a), dis(x, b)), sub(sz[1], sz[y]))); for (int i = 1; i <= y; ++i) { int D = dis(i, a) + dis(i, b); tab[D] ^= 1; if (i < y && cnt[i] % 2 == 0) { int u = i; do { ++u; tab[D + 2 * dis(u, i)] ^= 1; } while (cnt[u] % 2 == 1); } } int u = y; if (cnt[u] % 2 == 1) { do { ++u; tab[dis(u, a) + dis(u, b)] ^= 1; } while (cnt[u] % 2 == 1); } } else { int disA = a - c; int disB = b - c; ans1 = add(ans1, mul(sz[a], sub(0, add(disA, disB)))); ans1 = add(ans1, mul(sz[b], add(disA, disB))); ans1 = add(ans1, mul(sub(sz[1], sz[c]), sub(disA, disB))); for (int i = c + 1; i < a; ++i) { int val = sub(a - i, disB + i - c); ans1 = add(ans1, mul(sub(sz[i], sz[i + 1]), val)); } for (int i = c + 1; i < b; ++i) { int val = sub(disA + i - c, b - i); ans1 = add(ans1, mul(sub(sz[i], sz[i + 1]), val)); } ans1 = add(ans1, mul(sub(sz[c], add(sz[c + 1], sz[c + 1])), sub(disA, disB))); for (int i = c + 1; i < a; ++i) go(i, disA + disB, 0); for (int i = c + 1; i < b; ++i) go(i, disA + disB, 0); go(a, disA + disB, 1); go(b, disA + disB, 1); go(c, disA + disB, 1); for (int i = c - 1; 1 <= i; --i) go(i, disA + disB + 2 * (c - i), 0); } int turn = 0; for (int i = 4 * n; 1 <= i; --i) { if (!tab[i]) continue; if (!turn) ans2 = add(ans2, i); else ans2 = sub(ans2, i); turn ^= 1; } printf("%lld\n", 1LL * add(ans1, ans2) * (MOD + 1) / 2 % MOD); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define sz(x) (int)x.size() #define cat(x) cerr << #x << " = " << x << endl #define IOS cin.tie(0); ios_base::sync_with_stdio(0) using ll = long long; using namespace std; const int MOD = 1e9 + 7; const int N = 1 << 21; int n, q, cnt[N], sz[N], tab[N], a , b, c; int mul(int a, int b) { return ll(a) * b % MOD; } int add(int a, int b) { if (a + b < MOD) return a + b; return a + b - MOD; } int sub(int a, int b) { if (a - b >= 0) return a - b; return a - b + MOD; } int dis(int u, int v) { return abs(u - v); } void go(int start, int dis, int par) { int v = start; tab[dis] ^= 1; if (cnt[v] % 2 != par) return; do { ++v; tab[dis + 2 * (v - start)] ^= 1; } while (cnt[v] % 2 == 1); } int main() { scanf("%d%d", &n, &q); for (int i = 1; i < n; ++i) scanf("%d", cnt + i); for (int i = n; 1 <= i; --i) sz[i] = (1LL * sz[i + 1] * cnt[i] + 1) % MOD; while (q--) { scanf("%d%d%d", &a, &b, &c); int ans1 = 0, ans2 = 0; for (int i = 0; i <= 4 * n; ++i) tab[i] = 0; if (a == c || b == c) { int x = min(a, b), y = max(a, b); for (int i = x; i < y; ++i) ans1 = add(ans1, mul(sub(dis(i, a), dis(i, b)), sub(sz[i], sz[i + 1]))); ans1 = add(ans1, mul(sub(dis(y, a), dis(y, b)), sz[x])); ans1 = add(ans1, mul(sub(dis(x, a), dis(x, b)), sub(sz[1], sz[y]))); for (int i = 1; i <= y; ++i) { int D = dis(i, a) + dis(i, b); tab[D] ^= 1; if (i < y && cnt[i] % 2 == 0) { int u = i; do { ++u; tab[D + 2 * dis(u, i)] ^= 1; } while (cnt[u] % 2 == 1); } } int u = y; if (cnt[u] % 2 == 1) { do { ++u; tab[dis(u, a) + dis(u, b)] ^= 1; } while (cnt[u] % 2 == 1); } } else { int disA = a - c; int disB = b - c; ans1 = add(ans1, mul(sz[a], sub(0, add(disA, disB)))); ans1 = add(ans1, mul(sz[b], add(disA, disB))); ans1 = add(ans1, mul(sub(sz[1], sz[c]), sub(disA, disB))); for (int i = c + 1; i < a; ++i) { int val = sub(a - i, disB + i - c); ans1 = add(ans1, mul(sub(sz[i], sz[i + 1]), val)); } for (int i = c + 1; i < b; ++i) { int val = sub(disA + i - c, b - i); ans1 = add(ans1, mul(sub(sz[i], sz[i + 1]), val)); } ans1 = add(ans1, mul(sub(sz[c], add(sz[c + 1], sz[c + 1])), sub(disA, disB))); for (int i = c + 1; i < a; ++i) go(i, disA + disB, 0); for (int i = c + 1; i < b; ++i) go(i, disA + disB, 0); go(a, disA + disB, 1); go(b, disA + disB, 1); go(c, disA + disB, 1); for (int i = c - 1; 1 <= i; --i) go(i, disA + disB + 2 * (c - i), 0); } int turn = 0; for (int i = 4 * n; 1 <= i; --i) { if (!tab[i]) continue; if (!turn) ans2 = add(ans2, i); else ans2 = sub(ans2, i); turn ^= 1; } printf("%lld\n", 1LL * add(ans1, ans2) * (MOD + 1) / 2 % MOD); } return 0; } |