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;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<unsigned int mod_>
struct ModInt{
	using uint = unsigned int;
	using ll = long long;
	using ull = unsigned long long;

	constexpr static uint mod = mod_;

	uint v;
	ModInt():v(0){}
	ModInt(ll _v):v(normS(_v%mod+mod)){}
	explicit operator bool() const {return v!=0;}
	static uint normS(const uint &x){return (x<mod)?x:x-mod;}		// [0 , 2*mod-1] -> [0 , mod-1]
	static ModInt make(const uint &x){ModInt m; m.v=x; return m;}
	ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}
	ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}
	ModInt operator-() const { return make(normS(mod-v)); }
	ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}
	ModInt operator/(const ModInt& b) const { return *this*b.inv();}
	ModInt& operator+=(const ModInt& b){ return *this=*this+b;}
	ModInt& operator-=(const ModInt& b){ return *this=*this-b;}
	ModInt& operator*=(const ModInt& b){ return *this=*this*b;}
	ModInt& operator/=(const ModInt& b){ return *this=*this/b;}
	ModInt& operator++(int){ return *this=*this+1;}
	ModInt& operator--(int){ return *this=*this-1;}
	ll extgcd(ll a,ll b,ll &x,ll &y) const{
		ll p[]={a,1,0},q[]={b,0,1};
		while(*q){
			ll t=*p/ *q;
			rep(i,3) swap(p[i]-=t*q[i],q[i]);
		}
		if(p[0]<0) rep(i,3) p[i]=-p[i];
		x=p[1],y=p[2];
		return p[0];
	}
	ModInt inv() const {
		ll x,y;
		extgcd(v,mod,x,y);
		return make(normS(x+mod));
	}
	ModInt pow(ll p) const {
		if(p<0) return inv().pow(-p);
		ModInt a = 1;
		ModInt x = *this;
		while(p){
			if(p&1) a *= x;
			x *= x;
			p >>= 1;
		}
		return a;
	}
	bool operator==(const ModInt& b) const { return v==b.v;}
	bool operator!=(const ModInt& b) const { return v!=b.v;}
	friend istream& operator>>(istream &o,ModInt& x){
		ll tmp;
		o>>tmp;
		x=ModInt(tmp);
		return o;
	}
	friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;}
};
using mint = ModInt<1000000007>;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);
	using P = pair<int,int>;

	int H; cin >> H;
	int N = 1<<(H-1);
	V<int> Y(N); rep(i,N) cin >> Y[i],Y[i]--;
//	V<int> Y(N); iota(all(Y),0); random_shuffle(all(Y));

	V<mint> C(N*2);
	C[1] = 1;
	for(int v=2;v<N*2;v++) C[v] = C[v/2] * v;

	V<mint> y2val(N);
	rep(i,N){
		y2val[Y[i]] = C[i+N] * C[Y[i]+N];
	}
	V<mint> coef(N+N);
	rep1(i,N*2-1){
		coef[i] = i;
		ll v = i;
		while(v != 1){
			coef[i] /= v*v;
			v /= 2;
		}
	}
	show(C);
	show(y2val);
	show(coef);
	V<P> ps;

	function<mint(int,int,int,int,int,int)> solve2 = [&](int d,int u,int w,int di,int ui,int v){
		if(u-d == 1 || ui-di <= 1) return mint(0);
		shows(d,u,w,ps,v);
		int m = (d+u)/2;
		mint e,f,g,h;
		int mi = di;
		for(int i=di;i<ui;i++){
			auto p = ps[i];
			if(p.fs < m){
				if(p.sc) e += y2val[p.fs];
				else f += y2val[p.fs];
				mi=i+1;
			}else{
				if(p.sc) g += y2val[p.fs];
				else h += y2val[p.fs];
			}
		}
		mint res = (e*h+f*g) * coef[v] * coef[w];
		res += solve2(d,m,w*2,di,mi,v) + solve2(m,u,w*2+1,mi,ui,v);
		return res;
	};
	function<mint(int,int,int)> solve = [&](int l,int r,int v){
		if(r-l == 1) return mint(0);
		int m = (l+r)/2;
		{
			ps.clear();
			for(int i=l;i<r;i++){
				if(i<m) ps.pb(Y[i],0);
				else ps.pb(Y[i],1);
			}
			sort(all(ps));
		}
		mint res = solve2(0,N,1,0,si(ps),v);
		res += solve(l,m,v*2) + solve(m,r,v*2+1);
		return res;
	};

	auto brute = [&](){
		mint res = 0;
		auto f = [&](int i,int j){
			int v = i+N, u = j+N;
			while(v != u) v /= 2, u /= 2;
			shows(i,j,v,coef[v]);
			return C[i+N] * C[j+N] * coef[v];
		};
		auto g = [&](int i,int j){
			mint res = 1;
			i+=N,j+=N;
			while(i != j){
				res *= i; res *= j;

			}
		};
		rep(i,N) rep(j,i){
			res += f(i,j) * f(Y[i],Y[j]);
		}
		return res;
	};
	cout << solve(0,N,1) << endl;
	// show(solve(0,N,1));
	// show(brute());
	// assert(brute() == solve(0,N,1));
}
