#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using P=pair<int,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)

int main(){
  int n;
  cin >> n;
  vector<vector<int>> a(n,vector<int>(5));
  vector<vector<int>> b(n,vector<int>(5));
  rep(i,n){
    rep(j,5) cin >> a[i][j];
  }
  rep(i,5) b[n-1][i]=n-1;
  for(int i=n-2;0<=i;i--){
    rep(j,5){
      if(a[i][j]<a[b[i+1][j]][j]) b[i][j]=b[i+1][j];
      else b[i][j]=i;
    }
  }
  
  int ans=0;
  rep(i,n-2){
    for(int j=i+1;j<n-1;j++){
      rep(l,5){//もう1人を選ぶ時に見る能力
        vector<int> c(5);
        rep(k,5){
          c[k]=max(a[i][k],a[j][k]);
          c[k]=max(c[k],a[b[j+1][l]][k]);
        }
        int m=1e9+10;
        rep(k,5) m=min(m,c[k]);
        ans=max(ans,m);
      }
    }
  }
  
  cout << ans << endl;
  
}
#ifndef DEBUG
    #define NDEBUG
#endif
#include <bits/stdc++.h>
#define def(v) int &v = e[i].v
#define cls(a) memset (a, 0, sizeof (a))
#define mset(a,b) memset (a, b, sizeof (a))
#define go(i,e,u,...) for (int i = (u), ##__VA_ARGS__; i; i = e[i].nt)
#define fir(i,a,b,...) for (int i = (a), ##__VA_ARGS__; i <= (b); ++i)
#define firr(i,a,b,...) for (int i = (a), ##__VA_ARGS__; i >= (b); --i)
#ifdef DEBUG
    #define debug(format,...) (void)fprintf (stderr, "debug in %d: " format "\n", __LINE__, ##__VA_ARGS__)
    #define dbug(x) (void)(std::cerr << #x " = " << (x) << std::endl)
#else
    #define debug(...) (void)0
    #define dbug(...) (void)0
#endif
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#ifndef DEBUG
    namespace IO {
        char ibuf[1 << 21], *p1 = ibuf, *p2 = ibuf, obuf[1 << 21], *p = obuf;
        #define getchar() (::IO::p1 == ::IO::p2 && (::IO::p2 = (::IO::p1 = ::IO::ibuf) + fread (::IO::ibuf, 1, 1 << 21, stdin), ::IO::p1 == ::IO::p2) ? EOF : *::IO::p1++)
        #define putchar(x) (::IO::p == ::IO::obuf + sizeof (::IO::obuf) && (fwrite (::IO::obuf, 1, 1 << 21, stdout), ::IO::p = ::IO::obuf), *::IO::p++ = x)
        #define ioflush() (void)(fwrite (::IO::obuf, 1, ::IO::p - ::IO::obuf, stdout), fflush (stdout), ::IO::p = ::IO::obuf)
        struct outflush {~outflush () {ioflush ();}} flushout;
    }
#endif
int rec () {
    int ch;
    while (isspace (ch = getchar ()));
    return ch;
}
bool rei (char& c) {
    int ch;
    while (isspace (ch = getchar ()));
    if (ch == EOF) return false;
    c = ch;
    return true;
}
bool rei (char* s) {
    if (!rei (*s)) return false;
    int ch;
    while (*++s = ch = getchar (), ch != EOF && !isspace (ch));
    *s = 0;
    return true;
}
template<class T>
bool rei (T& x) {
    bool f = true; int ch; x = 0;
    while (!isdigit (ch = getchar ())) {
        if (ch == EOF) return false;
        f = ch != '-';
    }
    if (f) do x = x * 10 + (ch & 15); while (isdigit (ch = getchar ()));
    else do x = x * 10 - (ch & 15); while (isdigit (ch = getchar ()));
    return true;
}
void pti (const char& c) {putchar (c);}
void pti (const char* s) {while (*s) putchar (*s++);}
template<class T>
void pti (T x) {
    char buf[sizeof (T) * 3], *p = buf + 1;
    if (x >= 0) *buf = (x % 10) | 48, x /= 10;
    else putchar ('-'), *buf = (-(x % 10)) | 48, x = -(x / 10);
    while (x) *p++ = (x % 10) | 48, x /= 10;
    while (p != buf) putchar (*--p);
}
template<class T> void pri (const T& x) {pti (x), putchar (' ');}
template<class T> void prn (const T& x) {pti (x), putchar ('\n');}
template<class T, class... Args> bool rei (T&& x, Args&&... args) {return rei (std::forward<T> (x)) ? rei (std::forward<Args> (args)...) : false;}
template<class T, class... Args> void pti (const T& x, const Args&... args) {pti (x), pti (args...);}
template<class T, class... Args> void pri (const T& x, const Args&... args) {pri (x), pri (args...);}
template<class T, class... Args> void prn (const T& x, const Args&... args) {pri (x), prn (args...);}
template<class T1, class T2> T1& cmax (T1& a, const T2& b) {return a < b ? a = b : a;}
template<class T1, class T2> T1& cmin (T1& a, const T2& b) {return a > b ? a = b : a;}
template<class T> T lowbit (const T& x) {return x & -x;}
using namespace std;
const int MOD = 1e9 + 7;
int mod1 (int x) {return x >= MOD ? x - MOD : x;}
int mod2 (int x) {return x < 0 ? x + MOD : x;}
signed main () {
    int n, x;
    rei (n, x);
    int dp[] = {x, 0}, ct[] = {1, 0};
    fir (i, 2, n) {
        rei (x);
        int t = dp[0];
        dp[0] = (dp[0] + dp[1] + (LL)x * (ct[0] + ct[1])) % MOD;
        dp[1] = (t + (LL)(MOD - x) * ct[0]) % MOD;
        t = ct[0];
        ct[0] = mod1 (ct[0] + ct[1]);
        ct[1] = t;
    }
    prn (mod1 (dp[0] + dp[1]));
    return 0;
}