Think of every operation as being used either an even number of times (no net effect) or an odd number of times (used once), because XOR cancels in pairs. So the final result depends only on which unordered pairs $(i,j)$ you chose an odd number of times.
Let $x_{ij}\in\{0,1\}$ be that parity for each pair $i< j$. Then for each index $i$,
$$ b_i\ =\ \bigoplus_{j\ne i,\ x_{ij}=1} (a_i\oplus a_j). $$
Work over $\mathbb F_2$ (XOR as addition). It is convenient to package the parities into a symmetric $n\times n$ matrix $X$ over $\mathbb F_2$ with zero diagonal, where $X_{ij}=X_{ji}=x_{ij}$ for $i\ne j$.
A key rewrite: define another symmetric matrix $Y$ by
- $Y_{ij}=X_{ij}$ for $i\ne j$,
- $Y_{ii}=\sum_{j\ne i} X_{ij}$ (the degree parity of $i$).
Then every row sum of $Y$ is zero, so $Y\mathbf 1=0$ where $\mathbf 1=(1,1,\dots,1)^T$. Also one checks directly that the final vector $B$ can be written as
$$ B = Y A, $$
where $A$ is the $n\times 30$ binary matrix whose $i$-th row is the 30 bit vector of $a_i$, and $Y$ ranges over all symmetric matrices satisfying $Y\mathbf 1=0$. This gives a linear map (over $\mathbb F_2$) from the space of allowed $Y$ to the set of reachable $B$. Therefore the set of reachable $B$ is a linear subspace, and the answer equals $2^{\dim(\text{reachable})}$ modulo $998244353$.
So we only need that dimension.
- Dimension of the domain (all symmetric $Y$ with $Y\mathbf 1=0$)
For such a symmetric matrix, each diagonal entry is determined by the off diagonal entries via “row sum is 0”. The free choices are exactly the $\binom{n}{2}$ off diagonal bits, hence
$$ \dim(\text{domain})=\frac{n(n-1)}{2}. $$
- Dimension of the kernel
Let $W\subseteq \mathbb F_2^n$ be the span of the 30 column vectors of $A$ (each column is one bit position across all indices). The condition $YA=0$ is equivalent to $Yw=0$ for every $w\in W$. We also always have $Y\mathbf 1=0$. So define
$$ U = \text{span}(W \cup \{\mathbf 1\}), \quad d=\dim(U). $$
Then the kernel is exactly the set of symmetric matrices $Y$ whose nullspace contains $U$. By choosing a basis of $\mathbb F_2^n$ whose first $d$ vectors span $U$, every such $Y$ becomes a block matrix with the first $d$ rows and columns all zero, and an arbitrary symmetric $(n-d)\times(n-d)$ block. Therefore
$$ \dim(\ker)=\frac{(n-d)(n-d+1)}{2}. $$
- Rank and reachable dimension
By rank nullity,
$$ \dim(\text{reachable}) = \frac{n(n-1)}{2} - \frac{(n-d)(n-d+1)}{2} = \frac{(d-1)(2n-d)}{2}. $$
So the answer is $$ 2^{\frac{(d-1)(2n-d)}{2}} \bmod 998244353. $$
All that remains is to compute $d$.
How to compute $d$ in $O(31n)$
The vectors spanning $U$ are:
- the all ones vector $\mathbf 1$,
- the 30 bit columns of $A$.
Form a $31\times n$ binary matrix whose rows are exactly those 31 vectors. Its rank is $d$. Rank of rows equals rank of columns, and each column corresponds to one index $i$. The column is the 31 bit vector: $$ c_i = (1,\ \text{bit}_0(a_i),\ \text{bit}_1(a_i),\ \dots,\ \text{bit}_{29}(a_i)). $$ Encode it as a 31 bit integer $$ m_i = 1\ |\ (a_i \ll 1). $$ Then $d$ is the XOR linear rank of the set $\{m_i\}$ inside $\mathbb F_2^{31}$, which you can compute with a standard linear basis (Gaussian elimination on bits).
C++17 implementation
#include <bits/stdc++.h>
using namespace std;
static const long long MOD = 998244353;
long long mod_pow(long long a, long long e) {
long long r = 1 % MOD;
a %= MOD;
while (e > 0) {
if (e & 1) r = (r * a) % MOD;
a = (a * a) % MOD;
e >>= 1;
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
array<uint32_t, 31> basis{};
basis.fill(0);
for (int i = 0; i < n; i++) {
uint32_t a;
cin >> a;
uint32_t mask = 1u | (a << 1); // bit0 is the constant 1, bits1..30 are bits of a
uint32_t x = mask;
for (int b = 30; b >= 0; b--) {
if ((x >> b) & 1u) {
if (basis[b] == 0) {
basis[b] = x;
break;
} else {
x ^= basis[b];
}
}
}
}
int d = 0;
for (int b = 0; b <= 30; b++) if (basis[b]) d++;
long long nn = n;
long long dd = d;
long long exponent = (dd - 1) * (2 * nn - dd) / 2;
cout << mod_pow(2, exponent) << "\n";
return 0;
}
Time complexity is $O(31n)$, memory usage is $O(1)$ beyond the input.