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>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
const u32 P=1e9+7;
inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
u64 power(u64 a, int b) {
	u64 ans=1;
	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
	return ans;
}

struct Tree {
	u32 sum;
	Tree *ch[2];
};
const int N=1<<18;
int h, l, p[N];
u32 t[N], invt[N], res;
Tree nil[1], *root[N];
Tree *make(int lv, int pos, u32 v) {
	Tree *o=new Tree;
	o->sum=v;
	o->ch[0]=o->ch[1]=nil;
	if(lv>=0) o->ch[pos>>lv&1]=make(lv-1, pos, v);
	return o;
}
Tree *merge(int id, Tree *u, Tree *v) {
	if(v==nil) return u;
	if(u==nil) return v;
	Tree *w=new Tree;
	res=(res+(1llu*u->ch[0]->sum*v->ch[1]->sum+1llu*u->ch[1]->sum*v->ch[0]->sum)%P*invt[id]%P*invt[id/2])%P;
	w->sum=(u->sum+v->sum)%P;
	w->ch[0]=merge(id*2, u->ch[0], v->ch[0]);
	w->ch[1]=merge(id*2+1, u->ch[1], v->ch[1]);
	return w;
}
int main() {
	h=rd();
	l=1<<(h-1);
	t[0]=invt[0]=1;
	for(int i=1; i<2*l; ++i) {
		t[i]=1llu*t[i/2]*i%P;
		invt[i]=power(t[i], P-2);
	}
	for(int i=0; i<l; ++i) p[i]=rd()-1;
	for(int i=0; i<l; ++i) root[l+i]=make(h-2, p[i], 1llu*t[l+i]*t[l+p[i]]%P);
	u32 ans=0;
	for(int i=l; --i; ) {
		res=0;
		root[i]=merge(1, root[2*i], root[2*i+1]);
		ans=(ans+1llu*res*invt[i]%P*invt[i/2])%P;
	}
	printf("%u\n", ans);
	return 0;
}
