#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
const int maxn=434567;
const int M=1000000007;
typedef long long ll;
typedef pair<int,ll> pi;
void add(ll &x,ll y){x+=y;if(x>=M)x-=M;}
int n,h,p[maxn];
vector<pi> v[2];
ll ans,cnt[2][maxn];
void ins(int x,ll val,int o){
    while (x){
        val=val*x%M;
        add(cnt[o][x],val);
        x>>=1;
    }
}
ll dfs2(int x){
    if (!cnt[0][x]||!cnt[1][x]) return 0;
    ll ret=(cnt[0][x*2]*cnt[1][x*2+1]+cnt[1][x*2]*cnt[0][x*2+1])%M*x%M;
    add(ret,dfs2(x*2));
    add(ret,dfs2(x*2+1));
    return ret;
}
void dfs(int x,ll val,int o){
    val=val*x%M;
    if (x>=n){
        v[o].pb((pi){p[x-n]+n,val});
        return;
    }
    dfs(x*2,val,o);
    dfs(x*2+1,val,o);
}
ll solve(int x){
    v[0].clear(); v[1].clear();
    dfs(x*2,1,0);
    dfs(x*2+1,1,1);
    for (auto x:v[0]) ins(x.F,x.S,0);
    for (auto x:v[1]) ins(x.F,x.S,1);
    ll ret=dfs2(1);
    for (auto x:v[0]) ins(x.F,M-x.S,0);
    for (auto x:v[1]) ins(x.F,M-x.S,1);
    return ret;
}
int main(){
    cin >> h; n=1<<(h-1);
    for (int i=0;i<n;i++){
        cin >> p[i];
        p[i]--;
    }
    for (int i=1;i<n;i++) ans=(ans+solve(i)*i)%M;
    cout << ans << endl;
    return 0;
}
