#pragma GCC optimize(3)
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair < int, int >
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i, k, j) for(int i = (k); i >= (j); i--)
#define For(i, k, j) for(int i = (k); i <= (j); i++)
#define Foe(i, u) for(rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout)
#define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) {a = max(a, b);}
inline void ckmin(int &a, int b) {a = min(a, b);}
inline void mulmod(int &a, int b) {a = 1ll * a * b % P;}
inline void addmod(int &a, int b) {int t = a + b; a = (t >= P ? t - P : t); }
inline void delmod(int &a, int b) {int t = a - b; a = (t < 0 ? t + P : t); }
inline int ksm(int a, int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline int inv(int a) {return ksm(a, P-2);}

inline void printarray(int *a, int n) {For(i, 1, n) fprintf(stderr, "%d ", a[i]); fprintf(stderr, "\n");}
namespace FastIO {
    const int SIZE=1<<16; char buf[SIZE], obuf[SIZE], str[64]; int bi=SIZE, bn=SIZE, opt;
    int read(char *s) {
        while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
        int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
    }
    bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
    void write(int x) {
        if(!x) obuf[opt++] = '0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
        if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}
    }
    void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}}
    void Fflush() { if (opt) fwrite(obuf, 1, opt, stdout); opt=0;}
};
const int MAXN = (1 << 19) + 5;
int m, n, p[MAXN], res = 0, v[MAXN], iv[MAXN];
inline int mul(int x, int y) {
//	fprintf(stderr, "mul(%d, %d) = %d\n", x, y, 1ll * x * y % P);
	return 1ll * x * y % P;
}
#define ls (k << 1)
#define rs (k << 1 | 1)
#define mid ((l + r) >> 1)
#define fa (k >> 1)
void init(int k, int l, int r) {
	iv[k] = inv(v[k]); if(l == r) return;
	v[ls] = mul(v[k], ls); v[rs] = mul(v[k], rs);
	init(ls, l, mid); init(rs, mid+1, r);
}
int tmp[MAXN];
inline void add(int k, int val) {
	int st = k;
	while(k) addmod(tmp[k], mul(val, mul(v[st], iv[fa]))), k = fa;
//	cerr << st << ' ' << fa << ' ' << mul(v[st], iv[fa]) << endl;
}
inline void del(int k) {while(k) tmp[k] = 0, k = fa;}
inline int calc(int k) {
	int st = k, res = 0, lst = 0;
	while(k) {
		int tv = mul(v[st], iv[k]);  
		addmod(res, mul(tv, tmp[k] - lst + P) % P); lst = mul(tmp[k], fa);
//		cerr << lst << endl;
		k = fa;
	}
//	cerr << res << endl;
	return res;
}
void solve(int k, int l, int r) {
	if(l == r) return;
	For(i, l, mid) add(p[i] + (n >> 1) - 1, mul(v[i + (n >> 1) - 1], iv[fa]));
	For(i, mid+1, r) addmod(res, mul(calc(p[i] + (n >> 1) - 1), mul(v[i + (n >> 1) - 1], iv[k])));
	For(i, l, mid) del(p[i] + (n >> 1) - 1);
//	For(i, 1, n) assert(tmp[i] == 0);
	solve(ls, l, mid); solve(rs, mid+1, r);
}
signed main()
{
    #ifndef ONLINE_JUDGE
        file("pro");
    #endif
    cin >> m; n = (1 << m);
    For(i, 1, n >> 1) scanf("%d", &p[i]);
    iv[0] = 1; v[1] = 1; init(1, 1, n >> 1);
//    For(i, 1, n) cerr << v[i] << ' ';
    solve(1, 1, n >> 1);
    cout << res << endl;
    
    return FastIO::Fflush(), 0;
}
/*
Think twice :
mod ?
INF ?
n = 1 ?
long long ?
Fastio::Fflush() ?

*/


Measured memory_bytes: 10920