//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace __gnu_pbds;
using namespace std;
 
template <typename T>
using ordered_set =
	tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
#define shandom_ruffle random_shuffle
 
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;

const int mod = 1e9 + 7;

int PowMod(int a, int n) {
	int r = 1;
	while (n) {
		if (n & 1) { r = (ll)r * a % mod; }
		n >>= 1;
		a = (ll)a * a % mod;
	}
	return r;
}

int InvMod(int a) {
	return PowMod(a, mod - 2);
}

inline int Add(int a, int b) {
	return (a + b) % mod;
}
inline int Sub(int a, int b) {
	return (a - b + mod) % mod;
}
inline int Mul(int a, int b) {
	return ((ll)a * b) % mod;
}

struct Node {
	int sum = 0;
	int val = 0;
	Node *l = nullptr, *r = nullptr;
};

int ans = 0;
int coef;

Node *Merge(Node *A, Node *B) {
	debug() << "Merge" << imie(A) << imie(B);
	if (!A || !B) { return A ? A : B; }
	#ifdef LOCAL
	debug() << imie(A->val) << imie(A->sum) << imie(B->sum);
	debug() << imie(A->l) << imie(A->r) << imie(B->l) << imie(B->r);
	debug() << imie(InvMod(coef));
	#endif
	if (A->l && B->r) {
		ans = Add(ans,
			Mul(A->l->sum, Mul(B->r->sum, Mul(A->val, coef))));
	}
	if (A->r && B->l) {
		ans = Add(ans,
			Mul(A->r->sum, Mul(B->l->sum, Mul(A->val, coef))));
	}
	A->sum = Add(A->sum, B->sum);
	A->l = Merge(A->l, B->l);
	A->r = Merge(A->r, B->r);
	return A;
}

const int kMaxN = 1e6;

int H, N;
int perm[kMaxN];
Node *nodes[kMaxN];

void Run(int L, int R, int vert, int cur_coef) {
	if (L == R - 1) { return; }
	cur_coef = Mul(cur_coef, vert);
	const int mid = (L + R) / 2;
	Run(L, mid, vert * 2, Mul(cur_coef, vert));
	Run(mid, R, vert * 2 + 1, Mul(cur_coef, vert));
	coef = InvMod(cur_coef);
	nodes[vert] = Merge(nodes[vert * 2], nodes[vert * 2 + 1]);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);

	cin >> H;
	--H;
	N = (1 << H);
	for (int i = 0; i < N; ++i) {
		cin >> perm[i];
		--perm[i];
	}

	for (int leaf_id = 0; leaf_id < N; ++leaf_id) {
		const int L = leaf_id;
		const int R = perm[L];
		//~ int bit = 1;

		int lcopy = L + N;
		int weight_l = 1;
		while (lcopy) {
			weight_l = Mul(weight_l, lcopy);
			lcopy /= 2;
		}
		debug() << imie(leaf_id) << imie(weight_l);
		lcopy = weight_l;

		Node *node = new Node();
		int rcopy = R + N;
		lcopy = Mul(lcopy, rcopy);
		node->sum = lcopy;
		node->val = rcopy;

		for (int i = 1; i <= H; ++i) {
			Node *new_node = new Node();
			const bool is_right = (rcopy % 2 == 1);
			rcopy /= 2;
			lcopy = Mul(lcopy, rcopy);
			new_node->sum = lcopy;
			new_node->val = rcopy;
			(is_right ? new_node->r : new_node->l) = node;
			node = new_node;
		}


		nodes[leaf_id + N] = node;
	}

	Run(0, N, 1, 1);
	cout << ans << "\n";
}


Measured memory_bytes: 79860