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 <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <cstring>
#include <list>  
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXH = 18;
const int MOD = 1000000007;
void inc(int& a, int b) { if ((a += b) >= MOD) a -= MOD; }

int h;
int p[1 << (MAXH - 1)];

int lst[1 << MAXH], nlst;
int mark[1 << MAXH];
int sum[1 << MAXH][2][2];

void go(int at, int dep,int prod,int cmark,int atside) {
	prod = (ll)prod * at % MOD;
	if (dep + 1 < h) {
		go(2 * at + 0, dep + 1, prod, cmark, atside);
		go(2 * at + 1, dep + 1, prod, cmark, atside);
	} else {
		//printf("\tat=%d\n", at);
		at -= (1 << (h - 1));
		at = p[at];
		at += (1 << (h - 1));
		while (at != 1) {
			prod = (ll)prod * at % MOD;
			int to = at / 2, toside = at % 2;
			//printf("\t\tat=%d to=%d toside=%d prod=%d\n", at, to, toside, prod);
			if (mark[to] != cmark) mark[to] = cmark, sum[to][0][0] = sum[to][0][1] = sum[to][1][0] = sum[to][1][1] = 0, lst[nlst++] = to;
			inc(sum[to][atside][toside], prod);
			at = to;
		}
	}
}

int rec(int at,int dep) {
	if (dep + 1 >= h) return 0;
	int ret = 0;
	nlst = 0;
	go(2 * at + 0, dep + 1, 1, at, 0);
	go(2 * at + 1, dep + 1, 1, at, 1);
	REP(i, nlst) {
		int to = lst[i];
		REP(toside, 2) {
			int cur = (ll)sum[to][0][toside] * sum[to][1][1-toside] % MOD * at % MOD * to % MOD;
			//printf("at=%d to=%d toside=%d sum=%d/%d\n", at, to, toside, sum[to][0][toside], sum[to][1][1 - toside]);
			inc(ret, cur);
		}
	}
	inc(ret, rec(2 * at + 0, dep + 1));
	inc(ret, rec(2 * at + 1, dep + 1));
	return ret;
}

int solve() {
	REP(i, 1 << h) mark[i] = -1;
	return rec(1, 0);
}


void run() {
	scanf("%d", &h);
	REP(i, 1 << (h - 1)) scanf("%d", &p[i]), --p[i];
	printf("%d\n", solve());
}

void stress() {
	REP(rep, 1) {
		h = MAXH;
		REP(i, 1 << (h - 1)) p[i] = i;
		REP(i, 1 << (h - 1)) { int idx = rnd() % (i + 1); swap(p[i], p[idx]); }
		solve();
	}
}

int main() {
	run();
	//stress();
	return 0;
}
