This page is still under construction.

Parts of this page are still being built. What you see may change.

Winning Segments

Time limit4sMemory limit256 MB

Summary
Given a permutation of 0..2^M-1, count the nonempty subarrays whose XOR can be made equal to 2^M-1 by one mandatory swap of two elements.
Level

Hard8 of 10

Topics
Bit manipulation, Prefix sum, Combinatorics, Array
Solved
No attempts yet

Problem

You are given an integer MM and an array AA of length 2M2^M that holds each of 0,1,2,…,2M−10, 1, 2, \dots, 2^M - 1 exactly once.

The computer picks one nonempty contiguous segment of AA. You then pick two different positions and swap the two numbers sitting there. The swap is mandatory, and the two positions may both lie inside the segment, both outside it, or one on each side. After the swap you win if the bitwise XOR of every number inside the segment the computer picked is exactly 2M−12^M - 1.

The computer has 2M(2M+1)2\frac{2^M(2^M+1)}{2} segments to pick from. Count how many of them let you win.

Input

The first line contains the integer MM (1≤M≤201 \le M \le 20).

The second line contains the 2M2^M numbers of AA, separated by spaces. They form a permutation of 0,1,2,…,2M−10, 1, 2, \dots, 2^M - 1.

Output

Print the number of segments that let you win, on one line.

Hint

In the first example, if the computer picks the segment 1 2 3, you win by swapping 0 and 3. In that example every segment except the whole array lets you win.

In the second example, if the computer picks the whole array 3 7 0 4 6 1 5 2, the XOR of the segment is 0 and swapping any two numbers leaves it at 0.

Examples3

  1. Example 1

    Input
    2
    0 1 2 3
    
    Expected output
    9
    
  2. Example 2

    Input
    3
    3 7 0 4 6 1 5 2
    
    Expected output
    33
    
  3. Example 3

    Input
    4
    13 0 15 12 4 8 7 3 11 14 6 10 1 5 9 2
    
    Expected output
    133