Inspired by the tv series Stranger Things, bear Limak is going for a walk between two mirror worlds.

There are two perfect binary trees of height H, each with the standard numeration of vertices from 1 to 2^H-1. The root is 1 and the children of x are 2 \cdot x and 2 \cdot x + 1.

Let L denote the number of leaves in a single tree, L = 2^{H-1}.

You are given a permutation P_1, P_2, \ldots, P_L of numbers 1 through L. It describes L special edges that connect leaves of the two trees. There is a special edge between vertex L+i-1 in the first tree and vertex L+P_i-1 in the second tree.

graph for sample1

drawing for the first sample test, permutation P = (2, 3, 1, 4), special edges in green

Let's define product of a cycle as the product of numbers in its vertices. Compute the sum of products of all simple cycles that have exactly two special edges, modulo (10^9+7).

A simple cycle is a cycle of length at least 3, without repeated vertices or edges.

Constraints

* 2 \leq H \leq 18
* 1 \leq P_i \leq L where L = 2^{H-1}
* P_i \neq P_j (so this is a permutation)

Input

Input is given from Standard Input in the following format (where L = 2^{H-1}).


H
P_1 P_2 \cdots P_L


Output

Compute the sum of products of simple cycles that have exactly two special edges. Print the answer modulo (10^9+7).

Examples

Input

3
2 3 1 4


Output

121788


Input

2
1 2


Output

36


Input

5
6 14 15 7 12 16 5 4 11 9 3 10 8 2 13 1


Output

10199246
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 20, M = 1000000007;

int ans;

int n;
int a[(1 << N)];

int u[(1 << N)];

void dfs1(int x, int d, bool z, int p)
{
    if (!z)
    {
        if (d < n)
        {
            dfs1(x * 2, d + 1, z, (p * 1LL * x) % M);
            dfs1(x * 2 + 1, d + 1, z, (p * 1LL * x) % M);
        }
        else
        {
            dfs1((1 << (n - 1)) - 1 + a[x - (1 << (n - 1))], n, true, (p * 1LL * x) % M);
        }
    }
    else
    {
        u[x] = (u[x] + (p * 1LL * x) % M) % M;
        if (d > 1)
            dfs1(x / 2, d - 1, z, (p * 1LL * x) % M);
    }
}

void dfs2(int x, int d, bool z, int p, int s)
{
    if (!z)
    {
        if (d < n)
        {
            dfs2(x * 2, d + 1, z, (p * 1LL * x) % M, s);
            dfs2(x * 2 + 1, d + 1, z, (p * 1LL * x) % M, s);
        }
        else
        {
            dfs2((1 << (n - 1)) - 1 + a[x - (1 << (n - 1))], n, true, (p * 1LL * x) % M, s);
        }
    }
    else
    {
        ans = (ans + p * 1LL * u[x]) % M;
        ans = (ans - p * 1LL * s) % M;
        ans = (ans + M) % M;
        if (d > 1)
            dfs2(x / 2, d - 1, z, (p * 1LL * x) % M, ((u[x]) * 1LL * (x / 2)) % M);
    }
}

void dfs3(int x, int d, bool z, int p)
{
    if (!z)
    {
        if (d < n)
        {
            dfs3(x * 2, d + 1, z, (p * 1LL * x) % M);
            dfs3(x * 2 + 1, d + 1, z, (p * 1LL * x) % M);
        }
        else
        {
            dfs3((1 << (n - 1)) - 1 + a[x - (1 << (n - 1))], n, true, (p * 1LL * x) % M);
        }
    }
    else
    {
        u[x] = 0;
        if (d > 1)
            dfs3(x / 2, d - 1, z, (p * 1LL * x) % M);
    }
}

void dfs(int x, int d)
{
    if (d == n)
        return;
    dfs1(x * 2, d + 1, false, x);
    dfs2(x * 2 + 1, d + 1, false, 1, 0);
    dfs3(x * 2, d + 1, false, x);
    dfs(x * 2, d + 1);
    dfs(x * 2 + 1, d + 1);
}

void solv()
{
    scanf("%d", &n);
    for (int i = 0; i < (1 << (n - 1)); ++i)
        scanf("%d", &a[i]);
    dfs(1, 1);
    printf("%d\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
