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
//Arayi
//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int> 
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
    return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
    return (a * (a + 1LL)) / 2;
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 1, -1, 0, 0, -1, -1, 1, 1, 0 };
int dy[] = { 0, 0, 1, -1, -1, 1, -1, 1, 0 };


const int N = 4e5 + 30;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const int T = 238;
const ld e = 1e-13;

lli bp(lli a, lli b = mod - 2LL)
{
    lli ret = 1;
    while (b)
    {
        if (b & 1) ret *= a, ret %= mod;
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return ret;
}
ostream& operator<<(ostream& c, pir a)
{
    c << a.fr << " " << a.sc;
    return c;
}
struct pt
{
    lli x, y;
    int ind;
    int bl;
    bool operator==(const pt& a)
    {
        return ind == a.ind;
    }
    bool operator<(const pt& b)
    {
        if (x == b.x) return y < b.y;
        return x < b.x;
    }
};


int n;
int p[N];
lli col[N], pat;
vector < pair<int, lli> > a[N];
int main()
{
    fastio;
    cin >> n;
    for (int i = 0; i < (1 << (n - 1)); i++)
    {
        cin >> p[i];
        p[i]--;
    }
    for (lli i = (1LL << (n - 1)); i < (1LL << n); i++)
    {
        a[i].ad(MP(i - (1LL << (n - 1)), i));
        a[i / 2].ad(MP(i - (1LL << (n - 1)), (i * (i / 2LL)) % mod));
    }
    for (lli i = (1LL << (n - 1)) - 1; i > 0; i--)
    {
        for (auto p : a[i])
            a[i / 2].ad(MP(p.fr, (p.sc * (i / 2LL)) % mod));
    }
    for (lli i = 1; i < (1LL << (n - 1)); i++)
    {
        for (auto p1 : a[i + i])
        {
            lli sm = (p1.sc * i) % mod, i1 = p[p1.fr] + (1LL << (n - 1));
            //cout << i1 << " ";
            while (i1)
            {
                sm *= i1;
                sm %= mod;
                col[i1] += sm;
                col[i1] %= mod;
                i1 /= 2LL;
            }
        }
        for (auto p1 : a[i + i + 1])
        {
            lli sm = p1.sc, i1 = p[p1.fr] + (1LL << (n - 1)), nax = 0LL;
            while (i1)
            {
                lli sm1 = col[i1] - (col[nax] * i1) % mod;
                sm1 %= mod;
                if (sm1 < 0) sm1 += mod;
                pat += sm1 * sm;
                sm *= i1;
                sm %= mod;
                pat %= mod;
                nax = i1;
                i1 /= 2LL;
            }
        }
        for (auto p1 : a[i + i])
        {
            lli i1 = p[p1.fr] + (1LL << (n - 1));
            while (i1)
            {
                col[i1] = 0;
                i1 /= 2LL;
            }
        }
    }
    cout << pat << endl;
    return 0;
}

/*

  __
*(><)*
  \/ /
  ||/
--||
  ||
  /\
 /  \
/    \
*/