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 ll long long
#define int ll
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 2e6+5;

int p[maxn];

struct nd{
    int p;
    ll v;
    bool l;
};

signed main(){
    IOS();

    int n; cin>>n;
    n = (1<<(n-1));
    for (int i = 0; i<n; ++i) cin>>p[i], --p[i];
    ll re = 0;
    for (int o = 1; o<n; ++o) {
        int L = o;
        ll nmo = 0;
        while (L * 2 < n+n) L*=2, nmo++;
        int R = ((o+1)<<(nmo))-1;
        bug(o,L,R);
        vector<nd> pos;
        for (int i = L; i<=R; ++i) {
            ll vv = 1;
            int tmp = i;
            while (tmp != o) (vv *= tmp) %= mod, tmp /= 2;
            pos.pb({p[i-n] + n, vv, i <= ((L+R)/2) } );
        }
        sort(ALL(pos),[&](nd a, nd b){return a.p!=b.p?a.p<b.p:a.l<b.l;});
        while (SZ(pos) > 1) {

            vector<nd> np;


            for (int i = 0; i<SZ(pos); ++i) {
                if (i && pos[i-1].p == pos[i].p) {
                    re -= pos[i-1].v * pos[i].v % mod * o % mod * pos[i].p % mod * pos[i].p % mod * (pos[i-1].p/2) % mod;
                }
            }
            sort(ALL(pos),[&](nd a, nd b){return (a.p/2)!=(b.p/2)?a.p<b.p:a.l<b.l;});
            for (int i = 0; i<SZ(pos); ++i) {
                nd x = pos[i];
                if (i && (np.back().p) == (x.p/2) && np.back().l == x.l) {
                    bug(i);
                    (np.back().v += x.v*x.p)%=mod;
                }else{
                    np.pb({x.p/2, x.v*x.p%mod, x.l});
                }
            }
//            sort(ALL(pos),[&](nd a, nd b){return a.p!=b.p?a.p<b.p:a.l<b.l;});
            for (int i = 0; i<SZ(np); ++i) {
                if (i && np[i].p == np[i-1].p) {
                    assert(np[i].l != np[i-1].l);
                    re += np[i].v * np[i-1].v % mod * o % mod * np[i].p % mod;
                }
            }
            if (SZ(np) <= 1 || (SZ(np) == 2 && np[0].p == np[1].p)) break;
            pos = np;

        }
    }
    re = (re % mod + mod) % mod;
    cout<<re%mod<<endl;
}
