#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int long long
#define fs first
#define sc second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define MP make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef FlameDragon
    freopen("in.txt", "r", stdin);
#endif
    int n;
    cin >> n;
    int k = 5;
    vector<vector<int>> a(n, vector<int>(k));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            cin >> a[i][j];
        }
    }
    const int INF = 1e9 + 123;
    int l = 0, r = INF;
    auto check = [&](int x) -> bool {
        vector<int> cnt(1 << k);
        for (int i = 0; i < n; i++) {
            int mask = 0;
            for (int j = 0; j < k; j++) {
                if (a[i][j] >= x) {
                    mask ^= 1 << j;
                }
            }
            cnt[mask] = 1;
        }
        for (int i = 0; i < (1 << k); i++) {
            for (int j = 0; j < (1 << k); j++) {
                for (int p = 0; p < (1 << k); p++) {
                    if (cnt[i] && cnt[j] && cnt[p] && (i | j | p) == ((1 << k) - 1)) {
                        return true;
                    }
                }
            }
        }
        return false;
    };
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (check(mid)) {
            l = mid;
        } else {
            r = mid;
        }
    }
    cout << l << '\n';
}

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define endl "\n"
#define pb push_back
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define read(v) for(auto &it:v) cin>>it;
const int inf = 1e18;
const int N   = 2e5 + 5;
const int mod = 1000000007;

int madd(int a, int b)
{
    return (a + b) % mod;
}
int msub(int a, int b)
{
    return (((a - b) % mod) + mod) % mod;
}
int mmul(int a, int b)
{
    return ((a % mod) * (b % mod)) % mod;
}
int mpow(int base, int exp)
{
    int res = 1;
    while (exp)
    {
        if (exp % 2 == 1)
        {
            res = (res * base) % mod;
        }
        exp >>= 1;
        base = (base * base) % mod;
    }
    return res;
}
int minv(int base)
{
    return mpow(base, mod - 2);
}
int mdiv(int a, int b)
{
    return mmul(a, minv(b));
}
int dp[N][2];
int n;
int a[N];
void test_case() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    dp[0][0] = 1;
    dp[0][1] = 0;

    dp[1][0] = 1;
    dp[1][1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i][1] = madd(dp[i - 1][0], dp[i - 1][1]);
        dp[i][0] = dp[i - 1][1];
    }
    int ans = madd(dp[n - 1][0], dp[n - 1][1]);
    ans = mmul(ans, a[1]);
    int good = madd(dp[n - 1][0], dp[n - 1][1]);
    // deb(ans)
    for (int i = 2; i <= n; i++) {

        int left = madd(dp[i - 2][0], dp[i - 2][1]);
        if (i - 2 == 0) {
            left = 1;
        }
        int right = madd(dp[n - i][0], dp[n - i][1]);
        if (n - i == 0) {
            right = 1;
        }
        // deb(left, right)
        ans = madd(ans, mmul(a[i], mmul(left, right)));

        ans = msub(ans, mmul(a[i], msub(good, mmul(left, right))));
    }
    cout << ans << endl;



}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int  t = 1;
    // cin >> t;
    while (t--)test_case();
    return 0;
}