stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <set>

#include <map>

#include <cmath>

#include <queue>

#include <stack>

#include <vector>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>



using namespace std;



#define pb push_back

#define mp make_pair

#define fil(x,t) memset(x,t,sizeof(x));

#define siz(x) ((int)x.size())

#define rep(i,a,n) for (int i=(a);i<(n);++i)

#define per(i,a,n) for (int i=(n)-1;i>=(a);--i)

#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();++e)

typedef long long ll;

typedef double db;

typedef map<int,int> mii;

typedef pair<int,int> pii;

typedef vector<int> vi;

typedef vector<pii> vii;



int read() {

  int f=1,x=0;char ch=getchar();

  while (ch<'0'||ch>'9') f=(ch=='-')?-1:1,ch=getchar();

  while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();

  return f*x;

}



//head



const int N=1e5+5;

const int MAX=2e5+5;



int n;

int a[MAX],t[MAX];



int check(int v){

  int lim=(n<<1)-1;

  rep (i,0,lim) t[i]=(a[i]>=v)?1:0;

  int l=0,r=lim-1;

  rep (i,n-1,lim-1) if(t[i]==t[i+1]) {

    r=i;break;

  }

  per (i,1,n) if(t[i]==t[i-1]) {

    l=i;break;

  }

  if (r==lim-1&&l==0) return t[0];

  if (n-l-1<=r-n+1) return t[l];

  else return t[r];

}

int main(){

  scanf("%d",&n);

  int lim=(n<<1)-1;

  rep (i,0,lim) a[i]=read();



  int low=0,high=lim,ans=0;

  while (low<=high) {

    int mid=(low+high)>>1;

    if(check(mid)) ans=mid,low=mid+1;

    else high=mid-1;

  }

  printf("%d\n",ans);

  return 0;

}

// Optimized solution to the same problem (faster):
/*

#include<bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<=b;i++)

#define ll long long

#define midl (l+r)>>1 

const int maxn=1e6+7;

using namespace std;

int n,l,r,mid,arc[maxn];

bool check(int pp)

{

	if (arc[n]<=pp&&arc[n+1]<=pp) return 1;

	if (arc[n]<=pp&&arc[n-1]<=pp) return 1;

	if (arc[n]>pp&&arc[n+1]>pp) return 0;

	if (arc[n]>pp&&arc[n-1]>pp) return 0;

	rep(i,1,n) 

	 {

	 	if (arc[n+i]<=pp&&arc[n+i+1]<=pp) return 1;

	    if (arc[n-i]<=pp&&arc[n-i-1]<=pp) return 1;

     	if (arc[n+i]>pp&&arc[n+i+1]>pp) return 0;

    	if (arc[n-i]>pp&&arc[n-i-1]>pp) return 0;

	 }

}

int main(){

	scanf("%d",&n);

	rep(i,1,(n<<1)-1) 

	 scanf("%d",&arc[i]);

	for (r=(n<<1)-1,mid=l+r>>1;l<r;mid=l+r>>1)//while(l<r) 

	 if (check(mid)==1) r=mid;

	 else l=mid+1;

	cout<<l;

	return 0;

}

*/

#include<stdio.h>

#define dn(i,j) (a[i]<=k && a[j]<=k)

#define up(i,j) (k<a[i] && k<a[j])



int a[200005],n,l,r,mid;



inline bool judge(const int &k)

{

    if (dn(n-1,n) || dn(n,n+1)) return 1;

    if (up(n-1,n) || up(n,n+1)) return 0;

    for (int i=1;i<n-1;i++)

    {

        if (dn(n+i,n+i+1) || dn(n-i,n-i-1)) return 1;

        if (up(n+i,n+i+1) || up(n-i,n-i-1)) return 0;

    }

    return dn(1,1);

}



int main()

{

    scanf("%d",&n);

    for (int i=1;i<n<<1;i++) scanf("%d",a+i);

    for (r=(n<<1)-1,mid=l+r>>1;l<r;mid=l+r>>1) if (judge(mid)) r=mid;else l=mid+1;

    printf("%d",l);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<cmath>

#include<algorithm>

#include<string>

#include<cstring>

#include<cctype>

#include<queue>

#include<stack>

#include<map>

#include<set>

#include<iomanip>

#include<sstream>

#include<vector>

#include<cstdlib>

#include<ctime>

#include<list>

#include<deque>

#include<bitset>

#include<fstream>

#define ld double

#define ull unsigned long long

#define ll long long

#define pii pair<int,int >

#define iiii pair<int,pii >

#define mp make_pair

#define INF 1000000000

#define MOD 1000000007

#define rep(i,x) for(int (i)=0;(i)<(x);(i)++)

inline int getint()

{

	int x=0,p=1;char c=getchar();

	while (c<=32)c=getchar();

	if(c==45)p=-p,c=getchar();

	while (c>32)x=x*10+c-48,c=getchar();

	return x*p;

}

using namespace std;

//purplesun

const int N=2e5+10;

int n,a[N],b[N];

//

int main()

{

	n=getint();

	rep(i,2*n-1)b[i]=getint();

	int lb=1,rb=2*n;

	while(rb-lb>1)

	{

		int mid=(lb+rb)>>1;

		rep(i,2*n-1)a[i]=(b[i]>=mid);

		int p=n-1,x=a[p],ans=0;

		bool flag=false;

		while(p>0)

		{

			p--;

			if(a[p]==x||a[n*2-p-2]==x)

			{

				ans=x;

				flag=true;

				break;

			}

			x^=1;

		}

		if(!flag)ans=a[0]&1;

		if(ans)lb=mid;

		else rb=mid;

	}

	printf("%d\n",lb);

	return 0;

}


// Optimized solution to the same problem (faster):
/*

#include<bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<=b;i++)

#define ll long long

#define midl (l+r)>>1 

const int maxn=1e6+7;

using namespace std;

int n,l,r,mid,arc[maxn];

bool check(int pp)

{

	if (arc[n]<=pp&&arc[n+1]<=pp) return 1;

	if (arc[n]<=pp&&arc[n-1]<=pp) return 1;

	if (arc[n]>pp&&arc[n+1]>pp) return 0;

	if (arc[n]>pp&&arc[n-1]>pp) return 0;

	rep(i,1,n) 

	 {

	 	if (arc[n+i]<=pp&&arc[n+i+1]<=pp) return 1;

	    if (arc[n-i]<=pp&&arc[n-i-1]<=pp) return 1;

     	if (arc[n+i]>pp&&arc[n+i+1]>pp) return 0;

    	if (arc[n-i]>pp&&arc[n-i-1]>pp) return 0;

	 }

}

int main(){

	scanf("%d",&n);

	rep(i,1,(n<<1)-1) 

	 scanf("%d",&arc[i]);

	for (r=(n<<1)-1,mid=l+r>>1;l<r;mid=l+r>>1)//while(l<r) 

	 if (check(mid)==1) r=mid;

	 else l=mid+1;

	cout<<l;

	return 0;

}

*/

#include<stdio.h>

#define dn(i,j) (a[i]<=k && a[j]<=k)

#define up(i,j) (k<a[i] && k<a[j])



int a[200005],n,l,r,mid;



inline bool judge(const int &k)

{

    if (dn(n-1,n) || dn(n,n+1)) return 1;

    if (up(n-1,n) || up(n,n+1)) return 0;

    for (int i=1;i<n-1;i++)

    {

        if (dn(n+i,n+i+1) || dn(n-i,n-i-1)) return 1;

        if (up(n+i,n+i+1) || up(n-i,n-i-1)) return 0;

    }

    return dn(1,1);

}



int main()

{

    scanf("%d",&n);

    for (int i=1;i<n<<1;i++) scanf("%d",a+i);

    for (r=(n<<1)-1,mid=l+r>>1;l<r;mid=l+r>>1) if (judge(mid)) r=mid;else l=mid+1;

    printf("%d",l);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define rep(i,a,b) for(int i=(a);i<=(b);i++)

#define per(i,a,b) for(int i=(a);i>=(b);i--)

#define forE(i,x) for(int i=head[x];i!=-1;i=ne[i])

using namespace std;

typedef long long i64;

typedef unsigned long long u64;

typedef unsigned u32;

typedef pair<int,int> pin;

#define mk(a,b) make_pair(a,b)

#define lowbit(x) ((x)&(-(x)))

#define sqr(a) ((a)*(a))

#define clr(a) (memset((a),0,sizeof(a)))

#define ls ((x)<<1)

#define rs (((x)<<1)|1)

#define mid (((l)+(r))>>1)

#define pb push_back

#define w1 first

#define w2 second

inline void read(int &x){

	x=0;int f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

	x*=f;

}

inline void judge(){

	freopen("in.txt","r",stdin);

	freopen("out.txt","w",stdout);

}

/*******************************head*******************************/

const int maxn=200005;

int n,a[maxn],b[maxn];

inline bool check(int k){

	rep(i,1,2*n-1)b[i]=(a[i]>=k);

	int l=n,r=n;

	while(l>1&&b[l-1]!=b[l])l--;

	while(r<2*n-1&&b[r+1]!=b[r])r++;

	if(l==1&&r==2*n-1){

		return (n&1)?b[n]:(b[n]^1);

	}

	return n-l<r-n?b[l]:b[r];

}

int main(){

	//judge();

	read(n);rep(i,1,2*n-1)read(a[i]);

	int l=1,r=2*n-1,ans;

	while(l<=r){

		int md=(l+r)>>1;

		if(check(md)){

			ans=md;l=md+1;

		}else

			r=md-1;

	}printf("%d\n",ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define maxn 200005

using namespace std;

int n,l,r,a[maxn];

bool check(int x)

{

	for(int i=0;i<n-1;i++)

	{

		bool x1=0,x2=0;

		if(a[n-i]>=x&&a[n-i-1]>=x) x1=true;

		if(a[n+i]>=x&&a[n+i+1]>=x) x1=true;

		if(a[n-i]<x&&a[n-i-1]<x) x2=true;

		if(a[n+i]<x&&a[n+i+1]<x) x2=true;

		if(x1) return true;

		if(x2) return false;

	}

	return a[1]>=x;

}

int BS()

{

	int l=1,r=n*2+1;

	while(l+1<r)

	{

		int mid=(l+r)>>1;

		if(check(mid)) l=mid;

		else r=mid;

	}

	return l;

}

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n*2-1;i++) scanf("%d",&a[i]);

	printf("%d\n",BS());

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define f first 

#define s second 

#define pb push_back               

#define all(x) x.begin(), x.end())



using namespace std;                    



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<long long, long long> pll;



const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9, LOG = 18;

const ll INF = (ll)1e8;

const double eps = 1e-9;



int n, a[N], b[N];

bool check(int x){

	for(int i = 1; i <= n; i++)

		if(a[i] <= x)

			b[i] = 0;

		else

			b[i] = 1;

	

	bool ok1, ok2;

	ok1 = 1;         

	ok2 = 1;        

	for(int i = 1; i <= n; i++)

		if(b[i] == i % 2)

			ok1 = 0;

	 	else

			ok2 = 0;

    

    if(ok1 || ok2){

		int cnt = (n + 1) / 2 - 1;

        return b[n/2 + 1] ^ (cnt & 1);

	}

	for(int i = 1; ; i++){

		if(b[n / 2 + 1 + i] == b[n / 2 + 1 - i] && b[n / 2 + 1 + i] != b[n / 2  + i])

			continue;               

		if(b[n / 2 + 1 + i] == b[n / 2  + i])

			return b[n / 2 + 1 + i];

		else

			return b[n / 2 + 1 - i]; 

	}

}

int main(){

	cin >> n;

	n = n + n - 1;

	for(int i = 1; i <= n; i++)

		cin >> a[i];

	int l = 1, r = n;

	while(l <= r){

		int m = l + r >> 1;

		if(check(m))

			l = m + 1;

		else

			r = m - 1;

	}

	cout << l;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int N,A[200005],a[200005],l=1,r=200000,mid;

bool check(int n)

{

	int re=-1;

	for(int i=1;i<=N*2-1;i++)

	if(A[i]>=n)

	a[i]=1;

	else

	a[i]=0;

	for(int i=1;i<=N-1;i++)

	{

		if(a[N+i]==a[N+i-1])

		{

			re=a[N+i];

			break;

		}

		if(a[N-i]==a[N-i+1])

		{

			re=a[N-i];

			break;

		}

	}

	if(re!=-1)

	return re;

	else

	return a[1];

}

int main()

{

	scanf("%d",&N);

	for(int i=1;i<=N*2-1;i++)

	scanf("%d",&A[i]);

	while(l+1<r)

	{

		mid=(l+r)/2;

		if(check(mid))

		l=mid;

		else

		r=mid;

	}

	printf("%d",l);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

int n;

int a[200005];

int f[200005];

int Check(int x)

{

	for(int i=1;i<=2*n-1;++i)

	{

		if(a[i]>=x)

		{

			f[i]=1;

		}

		else

		{

			f[i]=0;

		}

	}

	int nst=-1,nds=291433;

	for(int i=2;i<=2*n-1;++i)

	{

		if(f[i]==f[i-1])

		{

			if(min(abs(n-i+1),abs(n-i))<nds)

			{

				nds=min(abs(n-i),abs(n-i+1));

				nst=f[i];

			}

		}

	}

	if(nst==-1)

	{

		return f[1];

	}

	return nst;

}



int main()

{

	scanf("%d",&n);

	for(int i=1;i<=2*n-1;++i)

	{

		scanf("%d",&a[i]);

	}

	int l=1,r=2*n-1;

	while(l+1<r)

	{

		int mid=(l+r)/2;

		if(Check(mid))

		{

			l=mid;

		}	

		else

		{

			r=mid;

		}

	}

	printf("%d",l);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int N,A[200005],a[200005],l=1,r=200000,mid;

bool check(int n)

{

	int re=-1;

	for(int i=1;i<=N*2-1;i++)

	if(A[i]>=n)

	a[i]=1;

	else

	a[i]=0;

	for(int i=1;i<=N-1;i++)

	{

		if(a[N+i]==a[N+i-1])

		{

			re=a[N+i];

			break;

		}

		if(a[N-i]==a[N-i+1])

		{

			re=a[N-i];

			break;

		}

	}

	if(re!=-1)

	return re;

	else

	return a[1];

}

int main()

{

	scanf("%d",&N);

	for(int i=1;i<=N*2-1;i++)

	scanf("%d",&A[i]);

	while(l+1<r)

	{

		mid=(l+r)/2;

		if(check(mid))

		l=mid;

		else

		r=mid;

	}

	printf("%d",l);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define For(i,a,b) for(int i=(a);i<=(b);++i)

#define Rof(i,a,b) for(int i=(a);i>=(b);--i)

#define S(i) ((int)(A[i]>=Pos))

#define LL long long

using namespace std;

int N,A[300001],INF=10000000;

bool Check(int Pos){

	int m[2];

	m[0]=m[1]=INF;

//	For(i,1,2*N-1) printf("%d ",S(i));puts("");

	For(i,N,2*N-2)

		if(S(i)==S(i+1)){

			m[S(i)]=min(m[S(i)],i-N);

//			cout<<'-'<<i<<' '<<S(i)<<endl;

			break;

		}

	Rof(i,N,2)

		if(S(i)==S(i-1)){

			m[S(i)]=min(m[S(i)],N-i);

			break;

		}

//	cout<<m[1]<<' '<<m[0]<<endl;

	if(m[0]==INF&&m[1]==INF)return (S(N)^(N&1)^1);

	if(m[1]<m[0])return 1;

	return 0;

}

int main(){

	scanf("%d",&N);For(i,1,2*N-1) scanf("%d",&A[i]);

	int l=1,r=2*N-1,Mid,Ans;

//	cout<<Check(4);



	while(l<=r){

		Mid=l+r>>1;

		if(Check(Mid))

			Ans=Mid,l=Mid+1;

		else

			r=Mid-1;

	}



	printf("%d\n",Ans);



	return 0;

}

/*

3

1 5 2 4 3

*/

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int N,A[200005],a[200005],l=1,r=200000,mid;

bool check(int n)

{

	int re=-1;

	for(int i=1;i<=N*2-1;i++)

	if(A[i]>=n)

	a[i]=1;

	else

	a[i]=0;

	for(int i=1;i<=N-1;i++)

	{

		if(a[N+i]==a[N+i-1])

		{

			re=a[N+i];

			break;

		}

		if(a[N-i]==a[N-i+1])

		{

			re=a[N-i];

			break;

		}

	}

	if(re!=-1)

	return re;

	else

	return a[1];

}

int main()

{

	scanf("%d",&N);

	for(int i=1;i<=N*2-1;i++)

	scanf("%d",&A[i]);

	while(l+1<r)

	{

		mid=(l+r)/2;

		if(check(mid))

		l=mid;

		else

		r=mid;

	}

	printf("%d",l);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

using namespace std;



int n,a[200005];



inline bool check(const int &k){

    if((a[n-1]<=k&&a[n]<=k)||(a[n]<=k&&a[n+1]<=k))return 1;

    if((a[n-1]> k&&a[n]> k)||(a[n]> k&&a[n+1]> k))return 0;

    for(int i=1;i<n-1;i++){

        if((a[n+i]<=k&&a[n+i+1]<=k)||(a[n-i]<=k&&a[n-i-1]<=k))return 1;

        if((a[n+i]> k&&a[n+i+1]> k)||(a[n-i]> k&&a[n-i-1]> k))return 0;

    }

    return a[1]<=k;

}



int main(){

    scanf("%d",&n);

    for(int i=1;i<n<<1;i++)scanf("%d",a+i);

    int l=1,r=(n<<1)-1;

    while(l<r){

        int mid=l+r>>1;

        if(check(mid))r=mid;

        else l=mid+1;

    }

    printf("%d",l);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int N,A[200005],a[200005],l=1,r=200000,mid;

bool check(int n)

{

	int re=-1;

	for(int i=1;i<=N*2-1;i++)

	if(A[i]>=n)

	a[i]=1;

	else

	a[i]=0;

	for(int i=1;i<=N-1;i++)

	{

		if(a[N+i]==a[N+i-1])

		{

			re=a[N+i];

			break;

		}

		if(a[N-i]==a[N-i+1])

		{

			re=a[N-i];

			break;

		}

	}

	if(re!=-1)

	return re;

	else

	return a[1];

}

int main()

{

	scanf("%d",&N);

	for(int i=1;i<=N*2-1;i++)

	scanf("%d",&A[i]);

	while(l+1<r)

	{

		mid=(l+r)/2;

		if(check(mid))

		l=mid;

		else

		r=mid;

	}

	printf("%d",l);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define D 1000000007

 

int main(){

 ll n,m,x[100001]={},y[100001]={}; 

 cin>>n>>m;

 

 for(int i=0;i<n;i++)cin>>x[i];

 for(int i=0;i<m;i++)cin>>y[i];

 

ll X=0,Y=0;

 for(int i=0;i<n-1;i++)

  X=(X+((x[i+1]-x[i])*((i+1)*(n-i-1))%D)%D)%D;

 

 for(int i=0;i<m-1;i++)

  Y=(Y+((y[i+1]-y[i])*((i+1)*(m-i-1))%D)%D)%D;

 

 cout<<(X*Y)%D<<endl;

 return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

typedef long long ll;

ll x[100001] = {};

ll y[100001] = {};

const ll mod = (int)1e9 + 7;

int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    ll n, m;

    cin >> n >> m;



    for(int i = 0; i < n; ++i)

        cin >> x[i];

    ll width = x[n - 1] - x[0];

    

    for(int i = 0; i < m; ++i)

        cin >> y[i];

    ll height = y[m - 1] - y[0];



    ll sum1 = 0, sum2 = 0;

    for(int i = 0; i < n - 1; ++i){

        sum1 = (sum1 + ((x[i + 1] - x[i]) * ((i + 1) * (n - i - 1)) % mod) % mod) % mod;

    }

    for(int i = 0; i < m - 1; ++i){

        sum2 = (sum2 + ((y[i + 1] - y[i]) * ((i + 1) * (m - i - 1)) % mod) % mod) % mod;        

    }

    cout << (sum1 * sum2) % mod << endl;



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

const int maxn=1e5+5;

const int mod=1e9+7;

#define ll long long

using namespace std;

ll a[maxn],b[maxn];

ll aa[maxn],bb[maxn];

int main()

{

    int i,j,n,m;

    ll h=0,w=0;

    scanf("%d%d",&n,&m);

    for(i=1; i<=n; i++)

    {

        scanf("%lld",&a[i]);

    }

    for(i=1; i<=m; i++)

    {

        scanf("%lld",&b[i]);

    }

//    for(i=1; i<=n; i++)

//    {

//        scanf("%d",&a[i]);

//        aa[i]=aa[i-1]+a[i];

//    }

//    for(i=1; i<=m; i++)

//    {

//        scanf("%d",&b[i]);

//        bb[i]=bb[i-1]+b[i];

//    }

//    for(i=1; i<n; i++)

//    {

//        h+=aa[n]-aa[i]-(a[i]%mod*(n-i)%mod)%mod;

//        if(h>=mod) h-=mod;

//    }

//    cout<<h<<endl;

//    for(i=1; i<m; i++)

//    {

//        w+=bb[m]-bb[i]-b[i]*(m-i);

//        if(w>=mod) w-=mod;

//    }

//    cout<<w<<endl;

    for(int l=1, r=n; l<r; ++l,--r)

        w = (w + (a[r]-a[l])*(r-l))%mod;

    for(int l=1, r=m; l<r; ++l,--r)

        h = (h + (b[r]-b[l])*(r-l))%mod;

    cout<<(w*h)%mod<<endl;

    return 0;

}

/*622259192

993828220*/


// Optimized solution to the same problem (faster):
#include<cstdio>

#define LL long long

#define N 100010

LL n,m,a[N],b[N];

signed main()

{

	scanf("%lld%lld",&n,&m);LL x(0),y(0),mod(1e9+7);

	for(register int i(1);i<=n;i++)

		scanf("%lld",&a[i]);

	for(register int i(1);i<=m;i++)

		scanf("%lld",&b[i]);

	for(register int i(1),j(n);i<j;i++,j--)

		x=(x+(a[j]-a[i])*(j-i)%mod)%mod;

	for(register int i(1),j(m);i<j;i++,j--)

		y=(y+(b[j]-b[i])*(j-i)%mod)%mod;

	return printf("%lld\n",x*y%mod),0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

confirm 0LL and 1LL

confirm cornercases such as 0

confirm times of cin < 10^6

*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ld = long double;

using P = pair<ll, ll>;

using Pld = pair<ld, ld>;

using Vec = vector<ll>;

using VecP = vector<P>;

using VecB = vector<bool>;

using VecC = vector<char>;

using VecD = vector<ld>;

using VecS = vector<string>;

using VecVec = vector<Vec>;

using Graph = vector<VecP>;

template <typename T>

using Vec1 = vector<T>;

template <typename T>

using Vec2 = vector<Vec1<T> >;

#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))

#define REPN(i, m, n) for(ll (i) = (m); (i) <= (n); ++(i))

#define REPR(i, m, n) for(ll (i) = (m)-1; (i) >= (n); --(i))

#define REPNR(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))

#define rep(i, n) REP(i, 0, n)

#define repn(i, n) REPN(i, 1, n)

#define repr(i, n) REPR(i, n, 0)

#define repnr(i, n) REPNR(i, n, 1)

#define all(s) (s).begin(), (s).end()

#define pb push_back

#define mp make_pair

#define fs first

#define sc second

template <typename T>

bool chmax(T &a, const T& b){if(a < b){a = b; return true;} return false;}

template <typename T>

bool chmin(T &a, const T& b){if(a > b){a = b; return true;} return false;}

template <typename T>

void co(const T n){cout << n << endl;}

template <typename T>

void cosp(const T n){cout << n << ' ';}

void coVec(const Vec &v){for(ll i : v) cosp(i); cout << endl;}

ll in(){ll x; cin >> x; return x;}

void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}

void setp(const ll n){cout << fixed << setprecision(n);}

const ll INF = 1e9+1;

const ll LINF = 1e18+1;

const ll MOD = 1e9+7;

//const ll MOD = 998244353;

const ld PI = acos(-1);

const ld EPS = 1e-11;



int main(void){

	ll n, m;

	cin >> n >> m;

	Vec x(n), y(m);

	rep(i, n) cin >> x[i];

	rep(i, m) cin >> y[i];

	sort(all(x));

	sort(all(y));



	ll sumx = 0;

	rep(i, n){

		sumx += x[i]*(i*2-(n-1));

		sumx %= MOD;

	}

	ll sumy = 0;

	rep(i, m){

		sumy += y[i]*(i*2-(m-1));

		sumy %= MOD;

	}

	co(sumx*sumy%MOD);



	return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#define MOD 1000000007



int main(void){

	long i, j, n, m;

	scanf("%ld %ld", &n, &m);

	long x[n], y[m];

	for(i = 0; i < n; i++) scanf("%ld", &x[i]);

	for(i = 0; i < m; i++) scanf("%ld", &y[i]);



	long sumx = 0;

	for(i = 0; i < n; i++){

		sumx += x[i]*(i*2-(n-1));

		if(sumx > 1e18 || sumx < -1e18) sumx %= MOD;

	}

	sumx %= MOD;

	long sumy = 0;

	for(i = 0; i < m; i++){

		sumy += y[i]*(i*2-(m-1));

		if(sumy > 1e18 || sumy < -1e18) sumy %= MOD;

	}

	sumy %= MOD;

	printf("%ld\n", sumx*sumy%MOD);



	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

confirm 0LL and 1LL

confirm cornercases such as 0

confirm times of cin < 10^6

*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ld = long double;

using P = pair<ll, ll>;

using Pld = pair<ld, ld>;

using Vec = vector<ll>;

using VecP = vector<P>;

using VecB = vector<bool>;

using VecC = vector<char>;

using VecD = vector<ld>;

using VecS = vector<string>;

using VecVec = vector<Vec>;

using Graph = vector<VecP>;

template <typename T>

using Vec1 = vector<T>;

template <typename T>

using Vec2 = vector<Vec1<T> >;

#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))

#define REPN(i, m, n) for(ll (i) = (m); (i) <= (n); ++(i))

#define REPR(i, m, n) for(ll (i) = (m)-1; (i) >= (n); --(i))

#define REPNR(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))

#define rep(i, n) REP(i, 0, n)

#define repn(i, n) REPN(i, 1, n)

#define repr(i, n) REPR(i, n, 0)

#define repnr(i, n) REPNR(i, n, 1)

#define all(s) (s).begin(), (s).end()

#define pb push_back

#define mp make_pair

#define fs first

#define sc second

template <typename T>

bool chmax(T &a, const T& b){if(a < b){a = b; return true;} return false;}

template <typename T>

bool chmin(T &a, const T& b){if(a > b){a = b; return true;} return false;}

template <typename T>

void co(const T n){cout << n << endl;}

template <typename T>

void cosp(const T n){cout << n << ' ';}

void coVec(const Vec &v){for(ll i : v) cosp(i); cout << endl;}

ll in(){ll x; cin >> x; return x;}

void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}

void setp(const ll n){cout << fixed << setprecision(n);}

const ll INF = 1e9+1;

const ll LINF = 1e18+1;

const ll MOD = 1e9+7;

//const ll MOD = 998244353;

const ld PI = acos(-1);

const ld EPS = 1e-11;



int main(void){

	ll n, m;

	cin >> n >> m;

	Vec x(n), y(m);

	rep(i, n) cin >> x[i];

	rep(i, m) cin >> y[i];



	ll sumx = 0;

	rep(i, n){

		sumx += x[i]*(i*2-(n-1));

		sumx %= MOD;

	}

	ll sumy = 0;

	rep(i, m){

		sumy += y[i]*(i*2-(m-1));

		sumy %= MOD;

	}

	co(sumx*sumy%MOD);



	return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#define MOD 1000000007



int main(void){

	long i, j, n, m;

	scanf("%ld %ld", &n, &m);

	long x[n], y[m];

	for(i = 0; i < n; i++) scanf("%ld", &x[i]);

	for(i = 0; i < m; i++) scanf("%ld", &y[i]);



	long sumx = 0;

	for(i = 0; i < n; i++){

		sumx += x[i]*(i*2-(n-1));

		if(sumx > 1e18 || sumx < -1e18) sumx %= MOD;

	}

	sumx %= MOD;

	long sumy = 0;

	for(i = 0; i < m; i++){

		sumy += y[i]*(i*2-(m-1));

		if(sumy > 1e18 || sumy < -1e18) sumy %= MOD;

	}

	sumy %= MOD;

	printf("%ld\n", sumx*sumy%MOD);



	return 0;

}


// Fastest known solution for this problem: