Guess Game
Contest: Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)
Difficulty rating: 2100
Tags: bitmasks, data structures, games, math, probabilities, sortings, strings, trees

Carol has a sequence $$$s$$$ of $$$n$$$ non-negative integers. She wants to play the "Guess Game" with Alice and Bob.

To play the game, Carol will randomly select two integer indices $$$i_a$$$ and $$$i_b$$$ within the range $$$[1, n]$$$, and set $$$a=s_{i_a}$$$, $$$b=s_{i_b}$$$. Please note that $$$i_a$$$ and $$$i_b$$$ may coincide.

Carol will tell:

- the value of $$$a$$$ to Alice;
- the value of $$$b$$$ to Bob;
- the value of $$$a \mid b$$$ to both Alice and Bob, where $$$|$$$ denotes the bitwise OR operation.

Please note that Carol will not tell any information about $$$s$$$ to either Alice or Bob.

Then the guessing starts. The two players take turns making guesses, with Alice starting first. The goal of both players is to establish which of the following is true: $$$a < b$$$, $$$a > b$$$, or $$$a = b$$$.

In their turn, a player can do one of the following two things:

- say "I don't know", and pass the turn to the other player;
- say "I know", followed by the answer "$$$a<b$$$", "$$$a>b$$$", or "$$$a=b$$$"; then the game ends.

Alice and Bob hear what each other says, and can use this information to deduce the answer. Both Alice and Bob are smart enough and only say "I know" when they are completely sure.

You need to calculate the expected value of the number of turns taken by players in the game. Output the answer modulo $$$998\,244\,353$$$.

Formally, let $$$M = 998\,244\,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$).

The second line of each testcase contains $$$n$$$ integers $$$s_1,s_2,\ldots, s_n$$$ ($$$0 \le s_i < 2^{30}$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, print a single integer — the answer to the problem modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first test case, there are only $$$4$$$ possible situations:

1. $$$i_a=1$$$, $$$i_b=1$$$, $$$a=2$$$, $$$b=2$$$, the number of turns is $$$2$$$;
2. $$$i_a=1$$$, $$$i_b=2$$$, $$$a=2$$$, $$$b=3$$$, the number of turns is $$$3$$$;
3. $$$i_a=2$$$, $$$i_b=1$$$, $$$a=3$$$, $$$b=2$$$, the number of turns is $$$2$$$;
4. $$$i_a=2$$$, $$$i_b=2$$$, $$$a=3$$$, $$$b=3$$$, the number of turns is $$$3$$$.

The expected number of turns is $$$\frac{2+3+2+3}{4}=\frac{5}{2}=499122179\pmod{998244353}$$$.

Consider the first case, when $$$a=2$$$, $$$b=2$$$. The guessing process is as follows.

In the first turn, Alice thinks, "I know that $$$a=2, a\mid b=2$$$. I can infer that $$$b=0$$$ or $$$b=2$$$, but I am not sure which one". Thus, she says, "I don't know".

In the second turn, Bob thinks, "I know that $$$b=2, a\mid b=2$$$. I can infer that $$$a=0$$$ or $$$a=2$$$. But if $$$a=0$$$, then Alice would have already said that $$$a<b$$$ in the first turn, but she hasn't. So $$$a=2$$$". Thus, he says, "I know, $$$a=b$$$". The game ends.

In the second test case, for $$$a=0$$$, $$$b=0$$$, Alice knows that $$$a=b$$$ immediately. The expected number of turns equals $$$1$$$.

Example input:
4
2
2 3
3
0 0 0
3
9 9 6
8
34124838 0 113193378 8 321939321 113193378 9463828 99

Example output:
499122179
1
332748120
77987843

Editorial:
First, let's analize a single game for fixed $$$a$$$, $$$b$$$, and how many turns it takes.
Consider the binary representation of $$$a \mid b$$$. We consider bits from highest to lowest. For bits with a value of $$$0$$$, we can ignore it because it firmly tells us that both bits of $$$a$$$ and $$$b$$$ are $$$0$$$. For convenience, we assume that all bits of $$$a \mid b$$$ are $$$1$$$.
In the first round, if the highest bit of $$$a$$$ is $$$0$$$, then Alice can immediately say that $$$a<b$$$. Otherwise, in the second round of the game, Bob knows that the highest bit of $$$a$$$ is not $$$0$$$. If the highest or the second highest bit of b is $$$0$$$, then Bob can immediately say that $$$a>b$$$. Otherwise, in the third round of the game, Alice knows that the highest and the second highest bits of $$$b$$$ are not $$$0$$$, and so on.
Consider only set bits in $$$a \mid b$$$. Let's enumerate these bits from highest to lowest. After some observation, we can conclude that:
• If $$$a<b$$$ and the $$$i$$$-th bit in $$$a$$$ is zero, then the answer is $$$i+1−(i \& 1)$$$;
• If $$$a=b$$$, then the answer is $$$k+1$$$, where $$$k$$$ is the number of set bits in $$$a \mid b$$$;
• If $$$a>b$$$ and the $$$i$$$-th bit in $$$b$$$ is zero, then the answer is $$$i+(i\&1)$$$.
Now that we have a brute force algorithm for $$$O (n^2 \log A)$$$, how can we optimize it?
We can build a bit trie and traverse all nodes. We can easily calculate the number of $$$1$$$s that pass from a node to the root node, as well as the number of numbers prefixed with it and followed by $$$0$$$ (or $$$1$$$). Use this information to calculate the answer.