Xoring Ninja
Difficulty: Hard

An [XOR](https://en.wikipedia.org/wiki/Exclusive_or) operation on a list is defined here as the *xor* ($\oplus$) of all its elements (e.g.: $XOR(\{A, B, C\}) = A \oplus B \oplus C$). 

The $XorSum$ of set $arr$ is defined here as the sum of the $XOR$s of all non-empty subsets of $arr$ known as $arr'$. The set $arr'$ can be expressed as:

$\begin{eqnarray} XorSum(arr) = \sum_{i = 1}^{2^n-1} XOR(arr'_i) = XOR(arr'_1) + XOR(arr'_2) + \dots + XOR(arr'_{2^n-2}) + XOR(arr'_{2^n-1}) \end{eqnarray}$	 

**For example:** Given set $arr = \{n_1, n_2, n_3\}$

- The set of possible non-empty subsets is: $arr' = \{\{n_1\}, \{n_2\}, \{n_3\}, \{n_1, n_2\}, \{n_1, n_3\}, \{n_2, n_3\}, \{n_1, n_2, n_3\}\}$ 	

- The $XorSum$ of these non-empty subsets is then calculated as follows: 	
$XorSum(arr)$ = $ n_1 + n_2 + n_3 + (n_1 \oplus n_2) + (n_1 \oplus n_3) + (n_2 \oplus n_3) + (n_1 \oplus n_2 \oplus n_3)$

Given a list of $n$ space-separated integers, determine and print $XorSum\ \%\ (10^9+7)$.    

For example, $arr = \{3,4\}$.  There are three possible subsets, $arr' = \{\{3\},\{4\},\{3,4\}\}$.  The XOR of $arr'[1] = 3$, of $arr'[2] = 4$ and of $arr[3] = 3 \oplus 4 = 7$.  The XorSum is the sum of these: $3 + 4 + 7 = 14$ and $14\ \%\ (10^9+7) = 14$.  

**Note:** The cardinality of [powerset](https://en.wikipedia.org/wiki/Power_set)$(n)$ is $2^n$, so the set of non-empty subsets of set $arr$ of size $n$ contains $2^n-1$ subsets.

**Function Description**  

Complete the *xoringNinja* function in the editor below.  It should return an integer that represents the XorSum of the input array, modulo $(10^9+7)$.  

xoringNinja has the following parameter(s):  

- *arr*: an integer array

Input Format:
The first line contains an integer $T$, the number of test cases.		

Each test case consists of two lines:  
-  The first line contains an integer $n$, the size of the set $arr$.   
-  The second line contains $n$ space-separated integers $arr[i]$.  


Output Format:
For each test case, print its $XorSum\ \%\ (10^9+7)$ on a new line.  The $i^{th}$ line should contain the output for the $i^{th}$ test case.

Sample Input:
    1
    3
    1 2 3

Sample Output:
12

Python 3 starter code:
#
# Complete the 'xoringNinja' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def xoringNinja(arr):
    # Write your code here



Editorial: Xoring Ninja 2020 Hack March Editorial
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
.MathJax_SVG_LineBox {display: table!important}
.MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p>Description: We have to find the sum of all subsets in which elements of each subset are XORed together. Fint it <a href="https://www.hackerrank.com/contests/mar14/challenges/xoring-ninja">here</a>.</p>

<p><strong>Problem Tester</strong> : <a href="https://www.hackerrank.com/shashank21j">Shashank Sharma</a></p>

<p><strong>Problem Editorialist</strong> : Tusshar Singh</p>

<p><strong>Time Complexity</strong> : O(N) per test cases.</p>

<p>Solution:  Find OR of all the elements and multiply it with 2^(n-1) where n is the total number of elements. This gives us the answer.</p>

<p>Idea: </p>

<p>There will be total 2^n subsets.<br>
If ith bit of any element is set, then that bit will be set in xor of half of the total subsets which is 2^(n-1).<br>
To find out all the set bits in all the numbers we find OR of all the numbers.<br>
Since each set bit appears in half of the total subsets we multiply OR of all numbers with 2^n-1 to get the final result.</p>

<p>Code:</p>

<pre><code>#include &lt;cmath&gt;
#include &lt;cstdio&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
using namespace std;

#define MOD 1000000007

long long int power(long long int n, long long int k)
{
  long long int result, pow;
  if(k==0)
    return 1;
  if(n==0)
    return 0;
  if(n==1)
    return 1;
  result=n%MOD;
  pow=1;
  while(pow*2&lt;=k)
  {
    pow=pow*2;
    result=(result*result);
    result=result%MOD;
  }

  if(k-pow!=0)
  result=result*power(n, k-pow);
  result=result%MOD;

  return result;
}

int main() {
  int t;
  cin&gt;&gt;t;
  while(t--)
    {
    int n;
    cin&gt;&gt;n;
    long long ans=0,temp;
    for(int i=0;i&lt;n;i++)
      {
      cin&gt;&gt;temp;
      ans|=temp;
      }
    ans*=power(2,n-1);
    ans%=MOD;
    cout&lt;&lt;ans&lt;&lt;"\n";
   }
    return 0;
}
</code></pre>

<p><strong>Tester's Code</strong> :</p>

<pre><code>#!/usr/bin
from math import *
t=int(raw_input())
assert(t&lt;=5)
assert(t&gt;=1)
for i in range(0,t):
    n=int(raw_input())
    assert(n&lt;=10**5)
    assert(n&gt;=1)
    a=[]
    b=[]
    for j in range(0,int(log(10**9,2)+1)):
        b=[0]*n
        a.append(b)
    number=map(int, raw_input().split())
    for j in range(0,n):
        z=number[j]
        assert(z&gt;=0)
        assert(z&lt;=10**9)
        k=0
        while(z&gt;0):
            a[k][j]=z&amp;1
            z&gt;&gt;=1
            k+=1
    sum1=0
    for j in range(0,int(log(10**9,2)+1)):
        od=0
        for k in range(0,n):
            if(a[j][k]==1):
                od=1
                break
        if od!=0:
            sum1=(sum1+ 2**(j+n-1))%1000000007
    print sum1
</code></pre>

Reference solution (C++):
###C++  
```cpp  
  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
    
#define MOD 1000000007
    
long long int power(long long int n, long long int k)
{
  long long int result, pow;
  if(k==0)
    return 1;
  if(n==0)
    return 0;
  if(n==1)
    return 1;
  result=n%MOD;
  pow=1;
  while(pow*2<=k)
  {
    pow=pow*2;
    result=(result*result);
    result=result%MOD;
  }
 
  if(k-pow!=0)
  result=result*power(n, k-pow);
  result=result%MOD;
 
  return result;
}
    
int main() {
  int t;
  cin>>t;
  while(t--)
    {
    int n;
    cin>>n;
    long long ans=0,temp;
    for(int i=0;i<n;i++)
      {
      cin>>temp;
      ans|=temp;
      }
    ans*=power(2,n-1);
    ans%=MOD;
    cout<<ans<<"\n";
   }
    return 0;
}
    
```