//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#include<set>
#include <array>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 998244353
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound

#define si(x) int(x.size())
#define sum_all(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
const double pi = 2 * acos(0.0);
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 };
const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 };

ll min(ll a,ll b)
{
    if(a<b)return a;
    return b;
}
ll max(ll a,ll b)
{
    if(a>b)return a;
    return b;
}
ll ceil1(ll a,ll b)
{
    return(a+b-1)/b;
}
void read(vector<ll> & arr)
{
  for(ll i=0;i<si(arr);i++)
    cin >> arr[i];

}

void read_graph(vector<vector<ll>>& g, ll m)
{
  while(m--)
  {
    ll x,y; cin >> x>> y ;
    x--,y--;
    g[x].pb(y);
    g[y].pb(x);
  }
}
ll modmul(ll x, ll y, ll m=mod)
{
    if (y==0) return 0;
    ll a = modmul(x, y / 2, m) % m;
    a = (a + a) % m;
    if (y % 2 != 0)
    {
        a = (a + x) % m;
    }
    return a;
}
 ll power(ll a, ll b, ll m)
{
    if (b == 1) return a;
    ll x = power(a, b / 2, m);
    x *= x;
    x %= m;
    if (b % 2 == 1)
    {
        x *= a;
        x %= m;
    }
    return x;
}
void solve()
{
int n;
cin >> n ;
vector<ll> arr(n);
for(int i=0;i<n;i++)
cin >> arr[i];

sort(arr.begin(),arr.end());
// for(ll i=0;i<n;i++) cout << arr[i]  << " " ;
  // cout << endl ;
vector<ll> suff(n);
vector<ll> pw(n+1);
pw[0]=1;
for(ll i=1;i<=n;i++)
{
   pw[i]=pw[i-1]*2;
   pw[i]%=mod;
}

for(ll i=0;i<n;i++)
{
   suff[i]= modmul(arr[i],pw[i],mod);
   suff[i]%=mod;
}
for(ll i=n-2;i>=0;i--)
{
  suff[i] +=suff[i+1];
  suff[i]%=mod;
}
ll inv_2= power(2,mod-2,mod);
ll base=inv_2;
// ll k =853983 *853983 ;
ll ans=0 ;

for(ll i=0;i<n;i++)
{
   ans+=(modmul(arr[i],arr[i]))%mod;
   ans%=mod;
}
for(ll i=0;i<n-1;i++)
{
  ll x ;
  x= modmul(suff[i+1],inv_2);
  x=modmul(arr[i],x);
  ans+=x;
  ans%=mod;
  inv_2= modmul(inv_2,base);
}
cout << ans <<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
   // int t; cin >> t; while (t--)
    solve();

}

#include<bits/stdc++.h>
#define re register
#define int long long
using namespace std;
inline int read(){
	re int t=0;re char v=getchar();
	while(v<'0')v=getchar();
	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
	return t;
}
const int M=998244353;
inline int ksm(re int x,re int y){
	re int s=1;
	while(y){
		if(y&1)s=1ll*s*x%M;
		x=1ll*x*x%M,y>>=1;
	}
	return s;
}
int n,m,a[1000002],ans,b[1000002],sum;
char s[1000002];
signed main(){
	n=read();
	for(re int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(re int i=1;i<=n;++i){
		ans=(ans+a[i]*sum)%M;
		ans=(ans+a[i]*a[i])%M;
		sum=(sum+sum)%M;
		sum=(sum+a[i])%M;
	}
	printf("%lld",ans);
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

int main() {
    ll N, M; cin >> N >> M;
    vector<ll> A(N), B(M);
    rep(i,N) cin >> A[i];
    rep(i,M) cin >> B[i];

    vector<vector<ll>> dp(N+1,vector<ll>(M+1,inf));
    rep(i,N+1) dp[i][0] = i;
    rep(i,M+1) dp[0][i] = i;
    repp(i,1,N+1) repp(j,1,M+1) {
        ll a = A[i-1], b = B[j-1];
        chmin(dp[i][j],dp[i-1][j]+1);
        chmin(dp[i][j],dp[i][j-1]+1);
        if (a==b) {
            chmin(dp[i][j],dp[i-1][j-1]);
        } else {
            chmin(dp[i][j],dp[i-1][j-1]+1);
        }
    }
    ll ans = dp[N][M];

    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll solve(int L, int R){
  vector<ll> memo(1<<21, -1);
  auto f = [&](auto self, int L, int R) -> ll {
    // L < x <= R であって、gcd(x,y) == 1 となるものを数える
    auto key = L + R;
    if(memo[key] != -1) return memo[key];

    ll res = ll(R - L) * (R - L);
    int d = max(L, R);
    while(d > 1){
      int nxt_d = max(L / (L / d + 1), R / (R / d + 1));
      res -= (d - nxt_d) * self(self, L / d, R / d);
      d = nxt_d;
    }
    memo[key] = res;
    return res;
  };

  auto g = [&](int L, int R) -> ll {
    ll res = 0;
    int d = max(L, R);
    while(d){
      int nxt_d = max(L / (L / d + 1), R / (R / d + 1));
      ll cnt = max(0, min(d, R) - max(nxt_d, L));
      res += cnt * (R / d - L / d);
      d = nxt_d;
    }
    return res;
  };

  --L;
  ll ans = ll(R - L) * (R - L);
  ans -= f(f, L, R) + 2 * g(L, R);
  ans += 2 * (L < 1 ? R - L : 0) + (R - L);
  ans -= L < 1;
  return ans;
}

int main(){
  int L, R;
  cin >> L >> R;
  ll ans = solve(L, R);
  cout << ans << endl;
  return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int v, t, s, d;
    cin >> v >> t >> s >> d;
    cout << (d < v*t || v*s < d ? "Yes" : "No") << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Test int T;cin>>T;while(T--)
#define PI acos(-1)
#define endl "\n"
#define fx(x) fixed<<setprecision(x)
#define sz(s) (int)s.size()
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define mem(a,n) memset((a),n,sizeof (a))
#define INF 1e9
#define ii pair<int,int>
ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
void file(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    //freopen("east.in", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
}
void fast(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    file();
}
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
const double eps=1e-9;
const int mod=1e9+7;
const int N=1e5+7;
int main(){
    fast();
    int v,t,s,d;
    cin>>v>>t>>s>>d;
    if(d>=t*v&&d<=s*v){
        cout<<"No";
    }
    else cout<<"Yes";
}


#pragma gcc target("avx2")
#pragma gcc optimize("o3")
#pragma gcc optimize("unroll-loops")
//#pragma warning(disable : 4996)

//#define ATCODER
#ifdef ATCODER
#include <atcoder/all>
using namespace atcoder;
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#if defined(LOCAL) || defined(_MSC_VER)
void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}

#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define DEBUG(...) 42
#endif

#ifdef _MSC_VER
#include <intrin.h>

#include "stdafx.h"
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#define ll long long
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define VSORT(v) sort(v.begin(), v.end());
#define VREVERSE(v) reverse(v.begin(), v.end())
#define print(x) cout << (x) << '\n'
#define spa << " " <<
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
//#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
    REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 400040;
const double pi = acos(-1);
constexpr long double EPS = 1e-9;
const int dx[4] = { 0, 0, -1, 1 }, dy[4] = { -1, 1, 0, 0 };
using ld = long double;
const int INF = 1e9;
struct Dijkstra {
	using pii = pair<int, int>;
	struct edge {
		int to, cost;
		edge(int to_,int cost_):to(to_),cost(cost_){}
	};
	int N;
	vector<vector<edge>> G;
	vector<int> d;
	Dijkstra(int N_) : N(N_), G(N_), d(N_, INF) {}
	void add_edge(int u, int v, int c) { G[u].emplace_back( v, c ); }
	void build(int s) {
		d[s] = 0;
		priority_queue<pii, vector<pii>, greater<pii>> que;
		que.push(pii(0, s));
		while (!que.empty()) {
			pii p = que.top();
			que.pop();
			int v = p.second;
			if (d[v] < p.first) continue;
			for (int i = 0; i < G[v].size(); i++) {
				edge e = G[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(pii(d[e.to], e.to));
				}
			}
		}
	}
};
int H, W;
int toid(int x, int y) { return x * W + y; }
int A[505][505], B[505][505];
void solve() {
	cin >> H >> W;
	REP(i, H) REP(j, W - 1) cin >> A[i][j];
	REP(i, H - 1) REP(j, W) cin >> B[i][j];
	int V = H * W;
	Dijkstra G(H * W);
	REP(i, H) REP(j, W) {
		if (j + 1 < W) { G.add_edge(toid(i, j), toid(i, j + 1), A[i][j]); }
		if (j - 1 >= 0) G.add_edge(toid(i, j), toid(i, j - 1), A[i][j - 1]);
		if (i + 1 < H) G.add_edge(toid(i, j), toid(i + 1, j), B[i][j]);
		REP(k, i) {
			G.add_edge(toid(i, j), toid(k, j), i - k + 1);
		}
	}
	G.build(toid(0, 0));
	print(G.d[H * W - 1]);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	// int q; cin >> q;
	// while (q--)
	solve();
}

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

const int N = 500, D = 2 * 1000 * N;

vector<array<short, 2>> dijkstra[D];
int a[N][N], b[N][N], dist[N][N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m - 1; ++j) {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> b[i][j];
        }
    }

    for (int i = 0; i < n; ++i) {
        fill(dist[i], dist[i] + m, INT_MAX);
    }

    dijkstra[0].push_back({0, 0});
    dist[0][0] = 0;

    for (int d = 0; d < D; ++d) {
        while (!dijkstra[d].empty()) {
            auto [r, c] = dijkstra[d].back();
            dijkstra[d].pop_back();

            if (d > dist[r][c]) {
                continue;
            }

            if (c < m - 1 && d + a[r][c] < dist[r][c + 1]) {
                dist[r][c + 1] = d + a[r][c];
                dijkstra[dist[r][c + 1]].push_back({r, c + 1});
            }

            if (c > 0 && d + a[r][c - 1] < dist[r][c - 1]) {
                dist[r][c - 1] = d + a[r][c - 1];
                dijkstra[dist[r][c - 1]].push_back({r, c - 1});
            }

            if (r < n - 1 && d + b[r][c] < dist[r + 1][c]) {
                dist[r + 1][c] = d + b[r][c];
                dijkstra[dist[r + 1][c]].push_back({r + 1, c});
            }

            for (int i = 1; i <= r; ++i) {
                if (d + 1 + i < dist[r - i][c]) {
                    dist[r - i][c] = d + 1 + i;
                    dijkstra[dist[r - i][c]].push_back({r - i, c});
                }
            }
        }
    }

    cout << dist[n - 1][m - 1] << "\n";
}


#include<bits/stdc++.h>
using namespace std;
int a[200010],b[200010];
queue<int>pos[200010];
map<int,int>id;
int tot,n;
int T[200010<<2];
int q(int rt,int l,int r,int pos){
	if(l==r)return T[rt];
	int mid=l+r>>1;
	if(pos<=mid)return T[rt]+q(rt<<1,l,mid,pos);
	else return T[rt]+q(rt<<1|1,mid+1,r,pos);
}
void ad(int rt,int l,int r,int L,int R){
	if(l==L&&r==R){
		T[rt]++;return;
	}
	int mid=l+r>>1;
	if(R<=mid)ad(rt<<1,l,mid,L,R);
	else if(L>mid)ad(rt<<1|1,mid+1,r,L,R);
	else ad(rt<<1,l,mid,L,mid),ad(rt<<1|1,mid+1,r,mid+1,R);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i]+=i;
		int t=id[a[i]];
		if(!t)id[a[i]]=t=++tot;
		pos[t].push(i);
	}
	int flag=1;
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);
		b[i]+=i;
		int t=id[b[i]];
		if(t&&!pos[t].empty()){
			b[i]=pos[t].front();
			pos[t].pop();
		}
		else flag=0;
	}
	if(!flag)printf("-1");
	else{
		long long ans=0;
		for(int i=1;i<=n;i++)ans+=q(1,1,n,b[i]),ad(1,1,n,1,b[i]);
		printf("%lld",ans);
	}
}

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

template<typename T>
struct BIT{
private:
  vector<T> A;
  const int n;

public:
  BIT(int _n) : A(_n+1,0), n(_n){}

  //i番目までの累積和
  T sum(int i){
    T s=0;
    while(i>0){
      s+=A[i];
      i-=i&-i;
    }
    return s;
  }

  //[i,j]の要素の総和
  T sum(int i,int j){
    return sum(j)-sum(i-1);
  }

  //i番目にxを代入
  void add(int i,T x){
    while(i<=n){
      A[i]+=x;
      i+=i&-i;
    }
  }
};

int main(){
  int n;
  cin>>n;
  vector<ll> A(n),B(n);
  rep(i,n) cin>>A[i];
  rep(i,n) cin>>B[i];

  vector<pair<ll,int>> C(n),D(n);
  rep(i,n){
    C[i]={A[i]+i,i};
    D[i]={B[i]+i,i};
  }
  sort(ALL(C));
  sort(ALL(D));

  vector<int> E(n);
  rep(i,n){
    if(C[i].first!=D[i].first){
      cout<<-1<<endl;
      return 0;
    }
    E[C[i].second]=D[i].second+1;
  }

  BIT<int> bit(n);
  ll ans=0;
  for(int j=0;j<n;j++){
    ans+=j-bit.sum(E[j]);
    bit.add(E[j],1);
  }
  cout<<ans<<endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
using namespace std;

int c[500009], p[500009];;
vector<int> v;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	string s, t; cin >> s >> t;
	for (int i = 0; i < n - 1; i++) {
		if (s[i] != t[i]) {
			p[i + 1] = 1;
			if (t[i + 1] == '0') t[i + 1] = '1';
			else t[i + 1] = '0';
		}
		if (s[i + 1] == '1') v.push_back(i + 1);
	}
	if (s[n - 1] != t[n - 1]) {
		cout << -1 << '\n';
		return 0;
	}
	long long ans = 0;
	int pi = 0, r = 0;
	for (int i = 1; i < n; i++) {
		while (pi < v.size() && v[pi] < i) pi++;
		r = (r + c[i]) % 2;
		if (r == p[i]) continue;
		if (pi >= v.size()) {
			cout << -1 << '\n';
			return 0;
		}
		c[v[pi] + 1]++;
		r = (r + 1) % 2;
		ans += v[pi] - i + 1;
		pi++;
	}
	cout << ans << '\n';
	return 0;
}

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

int main() {
  int N,M,T;
  cin >> N >> M >> T;

  vector<int> a(M);
  vector<int> b(M);
  for(int i = 0; i < M*2; i++) {
    if(i%2 == 0) {
      cin >> a.at(i/2);
    }else{
      cin >> b.at((i-1)/2);
    }
  }

  bool Ans = true;
  int B = N;
  int Time = 0;
  for(int i = 0; i < M; i++) {
    B -= (a.at(i)-Time);
    Time = a.at(i);
    if(B <= 0) Ans = false;
    B += (b.at(i)-Time);
    Time = b.at(i);
    if(N < B) B = N;
  }
  B -= (T-b.at(M-1));
  if(B <= 0) Ans = false;

  if(Ans) {
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}


#define LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

#define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++)
#define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
// #define M_PI 3.14159265358979323846 // CF
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank
#define endl "\n"
#define Case(x) printf("Case #%d: ", x); // gcj

typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;

constexpr ll LINF = 8001002003004005006ll;
constexpr int INF = 1002003004;
constexpr int n_max = 2e5+10;

template<class T>
inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; };
template<class T>
inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; };

template<class T, class U>
T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;};

// debug
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';};
string to_string(const char c) {return to_string((string) &c);};
string to_string(bool b) {return (b ? "true" : "false");};
template <size_t N>
string to_string(bitset<N> v){
  string res = "";
  for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
  return res;
};
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for(const auto &x : v) {
    if(!first) res += ", ";
    first = false; res += to_string(x);
  }
  res += "}";
  return res;
};
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}

void debug_out() {cerr << endl;};
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); };

void LINE_OUT() {
  cout << "--------------" << endl;
};

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define LINE LINE_OUT();
#else
#define debug(...) 71
#define LINE 71;
#endif

void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << " ";
  print(forward<Tail>(tail)...);
};

template <class T>
void print(vector<T> &vec) {
  for (auto& a : vec) {
    cout << a;
    if (&a != &vec.back()) cout << " ";
  }
  cout << endl;
};

template <class T>
void print(vector<vector<T>> &df) {
  for (auto& vec : df) {
    print(vec);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll X, Y, A, B; cin >> X >> Y >> A >> B;
  ll x = X; // 強さ
  ll ans = 0;

  while (1) {
    if ((ld)x * (ld)A > INF) {
      ll diff = Y - x -1;
      if (diff <= 0) break;
      ans += (diff/B);
      break;
    } else if (x * A > Y-1) {
      ll diff = Y - x -1;
      if (diff <= 0) break;
      ans += (diff/B);
      break;
    } else {
      if (x * A <= x + B) {
        x = x * A;
        ans++;
      } else {
        ll diff = x * A - x;
        ans += (diff/B);
        x = x + (diff/B) * B;
      }
    }
  }

  cout << ans << endl;

  return 0;
};

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <complex>
#include <iomanip>
#include <numeric>
#include<unordered_set>
#include <climits>//INT_100010n
#include <regex>
#include<deque>
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
#define LL unsigned int
#define wc 1e-18
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
ll x,y,a,b,ct;
int main()
{
    cin>>x>>y>>a>>b;
    while(x<y/a&&x<(x+b)/a){
        ct++;
        x=x*a;
    }
    ct=ct+(y-1-x)/b;
    cout<<ct<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define start() int t;cin>>t;while(t-->0)
#define speed ios_base::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);
#define endl "\n"
#define mod 1000000007

bool isPrime(int n)
{
    // Corner cases
    if(n<=3){
        return true;
    }
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

int main()
{
   speed;
   ll n;
   cin>>n;
   ll a[n];
   map<ll,ll> m;
   for(int i=0;i<n;i++){
       cin>>a[i];
       m[a[i]%200]++;
   }
   ll sum=0;
   for(auto x:m){
       if(x.second>1){
           sum+=(x.second*(x.second-1))/2;

       }
   }
   cout<<sum;
   return 0;
}

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

#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j);
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }

ll n;

int main()
{
  in(n);
  vll counter(200, 0);
  ll ip;
  loop0(i, n)
  {
    in(ip);
    counter[ip%200] += 1;
  }
  ll ans = 0;
  loop0(j, 200)
  {
    ll i = counter[j];
    ans += (i*(i-1)/2);
  }
  cout<<ans<<"\n";
}


#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
#define Please return
#define AC 0
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=998244353;
const int maxn = 1e6+10;

int n,m,a[maxn],q,t;
int b[maxn],c[maxn],fa[maxn],d[maxn];

int find(int x){
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
LL sum[maxn];
int main(){
//    op;
    read(n);read(m);
    fo(i,1,n) read(a[i]);
    fo(i,1,n) read(b[i]);

    fo(i,1,n) fa[i]=i;

    fo(i,1,m) read(c[i]),read(d[i]),fa[find(c[i])]=find(d[i]);

    fo(i,1,n) {
        sum[find(i)]+=a[i];
        sum[find(i)]-=b[i];
    }
    int flag = 0;
    fo(i,1,n) if(sum[i]) flag =1;
    if(flag)puts("No");
    else puts("Yes");

    Please AC;
}

#include <bits/stdc++.h>

#define pb push_back
#define int long long
#define ld long double
using namespace std;
const int M = 1000000007;
const int N = 1008;
vector<int> g[N];
char p[N][N];
bool used[N];
int parent[N];
char ans0[N][N];

void dfs(int v) {
    used[v] = true;
    for (auto u : g[v]) {
        if (!used[u]) {
            parent[u] = v;
            if (p[v][u] != '-') {
                ans0[v][u] = p[v][u];
                ans0[u][v] = p[v][u];
                p[v][u] = '-';
                p[u][v] = '-';
            }
            dfs(u);
        } else if (p[v][u] != '-') {
            ans0[v][u] = p[v][u];
            ans0[u][v] = p[v][u];
            p[v][u] = '-';
            p[u][v] = '-';
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> A(m), B(m);
    for (int i = 0; i < m; i++) {
        cin >> A[i] >> B[i];
        A[i]--;
        B[i]--;
    }
    vector<int> C(n);
    for (int i = 0; i < n; i++) cin >> C[i];
    vector<char> ans(m, '>');
    for (int i = 0; i < m; i++) {
        if (C[A[i]] > C[B[i]]) {
            ans[i] = '>';
        } else if (C[A[i]] < C[B[i]]) {
            ans[i] = '<';
        } else {
            g[A[i]].pb(B[i]);
            g[B[i]].pb(A[i]);
            p[A[i]][B[i]] = '>';
            p[B[i]][A[i]] = '<';
        }
    }
    for (int v = 0; v < n; v++) {
        if (!g[v].empty() && !used[v]) {
            dfs(v);
        }
    }
    for (int i = 0; i < m; i++) {
        if (C[A[i]] == C[B[i]]) {
            ans[i] = ans0[A[i]][B[i]];
        }
        if (ans[i] == '>') {
            cout << "->\n";
        } else {
            cout << "<-\n";
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz(a) int(a.size())
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; }
template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; }
template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; }
template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; }
template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; }
template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); }

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif

void solve() {
	int h,w,x,y;
	cin >> h >> w >> x >> y;
	vector<string> v(h);
	for(int i=0; i<h; i++) {
		cin >> v[i];
	}
	--x;
	--y;
	int ret = 0;
	int dir[] = {0, 1, 0, -1, 0};
	for(int d=0; d<4; d++) {
		int i = x, j = y;
		while(i >= 0 and i < h and j >= 0 and j < w and v[i][j] == '.') {
			++ret;
			i += dir[d];
			j += dir[d+1];
		}
	}
	ret -= 3;
	cout << ret << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	solve();
	return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(int i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cerr << #x << " = " << x << " "
#define fin cout << "\n";
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
/*
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
const int M=5e3+7;
const int inf=2e9;
const int mod=998244353;

int h,w,k;
ll dp[M][M];
char mat[M][M];
ll row[M][M],col[M][M];
ll pot3[M];

void go(){
	cin >> h >> w >> k;

	pot3[0] = 1;
	fp(i,1,M) pot3[i] = (pot3[i-1] * 3LL) % mod;

	memset(mat, '0', sizeof mat);
	while (k--){
		int i,j; cin >> i >> j;
		char c; cin >> c;
		mat[i][j] = c;
	}

	fp(i,1,h+1)
		fp(j,1,w+1) row[i][j] = row[i][j-1] + ((mat[i][j] == '0') ? 1 : 0);

	fp(j,1,w+1)
		fp(i,1,h+1) col[j][i] = col[j][i-1] + ((mat[i][j] == '0') ? 1 : 0);

	dp[1][1] = 1;
	fp(i,1,h+1)
		fp(j,1,w+1){
			char c = mat[i][j];
			if (c == 'D' || c == 'X') dp[i+1][j] = (dp[i+1][j] + (pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod;
			if (c == 'R' || c == 'X') dp[i][j+1] = (dp[i][j+1] + (pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod;
			if (c == '0'){
				dp[i+1][j] = (dp[i+1][j] + (2LL * pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod;
				dp[i][j+1] = (dp[i][j+1] + (2LL * pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod;
			}
		}

	/*fp(i,1,h+1){
		cout << i << " : ";
		fp(j,1,w+1) cout << dp[i][j] << " ";
		fin;
	}*/
	if (mat[h][w] == '0') dp[h][w] *= 3;
	cout << dp[h][w] % mod << "\n";
}

int main(){

	fastio;
	int tst=1;
	//cin >> tst;
	fp(i,1,tst+1) go();

	return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(){
  int n, m;
  cin >> n >> m;
  vector<int> A(m);
  for(int i = 0; i < m; ++i)
    cin >> A[i];
  m += 2;
  A.emplace_back(0);
  A.emplace_back(n+1);
  sort(A.begin(), A.end());
  int d = n;
  for(int i = 1; i < m; ++i){
    if(A[i] == A[i-1]+1) continue;
    d = min(d,A[i]-A[i-1]-1);
  }

  int ans = 0;
  for(int i = 1; i < m; ++i){
    int dd = A[i] - A[i-1] - 1;
    if(!dd) continue;
    ans += max(0,(dd+d-1)/d);
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std ;
#define rep(i,n) for( int i = 0 ; i < n ; i++ )
typedef long long ll ;

const ll mod = 1e9 + 7 ;

ll upper_cal(ll n,ll m){
    if( n%m == 0)return n/m ;
    else return n/m + 1 ;
}

int main(){
    ll n  , m ;
    cin >> n >> m ;
    if( m == 0 ){
        cout << 1 << endl ;
        return 0 ;
    }
    if( m == n ){
        cout << 0 << endl ;
        return 0 ;
    }
    vector<ll> A(m+2) ;
    A[0] = -1 ;
    A[m+1] = n ;
    rep(i,m){
        cin >> A.at(i+1) ;
        A.at(i+1)-- ;
    }
    sort(A.begin(),A.end()) ;
    ll range = n ;
    for(ll i =  0 ; i < m + 1 ; i++ ){
        ll tmp = max((ll)0, A.at(i+1) - A.at(i) - 1 ) ;
        if( tmp != 0 ) range = min( range , tmp ) ;
    }
    ll ans = 0 ;
    for(ll i =  0 ; i < m + 1 ; i++ ){
        ll tmp = max((ll)0, A.at(i+1) - A.at(i) - 1 ) ;
        if( tmp != 0 ) ans +=upper_cal(tmp,range) ;
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> dist(N, vector<int>(N, -1));
  queue<pair<int, int>> que;
  for (int i = 0; i < N; i++) {
    dist[i][i] = 0;
    que.push(make_pair(i, i));
  }
  vector<vector<pair<int, int>>> G(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    char c;
    cin >> c;
    u--, v--;
    G[u].emplace_back(make_pair(v, c - 'a'));
    G[v].emplace_back(make_pair(u, c - 'a'));
    if (dist[u][v] < 0) {
      dist[u][v] = 1;
      que.push(make_pair(u, v));
    }
    if (dist[v][u] < 0) {
      dist[v][u] = 1;
      que.push(make_pair(v, u));
    }
  }
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    for (const auto &a : G[x]) {
      for (const auto &b : G[y]) {
        if (a.second == b.second) {
          if (dist[a.first][b.first] < 0) {
            dist[a.first][b.first] = dist[x][y] + 2;
            que.push(make_pair(a.first, b.first));
          }
        }
      }
    }
  }
  cout << dist[0][N - 1] << '\n';
  return 0;
}

#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
using llong = long long;
using vi    = vector<int>;
using vvi   = vector<vi >;
using vvvi  = vector<vvi >;
using pii   = pair<int,int>;
constexpr int    INF  = 1e9;
constexpr llong  LINF = 1e18;
constexpr double EPS  = 1e-12;
constexpr int    MOD  = 1e9+7;
template<class Type>
void line(const Type &a){int cnt=0;for(const auto &elem:a)cerr<<(cnt++?' ':'>')<<elem;cerr<<endl;}

/*ダイクストラ法*/
template<typename T>
class Dijkstra{
	using P=std::pair<T,int>;
	struct Edge{						//Edge:辺の情報を表す構造体.
		int to;
		T cost;
	};

	int vn;								//vn:=(ノード数).
	std::vector<std::vector<Edge> > g;	//g[v]:=(ノードvがもつ辺のリスト).
	std::vector<T> d;					//d[v]:=(ノードsからvへの最短距離).
	std::vector<int> pre;				//pre:=(逆方向経路（経路復元用）).
	T inf;

public:
	//constructor.
	Dijkstra():Dijkstra(0){}
	Dijkstra(size_t vn_,T inf_=1e9):vn(vn_),g(vn_),d(vn_,inf_),pre(vn_,-1),inf(inf_){}

	int get_vn() const{return vn;}
	T get_inf() const{return inf;}
	void add_edge(int from,int to,T cost){//重み付き有向辺を張る.
		assert(0<=from and from<vn);
		assert(0<=to and to<vn);
		g[from].push_back((Edge){to,cost});
	}
	void dijkstra(int s){//sから各ノードへの最短距離を求める. O(|E|log|V|).
		assert(0<=s and s<vn);
		std::fill(d.begin(),d.end(),inf);
		d[s]=0;
		std::fill(pre.begin(),pre.end(),-1);
		std::priority_queue<P,std::vector<P>,std::greater<P> > pque;
		pque.emplace(0,s);
		while(!pque.empty()){
			auto [cost,v]=pque.top();
			pque.pop();
			if(d[v]<cost) continue;
			for(const Edge &e:g[v]){
				if(d[e.to]>d[v]+e.cost){
					d[e.to]=d[v]+e.cost;
					pre[e.to]=v;
					pque.emplace(d[e.to],e.to);
				}
			}
		}
	}
	T dist(int t) const{//ノードsからtへの最短距離を返す.
		assert(0<=t and t<vn);
		return d[t];
	}
	std::vector<int> get_path(int t) const{//ノードtへの最短経路を返す.
		assert(0<=t and t<vn);
		std::vector<int> path;
		for(;t!=-1;t=pre[t]) path.push_back(t);
		std::reverse(path.begin(),path.end());
		return path;
	}
};

int main(){
	int n,m;
	cin>>n>>m;

	vvvi g(n,vvi(26));//g[v][c]:=(vがもつ文字cが書かれる辺のリスト).
	REP(i,m){
		int a,b;
		char c;
		cin>>a>>b>>c;
		a--, b--;

		g[a][c-'a'].push_back(b);
		g[b][c-'a'].push_back(a);
	}

	map<pii,int> mp;//グラフのノード番号参照用.
	int vn=0;
	REP(i,n)for(int j=i;j<n;++j) mp[pii(i,j)]=vn++;

	Dijkstra<llong> ob(vn,LINF);
	int s=mp[pii(0,n-1)], t=vn-1;
	REP(i,n) ob.add_edge(mp[pii(i,i)],t,0);
	REP(i,n)REP(c,26)for(auto j:g[i][c]){
		if(i>=j) continue;
		ob.add_edge(mp[pii(i,j)],t,1);
	}
	REP(i,n)for(int j=i;j<n;++j){
		int u=mp[pii(i,j)];
		REP(c,26)for(auto ni:g[i][c])for(auto nj:g[j][c]){
			int cni=ni, cnj=nj;
			if(cni>cnj) swap(cni,cnj);
			int v=mp[pii(cni,cnj)];
			ob.add_edge(u,v,2);
		}
	}

	ob.dijkstra(s);

	llong ans=ob.dist(t);
	cout<<(ans==ob.get_inf()?-1:ans)<<endl;
}


#line 2 "header.hpp"

//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H

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

// varibable settings
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) { if ((a) < (b)) (a) = (b); }
template <class T, class U> inline void chmin(T &a, const U &b) { if ((a) > (b)) (a) = (b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }

template <class T> T cdiv(T a, T b){ assert(a >= 0 && b > 0); return (a+b-1)/b; }

#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x) sort(all(x)); x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)

typedef long long ll;
typedef long double ld;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;

template <typename T>
using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); }

#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif

#endif /* HEADER_H */
//%snippet.end()%
#line 2 "solve.cpp"
template<class T=ll> using vec = vector<T>;
struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

int solve() {
    ll n;cin>>n;
    ll ans = 0;
    rep(i, n){
        ll a,b;cin>>a>>b;
        ans += (a+b) * (b-a+1) / 2;
    }
    cout << ans << endl;
    return 0;
}

int main(){/*{{{*/
    solve();

    #if defined(PCM) || defined(LOCAL)
    check_input();
    #endif

    return 0;
}/*}}}*/

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define ll int64_t
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define all(V) V.begin(),V.end()
#define PI 3.14159265359
#define fi fixed
#define se setprecision(10)
using namespace std;

int main(){
  ll n;cin >> n;
  ll a[n],b[n];
  rep(i,n) cin >> a[i] >> b[i];
  ll ans = 0;
  rep(i,n){
    ll aaa = a[i];
    ll bbb = b[i];
    ans += (aaa+bbb)*(bbb-aaa+1);
  }
  cout << ans/2 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
int A[402020];

ll mo=3;

ll fact[403030];
ll fact3[403030];

ll modpow(ll a, ll n=mo-2) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

ll comb(int A,int B) {
	int C=A-B;

	if(fact3[A]>fact3[B]+fact3[C]) return 0;
	return fact[A]*modpow(fact[B]*fact[C])%mo;

}

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N;

	fact[0]=1;
	fact3[0]=0;
	for(i=1;i<=N-1;i++) {
		fact[i]=fact[i-1];
		fact3[i]=fact3[i-1];
		x=i;
		while(x%3==0) {
			fact3[i]++;
			x/=3;
		}
		fact[i]=fact[i]*x%3;
	}

	cin>>s;
	ll ret=0;
	FOR(i,N) {
		if(s[i]=='B') A[i]=0;
		if(s[i]=='W') A[i]=1;
		if(s[i]=='R') A[i]=2;

		if(N%2==1) {
			ret+=comb(N-1,i)*A[i];
		}
		else {
			ret-=comb(N-1,i)*A[i];
		}

	}
	ret=(ret%3+3)%3;
	cout<<"BWR"[ret]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef map<ll,ll>::iterator itll;
typedef long double ld;
typedef map<ll,ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(),vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp,it) for(it = mp.begin();it!=mp.end();it++)
#define FOR(i,a,n) for(ll i=a;i<n;i++)
#define FORE(i,a,n) FOR(i,a,n+1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl
#define MIN(x,y) x=min(x,y)
#define MAX(x,y) x=max(x,y)
#define left 0
#define right 1
#define pq priority_queue

const double one = 1.0;
const double PI = 3.14159265/*358979323846*/;
const ll maxn = 1e4+10;
const ld eps = 1e-14;
const ll mod = 1000000007;

map<ll,ll> cnt;

void solve()
{
	ll n,a,b;
	cin>>n>>a>>b;
	ll N = n,A = a,B = b;
	if(n-a+1 <= 0 || n-b+1 <= 0 || n-a-b+1 <= 0)
	{
		cout<<0<<endl;
		return;
	}
	ll x = ((N-A+1)*(N-B+1)-(N-A-B+2)*(N-A-B+1))%mod;
	ll aa = (N-A+1)*(N-A+1)%mod;
	ll bb = ((N-B+1)*(N-B+1)%mod);
	ll cc = (x*x%mod);
	ll res = (aa*bb+mod-cc)%mod;
	cout<<res<<Endl;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll t;
	cin>>t;
	while(t--)solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i, n) ;for (int i = 0;i < (int)(n);i++)
#define rep2(i, s, n) ;for (int i = s; i < (int)(n);i++)
#define ALL(vec)  (vec).begin(),(vec).end()
#define pi 3.1415926535897932384626433832795
#define MAX_INF 9223372036854775807
#define MIN_INF (9223372036854775807+1)
#define sosuu 1000000007

lli combi[65][35];

lli nCr(lli n, lli r)
{
       if(n<r) return 0;
       if(n==r) return combi[n][r] = 1;
       if(r==0) return combi[n][r] = 1;
       if(r==1) return combi[n][r] = n;
       if(combi[n][r]) return combi[n][r];
       return combi[n][r] = nCr(n-1,r) + nCr(n-1,r-1);
}

int main()
{
  int A, B;
  cin >> A >> B;
  lli K;
  cin >> K;
  int now=A+B;
  lli ground=0;
  int all=now;
  rep(i, all){
    if(now==0){
    if(K==0)
      cout << 'a';
      else
        cout << 'b';

      break;

    }
    if(A!=0){
      if(B!=0){
        if(nCr(now-1, A-1)<K){
          cout << 'b';
          K-=nCr(now-1, A-1);
          now --;
          B --;
          continue;
        }//(nCr(now-1, A-1)<K)
        else{
          cout << 'a';
          now --;
          A --;
          continue;
        }//(nCr(now-1, A-1)>=K)
      }//B!=0
      else{

        cout << 'a';
        A--;
        continue;
      }//B==0
    }//A!=0
    else{
      cout << 'b';
      B--;
      continue;
    }//A==0
  }

  cout << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
const int INF = (int)1e9;
const ll INFL = (ll)1e18;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
int dx[]={0, 0, -1, 1};
int dy[]={1, -1, 0, 0};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main()
{
    int h, w, x, y;
    cin >> h >> w >> x >> y;
    char s[110][110] = {'#'};
    x--;
    y--;
    rep(i,h)
    {
        rep(j,w)
        {
            cin >> s[i][j];
        }
    }

    int ans = 1;

    int p = 1;
    if (s[x][y + p] == '.')
    {
        while (s[x][y + p] == '.' && y + p != w)
        {
            p++;
            ans++;
        }
    }

    int i = 1;
    if (y != 0)
    {
        if (s[x][y - i] == '.')
        {
            while (s[x][y - i] == '.')
            {
                i++;
                ans++;
                if (y - i == -1)
                    break;
            }
        }
    }

    int j = 1;
    if (s[x + j][y] == '.')
    {
        while (s[x + j][y] == '.' && x + j != h)
        {
            j++;
            ans++;
        }
    }

    int k = 1;
    if (x != 0)
    {   if (s[x - k][y] == '.')
        {
            while (s[x - k][y] == '.')
            {
                k++;
                ans++;
                if (x - k == -1)
                    break;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a,b,c,mod=4;
long long mypow(long long x,long long times){
	if(times==0)return 1;
	long long ret=mypow(x,times/2);ret=ret*ret%mod;
	if(times%2)ret=ret*x%mod;return ret;
}
bool vis[10];
int x[4];
int main(){
	cin>>a>>b>>c;a%=10;
	int qaq;
	if(a==1){cout<<1;return 0;}
	if(a==0){cout<<0;return 0;}
	if(a==5){cout<<5;return 0;}
	x[1]=a;for(int i=2;i<=4;i++)x[i%4]=x[i-1]*a%10;
	cout<<x[mypow(b,c)];
}

//OM TAT SAT
#include<bits/stdc++.h>
using namespace std;
int main()
{

    int a,b,c;
    cin>>a>>b>>c;
    if(c==0 && a>b)
    {
    cout<<"Takahashi"<<endl;
    }
    else if(c==0 &&a<=b)
    {
    cout<<"Aoki"<<endl;
    }
    else if(c==1 && a<b)
    {
     cout<<"Aoki"<<endl;
    }
    else if(c==1 &&a>=b)
    {
     cout<<"Takahashi"<<endl;
    }

}


// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

template<class T>inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;}
template<class T>inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;}

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示

int main(){
    int n; cin >> n;
    vector<ll> lis(n);
    REP(i,0,n){cin >> lis[i];}
    vector<vector<vector<ll>>> dp(200), pdp(200);
    REP(i,0,n){
        int key = lis[i] % 200;
        dp = pdp;
        REP(l,0,200){
            int count = 0;
            REP(j,0, pdp[l].size() ){
                count++;
                if(count >= 3) break;
                vector<ll> mid = pdp[l][j];
                mid.pb(i);
                if(dp[(l+key)%200].size() >= 2) continue;
                dp[(l+key)%200].pb(mid);
            }
        }
        if(!(dp[(key)%200].size() >= 2)){
            vector<ll> mid;
            mid.pb(i);
            dp[key].pb(mid);
        }
        pdp = dp;
    }
    /*
    REP(i,0,10){
        cout << "#" << i << endl;
        cout << dp[i].size() << endl;;
        REP(l,0,dp[i].size()){
            REP(j,0,dp[i][l].size()){
                cout << dp[i][l][j] << " ";
            }
            cout << endl;
        }
    }
    */
    REP(i,0,200){
        if(dp[i].size() >= 2){
            cout << "Yes" << endl;
            REP(k,0,2){
                cout << dp[i][k].size()<< " ";
                REP(l,0,dp[i][k].size()){
                    cout << dp[i][k][l] + 1 << " ";
                }
                cout << endl;
            }
            return 0;
        }
    }
    cout << "No" <<  endl;
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
    int n;
    cin >> n;
    vector< int > a(n+1);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        a[i] %= 200;
    }

    for(int i = 1; i <= n; i++){
        if(a[i] % 200 == 0){
            puts("Yes");
            if(i == 1){
                cout << "1 " << i+1 << endl;
                cout << "2 " << i << " " << i+1 << endl;
            }else{
                cout << "1 " << i-1 << endl;
                cout << "2 " << i-1 << " " << i << endl;
            }
            return 0;
        }
    }

    vector< vector< int > > dp(n+1, vector< int >(200, 0));
    auto f = [&](auto f, int i, int j, vector< int >& v){
        if(i == 0) return;
        if(dp[i-1][j] > 0){
            f(f, i-1, j, v);
        }else{
            v.push_back(i);
            f(f, i-1, (j-a[i]+200)%200, v);
        }
    };
    dp[0][0] = 1;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < 200; j++){
            dp[i][j] = dp[i-1][j] + dp[i-1][(j-a[i]+200)%200];
            if(dp[i-1][j] > 0 && dp[i-1][(j-a[i]+200)%200] > 0){
                vector< int > b, c({i});
                f(f, i-1, j, b);
                if(b.empty()) continue;
                f(f, i-1, (j-a[i]+200)%200, c);
                puts("Yes");
                sort(b.begin(), b.end());
                cout << b.size();
                for(int i: b) cout << " " << i;
                cout << endl;
                sort(c.begin(), c.end());
                cout << c.size();
                for(int i: c) cout << " " << i;
                cout << endl;
                return 0;
            }
        }
    }
    puts("No");

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>

using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll INF=1e12;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}

int main(void){
    ll N;
    string T;
    cin>>N>>T;
    if(T=="1"){
        cout<<20000000000<<endl;
        return 0;
    }
    if(T=="11" || T=="0"){
        cout<<10000000000<<endl;
        return 0;
    }
    if(T.at(0)=='0') T="11"+T;
    if(T.at(0)=='1' && T.at(1)=='0') T="1"+T;
    if(T.at(T.size()-3)=='0' && T.at(T.size()-2)=='1' && T.at(T.size()-1)=='1') T+="0";
    if(T.at(T.size()-2)=='0' && T.at(T.size()-1)=='1') T+="10";

    if(T.size()%3){
        cout<<0<<endl;
        return 0;
    }
    rep(i,T.size()/3){
        if(T.at(3*i)=='1' && T.at(3*i+1)=='1' && T.at(3*i+2)=='0') continue;;
        cout<<0<<endl;
        return 0;
    }
    cout<<10000000000-T.size()/3+1<<endl;
    return 0;
}

//#include<atcoder/all>
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint1000000007;
using vi = vector<ll>;
using vi2 = vector<vector<ll>>;
using vl = vector<ll>;
using vl2 = vector<vector<ll>>;
using vb = vector<bool>;
using vb2 = vector<vector<bool>>;
using vc = vector<char>;
using vc2 = vector<vector<char>>;
using vs = vector<string>;
using si = set<ll>;
using vs2 = vector<vector<string>>;
using pii = pair<ll, ll>;
using mii = map<ll, ll>;
const int INF = 1e9+1;
const ll LINF = 1e18+1;
const long double LPI = acos(-1.0);
const  double PI = acos(-1.0);
const ll mod = 998244353;

#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)
#define sz(x) (int)(x).size()

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,x) for(ll i=0;i<(int)(x);i++)
#define REPS(i,x) for(ll i=1;i<=(int)(x);i++)
#define RREP(i,x) for(ll i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(ll i=((int)(x));i>0;i--)

#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SORT(v) sort(ALL(v));
#define RSORT(v) sort(RALL(v));
#define IN(type,a) type a;cin >> a;
#define VIN(type,a,n) vector<type> a(n);cin >> a;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl;
#define COUT(x) cout << (x) << endl;
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl;
#define ENDL cout << endl;
#define DB(s,x) cout << (s) << " : " << x << endl;

#define HOGE COUT("HOGE")

template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,sz(v))i>>v[j];return i;}

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> T gcd(T a,T b){if(b==0){return a;}else{return gcd(b, a%b);}}
template<typename T> T lcm(T a,T b){return a*b/gcd(a, b);}
template<typename T> bool isPrime(T a){if(a<=1){return false;}
    for(T i=2;i*i<=a;i++){if (a%i==0) {return false;}}return true;}
template<typename T> T ord(T p,T n){T ans=0;while(true){if(n%p==0){n/=p;ans++;}else{break;}}return ans;}

char janken(char a,char b){
    if (a==b) {
        return a;
    }
    if (a=='R') {
        if (b=='P') {
            return b;
        }
        if (b=='S') {
            return a;
        }
    }
    if (a=='P') {
        if (b=='R') {
            return a;
        }
        if (b=='S') {
            return b;
        }
    }
    if (a=='S') {
        if (b=='P') {
            return a;
        }
        if (b=='R') {
            return b;
        }
    }
    return 'k';

}

int main() {
    IN(ll, n);
    IN(string, s);
    reverse(ALL(s));
    ll start = -1;
    REP(i, n){
        if (s[i]=='0') {
            start = i;
            break;
        }
    }
    if (start==-1) {
        if (s=="1") {
            COUT(20000000000);
            return 0;
        }else if (s=="11"){
            COUT(10000000000);
            return 0;
        }else{
            COUT(0);
            return 0;
        }
    }
    if (start>=3) {
        COUT(0);
        return 0;
    }
    ll count = 0;
    for (ll i = start; i < n; i++) {
        if ((i-start)%3==0) {
            if (s[i]=='0') {
                count++;
            }else{
                COUT(0);
                return 0;
            }
        }else{
            if (s[i]=='1') {

            }else{
                COUT(0);
                return 0;
            }
        }
    }
    if (start==0) {
        COUT(10000000000-count+1);
    }else{
        COUT(10000000000-count);
    }

}


#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>
#include <fstream>
#include <cassert>
#include <numeric>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;
//using mint = modint;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()

#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

	int root(int x)
	{
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}

	bool issame(int x, int y)
	{
		return root(x) == root(y);
	}

	bool merge(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b)
{
	if (a%b == 0)return b;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region 一次元座圧
template <typename T>
vector<T> compress(vector<T> &X)
{
	vector<T> vals = X;
	sort(all(vals));
	vals.erase(unique(all(vals)), vals.end());
	rep(i, (int)X.size())X[i] = lower_bound(all(vals), X[i]) - vals.begin();
	return vals;
}

#pragma endregion
#pragma endregion

int main()
{
	int n; cin >> n;
	map<int, bool> mp;
	int now = 0;
	rep(i, n)
	{
		int q; cin >> q;
		mp[q] = true;
		while (mp[now])++now;
		cout << now << endl;
	}
}

// For the purpose of C++ competitive programming only.
#include "bits/stdc++.h"
using namespace std;

#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (int(a) = (b); (a) < (c); (a)++)
#define FORR(a, b, c) for (int(a) = (b); (a) > (c); (a)--)
#define INF 1000000000000000003
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define endl '\n'

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, f = 0, s = 0, t;
    string fn, sn, tn;
    cin >> N;
    FOR(i, N)
    {
        cin >> tn >> t;
        if (t > f)
        {
            s = f;
            f = t;
            sn = fn;
            fn = tn;
        }
        else
        {
            if (t > s)
            {
                s = t;
                sn = tn;
            }
        }
    }
    cout << sn << endl;
    return 0;
}


#include <bits/stdc++.h>

using ll = long long int;
using P = std::pair<int, int>;

#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rag(con) std::begin(con), std::end(con)

int main()
{
    int n;
    ll x;
    std::cin >> n >> x;

    std::vector<ll> a(n + 1), b(n + 1), c(n);
    std::vector<std::vector<ll>> d(n + 1, std::vector<ll>(2));

    rep(i, n)
            std::cin >>
        a[i];

    for (int i = n; i >= 1; --i)
    {
        b[i] = x / a[i];
        x = x % a[i];
        //std::cerr << b[i];
    }
    //std::cerr << std::endl;

    for (int i = 1; i < n; ++i)
        c[i] = a[i + 1] / a[i];

    d[1][0] = 1;
    if (b[1] > 0)
        d[1][1] = 1;

    for (int i = 2; i <= n; ++i)
    {
        d[i][0] = d[i - 1][0];
        if (b[i] != c[i] - 1)
            d[i][0] += d[i - 1][1];
        d[i][1] = d[i - 1][1];
        if (b[i] > 0)
            d[i][1] += d[i - 1][0];
        //std::cerr << d[i][0] << ' ' << d[i][1] << std::endl;
    }

    std::cout << d[n][0] << std::endl;

    return 0;
}

#include<bits/stdc++.h>
#define int long long
#define HgS 998244353
using namespace std;
char ch[510][510];
char dep[1510];
long long ksm(int x)
{
    long long a=2;
    long long ans=1;
    while(x)
    {
        if(x&1)ans=ans*a%HgS;
        a=a*a%HgS;x>>=1;
    }
    return ans;
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n,m;
    bool Noans=0;
    cin>>n>>m;
    for(int i=2;i<=n+m;++i)dep[i]='.';
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            cin>>ch[i][j];
            if(ch[i][j]=='.')continue;
           // cout<<i<<' '<<j<<'\n';
            if(dep[j+i]=='.')
            dep[j+i]=ch[i][j];
            else if(dep[j+i]!=ch[i][j])Noans=1;
        }
    }
    if(Noans)
    {
        cout<<0;
        return 0;
    }
    int ans=0;
    for(int i=2;i<=n+m;++i)
    {
        if(dep[i]=='.')ans++;
    }
   // cout<<ans<<'\n';
    cout<<ksm(ans)<<'\n';
    return 0;
}


#include <bits/stdc++.h>

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

using namespace std;

int main(){
	string s; cin>>s;

	if(s.length()==1){
		puts(stoi(s)%8==0?"Yes":"No");
		return 0;
	}
	if(s.length()==2){
		string t=s;
		swap(t[0],t[1]);
		puts(stoi(s)%8==0||stoi(t)%8==0?"Yes":"No");
		return 0;
	}

	int freq[10]={};
	for(char c:s) freq[c-'0']++;

	rep(d1,10) if(freq[d1]>0) {
		freq[d1]--;
		rep(d2,10) if(freq[d2]>0) {
			freq[d2]--;
			rep(d3,10) if(freq[d3]>0) {
				freq[d3]--;
				if((100*d1+10*d2+d3)%8==0){
					puts("Yes");
					return 0;
				}
				freq[d3]++;
			}
			freq[d2]++;
		}
		freq[d1]++;
	}
	puts("No");

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int ok[1<<20];
int N,M;
int E[20];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,M) {
		cin>>x>>y;
		x--,y--;
		E[x]|=1<<y;
		E[y]|=1<<x;
	}
	int mask;
	FOR(mask,1<<N) {
		ok[mask]=1;
		FOR(y,N) if(mask&(1<<y)) if(E[y]&mask) ok[mask]=0;
	}
	ll ret=0;
	for(mask=1;mask<(1<<N);mask+=2) if(ok[mask]) {
		int sm=mask^((1<<N)-1);
		for(int x=sm; x>=0; x--) {
			x&=sm;
			if(ok[x]&&ok[x^sm]) ret++;
		}
	}
	cout<<ret*3<<endl;
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


//in the name of god//
// SOBHET BEKHEIR JOOOOOOON E DEL

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define  mem(x,s)	memset(x,s,sizeof(x))
#define all(x)	x.begin(),x.end()
#define _ ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
#define F first
#define S second
#define endi return cout<<'\n',0
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define pll pair<ll,ll>
const int maxn = 3e5 + 10;
int n;
int a[maxn], b[maxn], p[maxn], arr[maxn];
vector <pii>  ans, v;

void sol(){
    cin >> n;
	for(int i = 1;i <= n; i++){
        cin >> a[i];
        v.pb(MP(a[i],i));
	}
	for(int i = 1; i <= n; i++)
        cin >> b[i];
	for(int i = 1;i <= n; i++){
        cin >> p[i];
        arr[p[i]] = i;
	}
	sort(v.begin(),v.end());
	for(int i = 0; i < v.size(); i++){
		int p0 = v[i].S;
		int p1 = arr[p0];
		if(p0 == p1)
            continue;
		if(a[p0] > b[p[p0]] && a[p1] > b[p[p1]]){
			ans.pb(MP(p0,p1));
			swap(p[p0],p[p1]);
			arr[p[p0]] = p0;
			arr[p[p1]] = p1;
		}
		else{
            cout << -1 << '\n';
            return;
		}
	}
	cout << ans.size() << '\n';
	for(auto i: ans)
        cout << i.F <<' '<< i.S << '\n';
}
int main(){_
    int t = 1;
    //cin >>t;
    while(t--){
        sol();
    }
    return 0;
}

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define rep(i, l, r) for (ll i = l; i < (ll)(r); i++)
#define rrep(i, l, r) for (ll i = ((ll)(r) - 1); i >= l; i--)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b) { while(b){ ll c = b; b = a % b; a = c; } return a; }
ll lcm(ll a, ll b) { if(!a || !b) return 0; return a * b / gcd(a, b); }

vector<ll> calc_depth(vector<vector<ll>>& g) {
    ll n = g.size();
    vector<ll> depth(n, INFL);
    deque<pii> que;
    que.push_back({0, 0});
    while (!que.empty()) {
        pii p = que.front();
        que.pop_front();
        ll pn = p.first;
        ll pd = p.second;
        depth[pn] = pd;
        for (auto cn : g[pn]) {
            if (depth[cn] == INFL) {
                que.push_back({cn, pd + 1});
            }
        }
    }
    return depth;
}

void update(vector<ll>& a, vector<ll>& b, vector<ll>& depth, vector<ll>& cnt, ll t, ll e, ll x) {
    if (t == 1) {
        if (depth[a[e]] < depth[b[e]]) {
            cnt[0] += x;
            cnt[b[e]] -= x;
        } else {
            cnt[a[e]] += x;
        }
    } else {
        if (depth[a[e]] < depth[b[e]]) {
            cnt[b[e]] += x;
        } else {
            cnt[0] += x;
            cnt[a[e]] -= x;
        }
    }
}

vector<ll> calc_score(vector<vector<ll>>& g, vector<ll>& cnt) {
    ll n = g.size();
    vector<ll> score(n, INFL);
    deque<pii> que;
    que.push_back({0, cnt[0]});
    while (!que.empty()) {
        pii p = que.front();
        que.pop_front();
        ll pn = p.first;
        ll pd = p.second;
        score[pn] = pd;
        for (auto cn : g[pn]) {
            if (score[cn] == INFL) {
                que.push_back({cn, pd + cnt[cn]});
            }
        }
    }
    return score;
}

template <typename Seq>
void print_vector(Seq seq) {
    for (auto const& value: seq) {
        cout << value << " ";
    }
    cout << endl;
}

signed main() {
    ll n;
    cin >> n;
    vector<ll> a;
    vector<ll> b;
    rep(_, 0, n - 1) {
        ll ai, bi;
        cin >> ai >> bi;
        a.push_back(ai - 1);
        b.push_back(bi - 1);
    }
    vector<vector<ll>> g(n);
    rep(i, 0, n - 1) {
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
    }
    vector<ll> depth = calc_depth(g);
    vector<ll> cnt(n, 0);
    ll q;
    cin >> q;
    rep(_, 0, q) {
        ll t, e, x;
        cin >> t >> e >> x;
        e -= 1;
        update(a, b, depth, cnt, t, e, x);
    }
    vector<ll> score = calc_score(g, cnt);
    for (auto c : score) {
        cout << c << endl;
    }
}


#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N = (int)1e5 + 7;

int n, m;
int x[N];
int ss[N];

int main() {
	scanf("%d %d", &n, &m);
	string s;
	for (int i = 0; i < n; i++) {
		cin >> s;
		for (int j = 0; j < m; j++) {
			x[i] += (s[j] - '0');
		}
		ss[x[i]]++;
	}
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= 20; j++) {
			if (abs(j - x[i]) % 2 == 1) {
				ans += ss[j];
			}
		}
	}
	printf("%lld\n", ans / 2);
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<bitset>

using namespace std;

#define rep(i,N) for(ll i=0;i<N;i++) //0から

typedef long long ll;

ll binary_search(vector<ll> a, ll n, ll key){
    ll right = n, left = -1;
    ll md = (right + left) / 2;

    while(right - left > 1){
        if(a[md] <= key){
            right = md;
        }else{
            left = md;
        }
        md = (right + left) / 2;
    }
    if(left == -1) return -1; //無い場合
    return right;
}

vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
    vector<ll> p(n,0);
    p[0]=1;
    p[1]=1;
    for(ll i=2;i<n;i++){
        if(p[i]==0){
            prime.push_back(i);
            p[i]=i;
        }
        ll k=prime.size();
        for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
            p[i*prime[j]]=prime[j];
        }
    }
}

ll gcd(ll a,ll b){
    if(a<b){
        swap(a,b);
    }
    //a>=b
    ll r=a%b;
    while(r!=0){
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}

ll modexp(ll x, ll a, ll m){ //x^a mod m
    ll ret = 1;
    while (a > 0) {
        if (a & 1) ret = ret * x % m;  // n の最下位bitが 1 ならば x^(2^i) をかける
        x = x * x % m;
        a >>= 1;  // n を1bit 左にずらす
    }
    return ret;
}

ll modinv(ll a, ll m){ //a^{-1} mod m
    ll b=m,u=1,v=0;
    while(b){
        ll t=a/b;
        a-=t*b; swap(a,b);
        u-=t*v; swap(u,v);
    }
    u=(u+m)%m;
    return u;
}

ll nCr(ll n, ll r) {
    ll ans = 1;
    for (ll i = n; i > n - r; --i) {
        ans = ans*i;
    }
    for (ll i = 1 ; i < r + 1; ++i) {
        ans = ans / i;
    }
    return ans;
}

double pi=3.14159265358979323846;

//next_purmitation!!!!!!!!!!!!!
//https://atcoder.jp/contests/abc198/tasks/abc198_d
//cout<<fixed<<setprecision(18);
int main(){
    ll n,m;
    cin>>n>>m;
    vector<string> s(n);
    ll odd=0,even=0;
    rep(i,n){
        cin>>s[i];
        ll cnt=0;
        rep(j,m){
            if(s[i][j]=='1'){
                cnt++;
            }
        }
        if(cnt%2==0){
            even++;
        }
        else{
            odd++;
        }
    }

    cout<<odd*even<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;
using ll = long long;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;
	set<int> s{};
	for (int i = 0; i <= 200000; i++) {
		s.insert(s.end(), i);
	}
	for (int i = 0; i < N; i++) {
		int p;
		cin >> p;
		if (s.find(p) != s.end()) {
			s.erase(p);
		}
		cout << *s.begin() << "\n";
	}

	return 0;
}

#include<fstream>
#include<iostream>
#include<sstream>
#include<vector>
#include<stack>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<tuple>
#include<iomanip>
#include<stdio.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using graph = vector<vector<int>>;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
using ll = long long;
using P = pair<int, int>;

int main(){
  int n;
  cin >> n;
  vector<pair<int,int>> a;
  vector<pair<int,int>> b;
  vector<int> z;

  rep(i,n){
    int c,d;
      cin >> c >> d;
      int f = c+d;
      a.push_back(make_pair(c,i));
      b.push_back(make_pair(d,i));
      z.push_back(f);
    }

sort(a.begin(),a.end());
sort(b.begin(),b.end());
sort(z.begin(),z.end());

int ans = 0;

if(a[0].second!=b[0].second) cout << max(a[0].first,b[0].first) << endl;
else{
  int p = max(a[1].first,b[0].first);
  int q = max(a[0].first,b[1].first);
  int r = min(p,q);
  ans = min(z[0],r);
  cout << ans << endl;
}

}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 21;
vector<int> g[N];
bool vis[N];

vector<int> cc;
void dfs(int u) {
	cc.push_back(u);
	vis[u] = 1;
	for (int v : g[u]) if (!vis[v]) dfs(v);
}

int nxt(int x, int b) {
	x += b + 1;
	if (x > 3) x -= 3;
	return x;
}

short col[N];

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m; cin >> n >> m;
	rep(e, 0, m) {
		int u, v; cin >> u >> v;
		--u; --v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	ll res = 1;
	rep(u, 0, n) {
		if (vis[u]) continue;
		cc.clear(); dfs(u);
		int mx = sz(cc) - 1;
		mx = 1 << mx;

		ll tmp = 0;

		rep(S, 0, mx) {
			for (auto u : cc) col[u] = 0;

			col[u] = 1;
			bool valid = 1;
			int T = S;
			int cur = 0;
			for (auto u : cc)
				for (auto v : g[u])
					if (col[v]) valid &= col[u] != col[v];
					else col[v] = nxt(col[u], T >> cur++ & 1);
			tmp += valid;
		}

		res *= 3 * tmp;
	}
	cout << res << '\n';
}

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
using P = std::pair<ll, ll>;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

std::vector<P> v;

int main(void){
	int n,i;
	ll a,b,c,m,ans=0ll,p=0ll;
	std::cin >> n >> m;
	for(i=0; i<n; ++i){
		std::cin >> a >> b >> c; --a;
		v.push_back(P(a,c));
		v.push_back(P(b,-c));
	}
	std::sort(v.begin(),v.end());
	for(i=1; i<v.l_ength(); ++i){
		p += v[i-1].second;
		ans += std::min(p,m)*(v[i].first-v[i-1].first);
	}
	std::cout << ans << std::endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,a,b) for (int i=a; i<b; ++i)
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll K;
    cin >> K;
    string S,T;
    cin >> S >> T;
    long double ans=0;
    vector <ll> pow10 = {1,10,100,1000, 10000, 100000};
    for (int i=1; i<=9; ++i)
    {
        for (int j=1; j<=9; ++j)
        {
            vector <int> freqS(10, 0), freqT(10, 0), freqcombine(10, 0);
            for (int k=0; k<4; ++k)
            {
                freqS[(S[k]-'0')]++;
                freqT[(T[k]-'0')]++;
                freqcombine[(S[k]-'0')]++;
                freqcombine[(T[k]-'0')]++;
            }
            bool possible=true;
            for (int k=1; k<=9; ++k)
            {
                if (freqcombine[k]>K) {
                    possible=false;
                }
            }
            if (!possible) continue;
            freqS[i]++;
            freqT[j]++;
            ll scoreA =0, scoreB=0;
            for (int k=1; k<=9; ++k)
            {
                scoreA += k*pow10[freqS[k]];
                scoreB += k*pow10[freqT[k]];
            }
            long double prob = ((long double)(K-freqcombine[i]))/(9*K-8);
            freqcombine[i]++;
            prob *= ((long double)(K-freqcombine[j]))/(9*K-9);
            if (scoreA>scoreB) ans += prob;
        }
    }
    cout << fixed << setprecision(20) << ans << "\n";
    return 0;
}

#include <iostream>
using namespace std;
using ll= long long;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define ALL(x) x.begin(),x.end()
#define MOD (int)1e9+7

signed main() {
  ll n, k;
  ll ans=0;
  cin>>n>>k;
  ll a[n];
  ll numc[n];
  REP(i,n) cin>>a[i];
  REP(i,n) numc[i]=0;
  REP(i,n) {
    numc[a[i]]++;
  }
  if(numc[0]>k) numc[0]=k;
  REP(i,n) {
    if(i==0){
      ans+=numc[i];
    }else if(numc[i-1]>numc[i]){
      ans+=numc[i];
    }else{
      numc[i]=numc[i-1];
      ans+=numc[i];
    }

  }

  cout<<ans<<endl;
  return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

int main(){
    long N, K, S = -1, A = -1;
    scanf("%ld %ld", &N, &K);
    while(true){
        S++;
        long cnt;
        if(S < N) cnt = (S + 1) * (S + 2) / 2;
        else if(S < 2 * N - 2) cnt = (N * 9 - N * N * 3 + N * S * 6 - S * 6 - S * S * 2 - 4) / 2;
        else cnt = (N * 3 - S - 1) * (N * 3 - S - 2) / 2;
        if(K <= cnt) break;
        K -= cnt;
    }
    while(true){
        A++;
        long cnt = min(S - A + 1, N * 2 - S + A - 1);
        if(cnt <= 0) continue;
        if(K <= cnt) break;
        K -= cnt;
    }
    long B = S - A - min(S - A, N - 1), C = S - A - B;
    while(--K) B++, C--;
    printf("%ld %ld %ld\n", A + 1, B + 1, C + 1);
    return 0;
}

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	int N;
	ll K;
	cin >> N >> K;
	ll dp[4][3000005] = {0};
	dp[0][0] = 1;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j <= i * N; j++) {
			dp[i + 1][j + 1] += dp[i][j];
			dp[i + 1][j + N + 1] -= dp[i][j];
		}
		for (int j = 1; j <= (i + 1) * N; j++) {
			dp[i + 1][j] += dp[i + 1][j - 1];
		}
	}
	int x;
	for (int i = 3; i <= 3 * N; i++) {
		if (K <= dp[3][i]) {
			x = i;
			break;
		} else {
			K -= dp[3][i];
		}
	}
	for (int i = 1; i <= N; i++) {
		int lo = max(1, x - i - N);
		int hi = min(N, x - i - 1);
		if (lo > hi) continue;
		if (K > (hi - lo + 1)) {
			K -= (hi - lo + 1);
			continue;
		}
		int y = lo + K - 1;
		int z = x - i - y;
		cout << i << " " << y << " " << z << endl;
		return 0;
	}
}


#include<iostream>
#include<vector>
#include<utility>
#include<map>
#include<deque>
#include<numeric>
#include<algorithm>
#include<unordered_map>
#include<cmath>
#include<string>
#include<iomanip>

using namespace std;
using ll = long long int;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(998244353);

vector<ll> ret;

int t, l, r;

int main(void) {
	cin >> t;
	ll n = 0;
	for (int i = 0; i < t; i++) {
		cin >> l >> r;

		n = r - l;
		if (l <= n) {
			n -= l - 1;
			ret.push_back(n * (n + 1) / 2);
		}

		else
			ret.push_back(0);
	}

	for (int i = 0; i < t; i++)
		cout << ret[i] << endl;
	return 0;
}

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

mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define ll           long long
#define int          long long
#define vi           std::vector<int>
#define setbits(x)   __builtin_popcountll(x)
#define pb           push_back
#define fn           for(int i=0;i<n;i++)
#define rep(i,a,b)   for(int i=a;i<b;i++)
#define all(x)       x.begin(), x.end()
#define  ret(x) {cout << #x << endl; return;}
#define PI           3.14159265358979323846
#define pii          pair<ll,ll>
#define fr           first
#define sc           second

const int mod = 1e9+7;
void getio();
ll ceill(ll a, ll b) { return (a + b - 1) / b; }

int ans[1000001];
void solve()
{
    int l, r;
    cin >> l >> r;
    int use = r-2*l;
    int ans = 0;
    // cout << "use " << use << endl;
    if(use<0) ret(0)
    // if(use==0) ret(1)
    if(use%2)
    {
        int n = (use+1)/2;
        int sum = (n * (2*1 + (n-1) * 2)) / 2;
        ans = sum*2 + n;
    }
    else
    {
        int n = use/2 + 1;
        int sum = n * (n-1);
        ans = sum * 2 + n;
    }
    cout << ans << endl;
}

int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    getio();
    int t; cin >> t; while (t--)
    solve();
}

void getio()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}


#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}

const int HIGH=30,WIDTH=30;
int start_x,start_y,end_x,end_y;
pair<int,int> DIRECTION[HIGH][WIDTH];
int DISTANCE[HIGH][WIDTH][2]={0};
int SUB_1[HIGH][WIDTH][2];
string WAY;
int ANSWER;
void solve();
void bulid();
void adjust();
void dijkstr(bool typ);
int diff_x[4]={0,1,0,-1};
int diff_y[4]={-1,0,1,0};
int TEISUU=20;
int D=5;

int f(int a,int b){
	if(a>b) swap(a,b);
	if(b-a==1) return DISTANCE[a/30][a%30][0];
	else return DISTANCE[a/30][a%30][1];
}

int g(int a,int b){
	if(a>b) swap(a,b);
	if(b-a==1) return SUB_1[a/30][a%30][0];
	else return SUB_1[a/30][a%30][1];
}

tuple<int,int,int> h(int a,int b){
	if(a>b) swap(a,b);
	if(b-a==1) return {a/30,a%30,0};
	else return{a/30,a%30,1};
}

char kigou(pair<int,int> a){
	if(a==(pair<int,int>){0,1}) return 'R';
	if(a==(pair<int,int>){1,0}) return 'D';
	if(a==(pair<int,int>){0,-1}) return 'L';
	return 'U';
}

pair<int,int> dif(char a){
	if(a=='R') return {0,1};
	if(a=='L') return {0,-1};
	if(a=='U') return {-1,0};
	return {1,0};
}

//main関数だよーーー
int main() {
	solve();
}

void solve(){
	rep(i,HIGH) rep(j,WIDTH){
		SUB_1[i][j][0]=TEISUU;
		SUB_1[i][j][1]=TEISUU;
		DISTANCE[i][j][0]=5000;
		DISTANCE[i][j][1]=5000;
	}
	int first_roop=167;
	rep(i,1000){
		//cout<<"I"<<endl;
		cin>>start_x>>start_y>>end_x>>end_y;
		dijkstr(i<first_roop);
		bulid();
		cout<<WAY<<endl;
		cin>>ANSWER;
		adjust();
	}
}

void bulid(){
	WAY="";
	int look_x=end_x,look_y=end_y;
	while(look_x!=start_x||look_y!=start_y){
		WAY+=kigou(DIRECTION[look_x][look_y]);
		int a,b;
		tie(a,b)=DIRECTION[look_x][look_y];
		look_x-=a;
		look_y-=b;
	}
	reverse(WAY.begin(),WAY.end());
}

void adjust(){
	int ave=ANSWER/(int) WAY.size();
	int look=start_x*30+start_y;
	rep(i,(int)WAY.size()){
		int a,b,c;
		tie(a,b)=dif(WAY[i]);
		int now=look;
		now+=a*30+b;
		tie(a,b,c)=h(look,now);
		if(SUB_1[a][b][c]==TEISUU) DISTANCE[a][b][c]=ave;
		else{
			int E=(SUB_1[a][b][c]-TEISUU);
			DISTANCE[a][b][c]=(DISTANCE[a][b][c]*E+ave+(E/2))/(E+1);
		}
		SUB_1[a][b][c]++;
		//cout<<a<<" "<<b<<" "<<c<<'\n';
		look=now;
	}
}

void dijkstr(bool type){
	_pq<tuple<int,int>> pq;//<lenth,to>
	vector<int> seen(900,(1<<30));
	int start=start_x*30+start_y;
	seen[start]=0;
	pq.push({0,start});
	while(!pq.empty()){
		int lenth,to;
		tie(lenth,to)=pq.top();
		pq.pop();
		if(lenth<seen[to]){
			continue;
		}
		int x=to/30,y=to%30;
		rep(i,4){
			int now_x=x+diff_x[i];
			int now_y=y+diff_y[i];
			int now=now_x*30+now_y;
			if(now_x==30||now_y==30||now_x<0||now_y<0) continue;
			int new_dis=lenth;
			if(type==true) new_dis+=g(to,now);
			else new_dis+=f(to,now);
			if(chmin(seen[now],new_dis)){
				pq.push({seen[now],now});
				DIRECTION[now_x][now_y]={diff_x[i],diff_y[i]};
			}
		}
	}
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
bool is_prime(ll n){
    if(n<=1)return false;
    REP(i,2,sqrt(n)+1)if(n%i==0)return false;
    return true;
}
int main(){
    ll a,b;cin>>a>>b;
    vi s;
    REP(i,2,72)if(is_prime(i))s.pb(i);
    vvi t(20);
    ll r=1;
    REP(i,a,b+1){
        bool c=false;
        rep(j,20)if(i%s[j]==0){
            c=true;t[j].pb(i);break;
        }
        if(!c)r*=2;
    }
    vb used(20,false);
    ll ans=0;
    function<void(ll)> dfs=[&](ll c){
        if(c==20){
            ans++;return;
        }
        dfs(c+1);
        if(used[c])return;
        for(ll x:t[c]){
            bool ok=true;
            rep(i,20)if(used[i]&&x%s[i]==0)ok=false;
            if(ok){
                rep(i,20)if(x%s[i]==0)used[i]=true;
                dfs(c+1);
                rep(i,20)if(x%s[i]==0)used[i]=false;
            }
        }
    };
    dfs(0);
    ans*=r;
    out(ans);
}


#include<iostream>
#include<vector>
#include<cmath>
#include<unordered_set>
#include<algorithm>

using namespace std;

int main(){
	int n;cin>>n;
	vector<int> a(n),b(n),c(n),d(n);
	for(int i=0;i<n;i++)cin>>a[i]>>b[i];
	for(int i=0;i<n;i++)cin>>c[i]>>d[i];
	if(n==1){
		puts("Yes");
		return 0;
	}/*
	if(n==2){
		if((a[1]-a[0])*(a[1]-a[0])+(b[1]-b[0])*(b[1]-b[0])
				==(c[1]-c[0])*(c[1]-c[0])+(d[1]-d[0])*(d[1]-d[0]))
			puts("Yes");
		else puts("No");
		return 0;
	}*/
	vector<pair<long double,long double>> ab;
	int A=b[1]-b[0],B=-a[1]+a[0],C=a[1]*b[0]-a[0]*b[1];
	for(int i=0;i<n;i++){
		long double y=(A*a[i]+B*b[i]+C)/(long double)(sqrtl(A*A+B*B));
		long double x=(a[i]-a[0])*(a[i]-a[0])+(b[i]-b[0])*(b[i]-b[0])-y*y;
		x=abs(x);
		x=sqrt(x);
		ab.push_back(make_pair(x,y));
	}stable_sort(ab.begin(),ab.end());
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j)continue;
			A=d[i]-d[j],B=-c[i]+c[j],C=c[i]*d[j]-c[j]*d[i];
			vector<pair<long double,long double>> cd;
			for(int k=0;k<n;k++){
				long double y=(A*c[k]+B*d[k]+C)/(long double)(sqrtl(A*A+B*B));
				long double x=(c[k]-c[j])*(c[k]-c[j])+(d[k]-d[j])*(d[k]-d[j])-y*y;
				x=abs(x);
				x=sqrt(x);
				cd.push_back(make_pair(x,y));
			}int f=0;
			stable_sort(cd.begin(),cd.end());
			for(int k=0;k<n;k++)if(ab[k]!=cd[k])f=1;
			if(f==0){
				puts("Yes");
				return 0;
			}
		}
	}puts("No");
	return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
#include<set>
#include<cstdlib>
#include<cmath>
#include<numeric>//fill
#include<iomanip>
#include<functional>//https://cpprefjp.github.io/reference/functional/function.html
#include<cstdlib>//https://cpprefjp.github.io/reference/cstdlib.html
#include<queue>
#include<deque>
#include<cassert>
#include<stack>
#include <iterator>     // std::back_inserter
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
const double PI = acos(-1);
using ll = long long;
using P = pair<ll,ll>;
#define rep(i,n)for(ll i=0;i<(n);i++)
#define repp(x,arr) for(auto& x:arr)
#define repp2(x,y,arr) for(auto& [x,y]:arr)
#define all(a) (a.begin()),(a.end())
const ll MOD = 1e9 + 7;
const ll inf = (ll)1e18+6;

// デフォルトは整数部も含めた桁数指定
//cout <<setprecision();
ll myPow(ll x, ll n, ll m) {
	if (n == 0)
		return 1;
	if (n % 2 == 0)
		return myPow(x * x % m, n / 2, m);
	else
		return x * myPow(x, n - 1, m) % m;
}
double dl[4] = { 0,0,0.1,0.1 };
double dr[4] = { 0,-0.1,0,-0.1 };
int main() {
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<pair<double, double>>s(N), t(N);
	map<pair<double, double>,bool>m;
	rep(i, N) {
		cin >> t[i].first >> t[i].second;

	}
	rep(i, N) {
		cin >>s[i].first >> s[i].second;
		m[s[i]] = true;
	}

	if (N == 1) {
		cout << "Yes";
		return 0;
	}
	double a, b,a2,b2;
	a = s[0].first, b = s[0].second;
	a2 = s[1].first, b2 = s[1].second;
	double p1;
	p1 = atan((b2 - b) / (a2 - a));
	bool ans = false;
	//cout << "指標は" << a << " " << b << endl;
	rep(i, N) {
		double q, r,c, d;
		c = t[i].first, d = t[i].second;
		for (int j = 0; j < N; j++) {
			if(j==i)continue;
			double c2, d2,p,p2;
			c2 = t[j].first;
			d2 = t[j].second;
			c2 -= c;
			d2 -= d;
			p = p1-atan(d2 / c2);
			p2 = p + PI;
			{
				double A1, B1, x, y, x2, y2;
				A1 = t[j].first - c;
				B1 = t[j].second - d;
				x = A1 * cos(p) - B1 * sin(p);
				y = A1 * sin(p) + B1 * cos(p);
				x2 = A1 * cos(p2) - B1 * sin(p2);
				y2 = A1 * sin(p2) + B1 * cos(p2);
				x += a;
				y += b;
				x = round(x);
				y = round(y);
				x2 += a;
				y2 += b;
				x2 = round(x2);
				y2 = round(y2);
				if (x==a2&&y==b2) {
					p = p;

				}
				else if (x2 == a2 && y2 == b2) {
					p = p2;
				}
				else {
					continue;
				}

			}
			rep(k, N) {
				double A1, B1,x,y,x2,y2;
				A1 = t[k].first - c;
				B1 = t[k].second - d;
				x = A1 * cos(p) - B1 * sin(p);
				y = A1 * sin(p) + B1 * cos(p);
				//x2 = A1 * cos(p2) - B1 * sin(p2);
				y2 = A1 * sin(p2) + B1 * cos(p2);
				//cout << x << " " << y << endl;
				//cout << x2 << " " << y2 << endl;
				x += a;
				y+= b;
				x = round(x);
				y = round(y);
				if (m[{x,y}] == false) {
				//cout << "break"<<endl;
					break;
				}
				if (k == N - 1)ans = true;

			}
		}

	}
	if (ans)cout << "Yes";
	else cout << "No";
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5;
const int maxq=3e5;
char ss[maxn + 1 << 1];
int main()
{
	int n,q,t,a,b;
	scanf("%d\n%s\n%d\n",&n,ss,&q);
	int base=0;
	int l=n<<1;
	while(q-->0)
	{
		scanf("%d %d %d",&t,&a,&b);
		if(t==2)
		{
			base^=n;
		}
		else
		{// T==1
			a=(a-1+base)%l;
			b=(b-1+base)%l;
			swap(ss[a],ss[b]);
		}
	}
	for(int i=0;i<l;i++)
	{
		putchar(ss[(base+i)%l]);
	}
 }

#include <bits/stdc++.h>

#define rep(i, n) for (auto i = 0; i < n; ++i)
#define ALL(a) a.begin(), a.end()

using namespace std;

using ll = long long int;

const int MOD_NUM = 1e9 + 7;

int main() {
    // Input
    int N, Q;
    string S;
    cin >> N >> S >> Q;
    vector<int> T(Q), A(Q), B(Q);
    rep(i, Q) cin >> T[i] >> A[i] >> B[i];

    // Process
    string ans(S);
    int reverse = 0;
    rep(i, Q) {
        if (T[i] == 1) {
            int a = A[i] - 1;
            int b = B[i] - 1;
            if (reverse % 2 == 1) {
                a = (a + N) % (N * 2);
                b = (b + N) % (N * 2);
            }

            char tmp = ans[a];
            ans[a] = ans[b];
            ans[b] = tmp;
        } else
            reverse++;
    }
    if (reverse % 2 == 1) {
        string tmp1 = ans.substr(N);
        string tmp2 = ans.substr(0, N);
        ans = tmp1 + tmp2;
    }

    // Output
    cout << ans << endl;

    return 0;
}


///Astalavista Code........~sachinait
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define Br cout<<endl
#define int long long
#define pb push_back
#define mp make_pair
#define double long double
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define PT(x) cout << (x) << endl
#define PTT(x) cout << (#x) << " is " << (x) << endl;
#define debug(x) cout<<"~~"<<#x<<" => "<<x<<endl;
#define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int M = 1e9 + 7;
const int N = 2e6 + 10;
const int IM = 1e15 + 37;
const double PI = 3.1415926535897932384;
inline void PP(int n) { cout << n << " ";}
void IN(vector<int>&arr,int n,int x=0){for(int i=x;i<n+x;i++)cin>>arr[i];}
void PV(vi v) { for (int i = 0 ; i < v.size() ; i++) cout << v[i] << " " ; cout << endl;}
void PVR(vi v) { for (int i = v.size()-1 ; i >=0 ; i--) cout << v[i] << " " ; cout << endl;}
void PVV(vii v) {for (int i = 0 ; i < v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;}
void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << " " ; cout << endl;}
void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];}
// <=================================================================================================================================
// <=================================================================================================================================

void fun(){
    int h,w;cin>>h>>w;
    vector<string>a(h);
    for(int i=0;i<h;i++)
    {
        cin>>a[i];
    }
    int ans=0;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(j+1<w && a[i][j]=='.' && a[i][j+1]=='.')
            {
                ans++;
            }
            if(i+1<h && a[i][j]=='.' && a[i+1][j]=='.')
            {
                ans++;
            }
        }
    }
    cout<<ans;
}

signed  main() {
    lets_go

    int t;
    t=1;
    // cin >> t;
    while (t--) {
         fun();
    }

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 20 + 10;
int n, m, ans;
char s[maxm];
int a[2];
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%s", s);
		int ret = 0;
		for(int j = 0; j < m; ++j)
			ret = (ret << 1) + s[j] - '0';
		++a[__builtin_popcount(ret) & 1];
	}
	printf("%lld\n", 1ll * a[0] * a[1]);
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 2e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = INF)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

struct comp
{
	int l, t, lt, val;
	comp()
	{
		l = 0;
		t = 0;
		lt = 0;
		val = 0;
	}
	comp(int l, int t, int lt, int val)
		: l(l), t(t), lt(lt), val(val) {}
	int sum() { return l + lt + t; }
};

void solve(int tc)
{
	int n, m;
	cin >> n >> m;
	char a[n + 1][m + 1];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];

	comp dp[n + 1][m + 1];
	for (int i = 0; i <= n; i++)
	{
		for (int j = 0; j <= m; j++)
		{
			if (!i || !j)
				continue;
			if (a[i][j] == '#')
			{
				dp[i][j] = comp();
				continue;
			}
			if (i == 1 && j == 1)
			{
				dp[1][1] = comp(0, 0, 0, 1);
				continue;
			}
			dp[i][j] = comp();
			dp[i][j].l  += (dp[i][j - 1].val + dp[i][j - 1].l) % MOD;
			dp[i][j].t  += (dp[i - 1][j].val + dp[i - 1][j].t) % MOD;
			dp[i][j].lt += (dp[i - 1][j - 1].val + dp[i - 1][j - 1].lt) % MOD;
			dp[i][j].val = (dp[i][j].sum()) % MOD;
		}
	}

	cout << dp[n][m].val;
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}
	return 0;
}