#include <bits/stdc++.h>

typedef long double ld;
#define int long long

#define gcd            __gcd
#define endl           "\n"
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            998244353
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define pb             push_back
#define all(x)         x.begin(), x.end()
#define f              first
#define s              second
#define lb             lower_bound
#define ub             upper_bound
#define ins            insert
#define sz(x)          (int)(x).size()
#define mk             make_pair
#define deci(x, y)     fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define nitin          ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI             3.141592653589793238
#define mem0(x)        memset(x,0,sizeof x)
#define mem1(x)        memset(x,-1,sizeof x)
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.f << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NITIN
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
int power(int x, int y, int p) {
    int res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int modi(int a, int m) {
    return power(a, m - 2, m);
}
void solve() {
    int n,m,t;
    cin>>n>>m>>t;
    int inv=modi(3,mod);
    char a[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            a[i][j]='?';
        }
    }
    for(int p=0;p<t;p++) {
        int i, j;
        char c;
        cin >> i >> j >> c;
        --i;
        --j;
        a[i][j] = c;
    }
    int dp[n][m];
    mem0(dp);
    dp[0][0]=power(3,n*m-t,mod);
    for(int i=1;i<n;i++){
        if(a[i-1][0]=='?')
            dp[i][0]=(((dp[i-1][0]*2)%mod)*inv)%mod;
        if(a[i-1][0]=='X' || a[i-1][0]=='D')
            dp[i][0]=dp[i-1][0];
    }
    for(int j=1;j<m;j++){
        if(a[0][j-1]=='?')
            dp[0][j]=(((dp[0][j-1]*2)%mod)*inv)%mod;
        if(a[0][j-1]=='X' || a[0][j-1]=='R')
            dp[0][j]=dp[0][j-1];
    }
    for(int i=1;i< n;i++) {
        for (int j = 1; j < m; j++) {
            int l=0,r=0;
            if (a[i - 1][j] == '?')
                (l += (((dp[i - 1][j] * 2)%mod)*inv)%mod)%=mod;
            if (a[i - 1][j] == 'X' || a[i - 1][j] == 'D')
                (l += dp[i - 1][j])%=mod;
            if (a[i][j - 1] == '?')
               ( r += (((dp[i][j - 1] * 2)%mod)*inv)%mod)%=mod;
            if (a[i][j - 1] == 'X' || a[i][j - 1] == 'R')
                (r += dp[i][j - 1])%=mod;
            (dp[i][j]=l+r)%=mod;
        }
    }
//    for(int i=0;i<n;i++){
//        for(int j=0;j<m;j++){
//            cout<<dp[i][j]<<" ";
//        }
//        cout<<endl;
//    }
    cout<<dp[n-1][m-1]<<endl;
}

int32_t main() {
    nitin;

    solve();
}

#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
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; }
ostream &operator<<(ostream &os, const vector<lli> &x){
    os << "{";
    for(int i = 0; i < x.size(); i++){
        if(i < x.size()-1) os << x[i] << ", ";
        else os << x[i];
    }
    os << "}";
    return os;
}
const lli mod = 998244353;
template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< mod >;

int main(void){
    lli h, w, k;
    cin >> h >> w >> k;
    vector<vector<char>> m(h, vector<char>(w, '.'));
    rep(i, k){
      lli hi, wi;
      char ci;
      cin >> hi >> wi >> ci;
      hi--;
      wi--;
      m[hi][wi] = ci;
    }
    vector<vector<modint>> dp(h+1, vector<modint>(w+1));
    dp[0][0] = 1;
    modint v3 = 3;
    modint inv3 = v3.inverse();
    rep(i, h){
      rep(j, w){
        if(m[i][j] == 'R'){
          dp[i][j+1] += dp[i][j];
        }else if(m[i][j] == 'D'){
          dp[i+1][j] += dp[i][j];
        }else if(m[i][j] == 'X'){
          dp[i][j+1] += dp[i][j];
          dp[i+1][j] += dp[i][j];
        }else{
          dp[i][j+1] += dp[i][j]*inv3*2;
          dp[i+1][j] += dp[i][j]*inv3*2;
        }
      }
    }
    modint tmp = 3;
    modint ans = dp[h-1][w-1]*(tmp.pow(h*w-k));
    cout << ans << endl;
    return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
   #include <atcoder/all>
   using namespace atcoder;
#endif
#define all(v) v.begin(), v.end()
using in = long long;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i,a,b) for(in i=(in)(a);i<(in)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const in MOD = 1000000007;
const in INF=(in)1e18+7;
const in inf=max(in(1e18)+7,in(1e9)+7);
using P=pair<in,in>;
using T=tuple<in,in,in>;
vector<in> dx={0,1,-1,0};
vector<in> dy={1,0,0,-1};
template <typename Typ>
bool chmin(Typ &a, const Typ& b) {if (a > b) {a = b; return true;} return false;}
template <typename Typ>
bool chmax(Typ &a, const Typ& b) {if (a < b) {a = b; return true;} return false;}
template <typename Typ>
Typ ceil(Typ x, Typ y){return (x+y-1)/y;}
#ifdef  _DEBUG
    #define debug(x) cout<<"debug:"<<x<<endl
    #define vdebug(x) cout<<"debug:";\
                      for(auto asdf:x) cout<<asdf<<" ";\
                      cout<<endl
#else
    #define debug(x)
    #define vdebug(x)
#endif

int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
cout << fixed << setprecision(12);
    in k;  cin>>k;
    string s,t; cin>>s>>t;
    vector<in> zan(9,k);
    vector<in> cs(9,0),ct(9,0);
    rep(i,4){
        zan[s[i]-'1'] --;
        zan[t[i]-'1'] --;
        cs[s[i]-'1'] ++;
        ct[t[i]-'1'] ++;
    }
    in sum=0;

    rep(i,9)rep(j,9){
        if(zan[i]==0 or zan[j]==0) continue;
        in ns=0,nt=0;
        cs[i]++;
        ct[j]++;
        rep(l,9){
            in s10=1,t10=1;
            rep(m,cs[l]) s10 *=10;
            rep(m,ct[l]) t10 *=10;
            ns += s10*(l+1);
            nt += t10*(l+1);
        }
        cs[i]--;
        ct[j]--;
        if(ns>nt){
            if(i==j and zan[i]>=2) sum += zan[i]*(zan[i]-1);
            if(i!=j) sum += zan[i]*zan[j];
            vector<in> dv(2);
            dv[0]=i+1;dv[1]=j+1;
            vdebug(dv);
        }
    }
    in s_z=0;
    rep(i,9) s_z += zan[i];
    s_z *= (s_z-1);
    double ans=sum;
    ans /=double(s_z);
    debug(sum);
    debug(s_z);
    cout<<ans<<"\n";

}/*
./problem.exe
*/

#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5 + 10;

int n,a[N],b[N],lisan[N],num,c[N],tmp[N];

ll ans = 0;

queue <int> q[N];

void mysort(int l,int r)
{
	if(l == r) return ;
	int mid = (l + r) >> 1;
	mysort(l,mid); mysort(mid+1,r);
	int lnow = l , rnow = mid + 1 , top = l-1;
	while(lnow <= mid || rnow <= r){
		if((c[lnow] <= c[rnow] && lnow <= mid)|| rnow > r){
			tmp[++top] = c[lnow]; lnow ++;
		}
		else{
			tmp[++top] = c[rnow]; rnow ++;
			ans += mid - lnow + 1;
		}
 	}
 	for(int i=l;i<=r;i++) c[i] = tmp[i];
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n;i++) a[i] += i , lisan[i] = a[i];
	sort(lisan + 1 , lisan + 1 + n);
	num = unique(lisan + 1 , lisan + 1 + n) - lisan - 1;
	for(int i=1;i<=n;i++){
		a[i] = lower_bound(lisan + 1 , lisan + 1 + num , a[i]) - lisan;
		q[a[i]].push(i);
	}
	for(int i=1;i<=n;i++){
		b[i] += i;
		int x = lower_bound(lisan + 1 , lisan + 1 + num , b[i]) - lisan;
		if(b[i] != lisan[x]){
			cout<<"-1"; return 0;
		}
		if(q[x].empty()){
			cout<<"-1"; return 0;
		}
		int o = q[x].front();
		q[x].pop();
		c[i] = o;
	}
	mysort(1,n);
	cout<<ans;
	return 0;
}


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

int main()
{
	int n;
	cin >> n;
	vector<int> p(n);
	for(int i = 0; i < n; i++) cin >> p[i];
	int l = 0;
	vector<int> ans;
	for(int r = 0; r < n; r++) {
		if(p[l] == l + 1) {
			cout << -1 << endl;
			return 0;
		}
		if(p[r] == l + 1) {
			for(int j = r - 1; j >= l; j--) {
				swap(p[j], p[j + 1]);
				ans.push_back(j + 1);
			}
			for(int j = l; j < r; j++) {
				if(j + 1 != p[j]) {
					cout << -1 << endl;
					return 0;
				}
			}
			l = r;
		}
	}
	for(auto&& e : ans) cout << e << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
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; }
const ll MOD = 1e9+7;

int main(){
    ll N;
    cin >> N;
    vector<ll> P(N);
    vector<ll> X(N);
    REP(i,N){
        ll p;
        cin >> p;
        P[i] = p-1;
        X[p-1] = i;
    }

    vector<ll> ans;
    vector<bool> used(N, false);
    bool valid = true;
    REP(i,N){
        ll idx = X[i];
        for(int k = idx; k > i; k--){
            if(used[k-1]){
                valid = false;
                break;
            }
            X[P[k]] = k-1;
            X[P[k-1]] = k;
            swap(P[k], P[k-1]);
            used[k-1] = true;
            ans.push_back(k);
        }

        if(!valid)
            break;
    }

    REP(i,N){
        if(P[i] != i){
            valid = false;
            break;
        }
    }

    if(ans.size() != N-1)
        valid = false;

    if(valid){
        for(auto&& a : ans)
            cout << a << endl;
    }
    else{
        cout << -1 << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(uint i=0; i<(n); i++)

uint GCD(uint a,uint b){ return b?GCD(b,a%b):a; }

uint sieve[32768]={};
vector<uint> P;

vector<pair<uint,uint>> factorize(uint N){
  vector<pair<uint,uint>> res;
  for(uint p:P){
    if(N%p) continue;
    if(N<p) break;
    res.push_back({p,0});
    while(N%p==0){ N/=p; res.back().second++; }
  }
  if(N!=1) res.push_back({N,1});
  return move(res);
}
vector<uint> divs(vector<pair<uint,uint>> F){
  vector<uint> res = {1};
  for(auto f:F){
    uint t=res.size() * f.second;
    rep(i,t) res.push_back(res[i] * f.first);
  }
  return move(res);
}

uint N;
uint A[2000];
unordered_map<uint,uint> D;
uint m;

int main(){
  scanf("%u",&N); rep(i,N) scanf("%u",&A[i]);
  m=1000000000; rep(i,N) m=min(m,A[i]);
  for(uint i=2; i<32768; i++) if(!sieve[i]){
    P.push_back(i); sieve[i]=i;
    for(uint p:P) if(p<=sieve[i]) if(i*p<32768) sieve[i*p]=p; else break;
  }
  rep(i,N){
    auto B=divs(factorize(A[i]));
    for(uint d:B){
      uint x=D[d];
      if(x==0) D[d]=A[i];
      else D[d]=GCD(x,A[i]);
    }
  }
  uint ans=0;
  for(auto d:D) if(d.first==d.second) if(d.first<=m) ans++;
  printf("%d\n",ans);
  return 0;
}

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

int gcd(int a, int b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

unordered_map<int,int> mp;

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

	cin >> N;
	vector<int>v(N);
	for (auto &i : v)cin >> i;
	sort(v.begin(), v.end());
	for (int i = 0; i < N; i++) {
		if (i&&v[i] == v[i - 1])continue;
		for (int j = 1; j*j <= v[i]; j++) {
			if (j > v[0])break;
			if (v[i]%j)continue;
			if (mp.find(j) == mp.end()) {
				mp[j] = v[i] / j;
			}
			else {
				mp[j] = gcd(mp[j], v[i] / j);
			}
			j = v[i] / j;
			if (j > v[0]) {
				j = v[i] / j;
				continue;
			}
			if (mp.find(j) == mp.end()) {
				mp[j] = v[i] / j;
			}
			else {
				mp[j] = gcd(mp[j], v[i] / j);
			}
			j = v[i] / j;
		}
	}
	int ans = 0;
	for (auto i : mp) {
		if (i.second == 1)ans++;
	}
	cout << ans << endl;
}


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

#define fast ios::sync_with_stdio(false);cin.tie(NULL)
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define eb emplace_back
#define em emplace
#define mp make_pair
#define pre(a) cout<<fixed; cout.precision(a);

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;

int n;
ll a, c;
long double b;

int main() {
	fast;
	pre(15)

	cin >> n;

	for(int i=0; i<n; i++) {
		ll x;
		cin >> x;

		a += abs(x);
		b += x * x;
		c = max(c, abs(x));
	}

	cout << a << "\n" << sqrt(b) << "\n" << c;
}

#include<cstdio>
#include<iostream>
#include<cctype>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;

int main() {
    int n;
    long long ans[3] = {};
    cin >> n;
    int x[n] = {};
    for (int i = 0; i < n; i++) {
        cin >> x[i];
        if (x[i] < 0) {
            x[i] = -x[i];
        }
        ans[0] += x[i];
        ans[1] += ((long long)x[i])*x[i];
        if (x[i] > ans[2]) {
            ans[2] = x[i];
        }
    }
    cout << ans[0] << endl;
    printf("%.10f\n", sqrt(ans[1]));
    cout << ans[2] << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;

ll mpow(ll x,ll n){
    if(n==0) return 1;
    else if(n%2) return x*mpow(x,n-1);
    return mpow(x*x,n/2);
}

ll mfrac(ll n){
    if(n==0) return 1;
    else return n*mfrac(n-1);
}

ll mperm(ll n,ll r){
    ll res=1;
    for(ll i=0;i<r;i++) res=res*(n-i);
    return res;
}

ll mcomb(ll n,ll r){
    if(r>n-r) r=n-r;
    ll res=mperm(n,r);
    res/=mfrac(r);
    return res;
}

ll L;
ll p[201][201];

int main(){
    cin>>L;

    p[0][0]=1;
    repn(n,L){
        p[n][0]=1;
        p[n][n]=1;
        repn(k,n-1) p[n][k]=p[n-1][k-1]+p[n-1][k];
    }
    cout<<p[L-1][11]<<endl;

    return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define int long long
#define pb push_back
#define deb(x) cerr << #x << " is " << x << endl;
const int N = 3e5 + 5, MOD = 998244353; //1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
#define ff first
#define ss second
int T;

int maxx = (1 << 16);

int a[(1 << 16) + 5];
pair<int, int> tree[4*(1 << 16) + 5];

void build(int node,int start,int end){
    if(start==end)  tree[node].ff=a[start], tree[node].ss=start;
    else{
        int mid=(start+end)/2;
        build(2*node,start,mid);
        build(2*node+1,mid+1,end);
        if(tree[2*node].ff>tree[2*node+1].ff)
            tree[node].ff=tree[2*node].ff, tree[node].ss=tree[2*node].ss;
        else
            tree[node].ff=tree[2*node+1].ff, tree[node].ss=tree[2*node+1].ss;
    }
}
void update(int node,int start,int end,int idx,int val){
    if(start==end){
        a[idx]=val;
        tree[node].ff=val;
    }
    else{
        int mid=(start+end)/2;
        if(idx>=start&&idx<=mid)        update(2*node,start,mid,idx,val);
        else                            update(2*node+1,mid+1,end,idx,val);
        if(tree[2*node]>tree[2*node+1])
            tree[node].ff=tree[2*node].ff, tree[node].ss=tree[2*node].ss;
        else
            tree[node].ff=tree[2*node+1].ff, tree[node].ss=tree[2*node+1].ss;
    }
}
pair<int, int> query(int node,int start,int end,int l,int r){
    if((l>end) || (start>r))    return {-1000000005,100005};
    if((l<=start) && (r>=end))    return tree[node];
    pair<int, int> p1,p2;
    int mid=(start+end)/2;
    p1=query(2*node,start,mid,l,r);
    p2=query(2*node+1,mid+1,end,l,r);
    if(p1.ff>p2.ff)         return p1;
    else                    return p2;
}

signed main()
{
	int n;
	cin>>n;
	maxx = (1 << n);

	for(int i = 1; i <= maxx; i++){
        cin>>a[i];
        //update(1, 1, maxx, i, a[i]);
	}
	build(1, 1, maxx);
	auto q1 = query(1, 1, maxx, 1, maxx/2), q2 = query(1, 1, maxx, maxx/2 + 1, maxx);
    if(q1.first > q2.first) cout<<q2.second;
    else                    cout<<q1.second;

	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

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

using namespace std;

int n, a[200010], b[200010], c[200010];
map<int, vector<int>> m;
map<int, int> id;
ll res;

int bit[200010];

ll sum(int i){
    ll s = 0;
    while(i>0){
        s += bit[i];
        i -= i&(-i);
    }
    return s;
}

void add(int i, int x){
    while(i < 200010){
        bit[i] += x;
        i += (i&(-i));
    }
}

int main(){
    cin >> n;
    rep(i,n){cin>>a[i];a[i]+=i;}
    rep(i,n){cin>>b[i];b[i]+=i;}
    rep(i,n){
        m[b[i]].push_back(i);
    }
    rep(i,n){
        int t = a[i];
        if(m[t].size() <= id[t]){
            cout << -1 << endl;
            return 0;
        }
        else {
            c[i] = m[t][id[t]]+1; id[t]++;
        }
    }

    rep(i,n){
        res += i - sum(c[i]);
        add(c[i], 1);
    }
    cout << res << endl;
}

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

void DBG() { cerr << endl; }
template<class Head, class... Tail>
void DBG(Head H, Tail... T) { cerr << ' ' << H; DBG(T...); }
#define dbg(...) cerr << "(" << (#__VA_ARGS__) << "):", DBG(__VA_ARGS__)

using ll = long long;
using ld = long double;

#define sqr(a) ll(a) * (a)
#define siz(a) int(a.size())
#define ints(a...) int a; read(a)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define trav(i, v) for (const auto i : v)
#define Fill(a, b) memset(a, b, sizeof(a))
#define rep(i, b, n) for (auto i = b; i < n; i++)
#define per(i, b, n) for (auto i = b; i >= n; i--)
#define unify(a) sort(all(a)), a.resize(unique(all(a)) - a.begin())

template <class T> using vec = vector<T>;
template <class... Args> inline void read(Args&... args) { ((cin >> args), ...); }
template <class... Args> inline void show(Args... args) { ((cout << args << " "), ...); }
template <class T1, class T2> inline bool ckmin(T1 &a, T2 b) { return a > b ? a = b, 1 : 0; }
template <class T1, class T2> inline bool ckmax(T1 &a, T2 b) { return a < b ? a = b, 1 : 0;  }
template <class T> inline void operator>> (istream& in, vector<T>& v) { rep(i, 0, siz(v)) in >> v[i]; }
template <class T> inline void operator<< (ostream& out, const vector<T>& v) { rep(i, 0, siz(v)) out << v[i] << " \n"[i + 1 == siz(v)]; }

const int MOD = 1e9 + 7;

void solve() {
    ints(n);
    string s; read(s);
    auto idx = s.find("fox");
    while (idx != string::npos) {
        s.erase(idx, 3);
        idx = s.find("fox", max<int>(idx - 2, 0));
    }
    cout << siz(s);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
    int _ = 1;
    // cin >> _;
    rep(i, 1, _ + 1) {
        // printf("Case %d: ", i);
        // Solution solve;
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = (b); i < (n); i++)
using namespace std;
using ll = long long;
const ll M = 998244353;

int main() {
	int h, w;
	string s[101];
	int ans = 0;
	cin >> h >> w;
	rep(i, h) cin >> s[i];

	int cnt = 0;
	rep(i, h) {
		rep(j, w) {
			if (s[i][j] == '.') cnt++;
			if (s[i][j] == '#') {
				ans += max(cnt - 1, 0);
				cnt = 0;
			}
		}
		ans += max(cnt - 1, 0);
		cnt = 0;
	}
	rep(i, w) {
		rep(j, h) {
			if (s[j][i] == '.') cnt++;
			if (s[j][i] == '#') {
				ans += max(cnt - 1, 0);
				cnt = 0;
			}
		}
		ans += max(cnt - 1, 0);
		cnt = 0;
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define ll long long
int main(){
  ll n;
  cin>>n;
  vector<int> v;
  rep(i,n){
    int a;
    cin>>a;
    v.push_back(a);
  }
  sort(v.begin(),v.end());
  ll ans=n*(n-1)/2;
  ll num=1;
  rep(i,n-1){
    if(v[i]==v[i+1]){
      ++num;
    }else{
      ans-=num*(num-1)/2;
      num=1;
    }
  }
  ans-=num*(num-1)/2;
  cout<<ans<<endl;
  return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>;
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;

int main() {
    ll A,B;
    cin>>A>>B;

    if(A+B >= 15 && B >= 8){
        cout << 1;
        return 0;
    }
    if(A+B >= 10 && B >= 3){
        cout << 2;
        return 0;
    }
    if(A+B >= 3){
        cout << 3;
        return 0;
    }
    cout << 4;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int A, B;
  cin >> A >> B;
  vector<int> ans(A+B);
  if(A == B){
    for(int i=0; i<A; i++){
      ans[i] = i+1;
    }
    for(int i=0; i<B; i++){
      ans[i+A] = -(i+1);
    }
  }
  else if(A > B){
    int sum = 0;
    for(int i=0; i<A; i++){
      ans[i] = i+1;
      sum += ans[i];
    }
    for(int i=0; i<B-1; i++){
      ans[i+A] = -(i+1);
      sum += ans[i+A];
    }
    ans[A+B-1] = -sum;
  }
  else if(A < B){
    int sum = 0;
    for(int i=0; i<A-1; i++){
      ans[i] = i+1;
      sum += ans[i];
    }
    for(int i=0; i<B; i++){
      ans[i+A] = -(i+1);
      sum += ans[i+A];
    }
    ans[A-1] = -sum;
  }
  for(int i=0; i<A+B; i++){
    if(i != A+B-1){
      cout << ans[i] << " ";
    }
    else{
      cout << ans[i] << endl;
    }
  }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

template<ll MOD = 1000000007>
class ModInt {
    ll n;
    ModInt constexpr inverse() const {
        return ModInt::pow(*this, MOD - 2);
    }

public:
    ModInt() : n(0) {}
    ModInt(ll _n) : n(((_n % MOD) + MOD) % MOD) {}
    ModInt operator+=(const ModInt &m) {
        n += m.n;
        if (n >= MOD) n -= MOD;
        return *this;
    }
    ModInt operator-=(const ModInt &m) {
        n -= m.n;
        if (n < 0) n += MOD;
        return *this;
    }
    ModInt operator*=(const ModInt &m) {
        n *= m.n;
        if (n >= MOD) n %= MOD;
        return *this;
    }
    ModInt operator/=(const ModInt &m) {
        (*this) *= m.inverse();
        return *this;
    }
    friend ModInt operator+(ModInt t, const ModInt &m) {
        return t += m;
    }
    friend ModInt operator-(ModInt t, const ModInt &m) {
        return t -= m;
    }
    friend ModInt operator*(ModInt t, const ModInt &m) {
        return t *= m;
    }
    friend ModInt operator/(ModInt t, const ModInt &m) {
        return t /= m;
    }
    ModInt operator=(const ll l) {
        n = l % MOD;
        if (n < 0) n += MOD;
        return *this;
    }
    friend ostream &operator<<(ostream &out, const ModInt &m) {
        out << m.n;
        return out;
    }
    friend istream &operator>>(istream &in, ModInt &m) {
        ll l;
        in >> l;
        m = l;
        return in;
    }
    static constexpr ModInt pow(const ModInt x, ll p) {
        ModInt<MOD> ans = 1;
        for (ModInt<MOD> m = x; p > 0; p /= 2, m *= m) {
            if (p % 2) ans *= m;
        }
        return ans;
    }
};
using mint = ModInt<998244353>;
mint operator"" _m(unsigned long long m) {
    return mint(m);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m, k;
    cin, n, m, k;
    mint ans = 0;
    if (n == 1 || m == 1) {
        print(mint::pow(k, m * n));
        return 0;
    }
    rep(i, 0, k) {
        mint a = mint::pow(k - i, m) - mint::pow(k - i - 1, m);
        ans += a * mint::pow(i + 1, n);
        // dump(ans, a);
    }
    print(ans);
    return 0;
}

// Skyqwq
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 2e5 + 5, P = 998244353;

int n, m, K;

int inline power(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = (LL)res * a % P;
		a = (LL)a * a % P;
		b >>= 1;
	}
	return res;
}

int ans;

int main() {
	scanf("%d%d%d", &n, &m, &K);
	if (n > 1 && m > 1) {
		for (int i = 1; i <= K; i++) {
			ans = (ans + ((LL)power(i, n) - power(i - 1, n) + P) * power(K - i + 1, m)) % P;
		}
	} else if (n == 1 && m == 1) {
		ans = K;
	} else if (n == 1) {
		for (int i = 1; i <= K; i++) {
			ans = (ans + (LL)power(K - i + 1, m) - power(K - i, m) + P) % P;
		}
	} else if (m == 1) {
		for (int i = 1; i <= K; i++) {
			ans = (ans + (LL)power(i, n) - power(i - 1, n) + P) % P;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<unordered_map>
#include<map>
#include<set>
#include<unordered_set>
#include<algorithm>
#include<list>
#include<bitset>
#include<utility>
#include<climits>
#include<deque>
#include<math.h>
#include<stdio.h>
#define INF  (int)1e9
#define MOD 1000000007ll
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define ppb pop_back
#define uset unordered_set
#define umap unordered_map
using namespace std;
bool possible(pii a,pii b,pii c){
	int x1=a.first, y1=a.second;
	int x2=b.first, y2=b.second;
	int x3=c.first, y3=c.second;
	if ((y3 - y2) * (x2 - x1) ==
        (y2 - y1) * (x3 - x2))
        return true;
    else
	return false;
}
void solve(){
		int n;
		cin>>n;
		vector<pii>a;
		unordered_map<int,unordered_set<int> > mp;
	for(int i=0,x,y;i<n;i++){
		cin>>x>>y;
		if(mp[x].find(y)==mp[x].end()){
			a.push_back({x,y});
			mp[x].insert(y);
		}

	}
	for(int i=0;i<a.size();i++){
		for(int j=i+1;j<a.size();j++){
			for(int k=j+1;k<a.size();k++){
				if(possible(a[i],a[j],a[k])){
					cout<<"Yes\n";
				return ;
				}
			}
		}
	}
		cout<<"No\n";
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t=1;
	//	cin>>t;
	while(t--){
	solve();
	}
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int l,r,ans;
signed main()
{
	cin>>T;
	while(T--)
	{
		cin>>l>>r;
		if(2*l>r)
		{
			cout<<0<<endl;
			continue;
		}
		ans=(r-2*l+1);
		ans=((ans+1)*(ans))/2;
		cout<<ans<<endl;
	}
	return 0;
}


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

// https://github.com/nweeks1/UltiSnips
template <const int32_t MOD> struct ModInt {
  long long x;
  ModInt() : x(0) {}
  ModInt(long long u) : x(u) {
    x %= MOD;
    if (x < 0)
      x += MOD;
  }
  friend bool operator==(const ModInt &a, const ModInt &b) {
    return a.x == b.x;
  }
  friend bool operator!=(const ModInt &a, const ModInt &b) {
    return a.x != b.x;
  }
  friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
  friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
  friend bool operator<=(const ModInt &a, const ModInt &b) {
    return a.x <= b.x;
  }
  friend bool operator>=(const ModInt &a, const ModInt &b) {
    return a.x >= b.x;
  }
  static ModInt sign(long long k) {
    return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
  }

  ModInt &operator+=(const ModInt &m) {
    x += m.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &m) {
    x -= m.x;
    if (x < 0LL)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &m) {
    x = (1LL * x * m.x) % MOD;
    return *this;
  }

  friend ModInt operator-(const ModInt &a) {
    ModInt res(a);
    if (res.x)
      res.x = MOD - res.x;
    return res;
  }
  friend ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += ModInt(b);
  }
  friend ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= ModInt(b);
  }
  friend ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b);
  }

  static long long fp(long long u, long long k) {
    long long res = 1LL;
    while (k > 0LL) {
      if (k & 1LL)
        res = (res * u) % MOD;
      u = (u * u) % MOD;
      k /= 2LL;
    }
    return res;
  }

  static constexpr int mod() { return MOD; }

  ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
  ModInt inv() {
    assert(x);
    return ModInt(fp(x, MOD - 2));
  }
  ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
  friend ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b).inv();
  }

  friend ostream &operator<<(ostream &out, const ModInt &a) {
    return out << a.x;
  }
  friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};

// const int MOD = 1e9 + 7;
const int MOD = 998244353;
using Mint = ModInt<MOD>;

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

  int N, M;
  cin >> N >> M;

  vector<Mint> dp(N);
  vector<Mint> A(N);
  dp[0] = M;
  for (int i(1); i < N; ++i) {
    A[i] = A[i - 1] * M;
    Mint x = Mint(M - 1).fastpow(i - 1);
    for (int m(1); m <= M; ++m)
      A[i] += x - Mint(M - m).fastpow(i - 1);
    dp[i] = dp[i - 1] * M + M * Mint(M - 1).fastpow(i) + A[i];
  }

  cout << dp[N - 1] << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003, M = 998244353;

/*int ast(int x, int n)
{
    int ans = 1;
    while (n)
    {
        if (n % 2 == 1)
            ans = (ans * 1LL * x) % M;
        x = (x * 1LL * x) % M;
        n /= 2;
    }
    return ans;
}*/

int n0, m0;
int dp[N][N];

int ast[N][N];

void solv()
{
    cin >> n0 >> m0;

    for (int i = 1; i < N; ++i)
    {
        ast[i][0] = 1;
        for (int j = 1; j < N; ++j)
            ast[i][j] = (ast[i][j - 1] * 1LL * i) % M;
    }

    for (int m = 1; m <= m0; ++m)
    {
        int f0 = 0;
        int f = 0;
        int s = 0;
        for (int n = 1; n <= n0; ++n)
        {
            /*for (int d = 1; d <= n - 2; ++d)
            {
                dp[n][m] = (dp[n][m] + dp[d][m - 1] * 1LL * ast(m, n - d - 2) % M * 1LL * (n - d - 1)) % M;
            }*/
            f = (f + f0) % M;
            f = (f * 1LL * m) % M;
            if (n - 2 >= 1)
                f = (f + dp[n - 2][m - 1]) % M;
            f0 = (f0 * 1LL * m) % M;
            if (n - 2 >= 1)
                f0 = (f0 + dp[n - 2][m - 1]) % M;
            /*for (int d = 1; d <= n - 1; ++d)
            {
                dp[n][m] = (dp[n][m] + dp[d][m - 1] * 2LL * ast(m, n - d - 1)) % M;
            }*/
            s = (s * 1LL * m) % M;
            if (n - 1 >= 1)
                s = (s + dp[n - 1][m - 1] * 2LL) % M;
            dp[n][m] = (f + s) % M;
            dp[n][m] = (dp[n][m] + dp[n][m - 1]) % M;
            dp[n][m] = (dp[n][m] + ast[m][n]) % M;
            dp[n][m] = (dp[n][m] - ast[m - 1][n] + M) % M;
        }
    }

    cout << dp[n0][m0] << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}


//* Jai shree Ram
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lli                                 long long int
#define ll                                  long long
#define FOR(i,a,b)                          for(int i=a;i<b;i++)
#define pi                                  pair<int,int>
#define vi                                  vector<int>
#define vll                                 vector<ll>
#define all(a)                              a.begin(),a.end()
#define sz(a)                               (int)a.size()
#define pb                                  push_back
#define ppb                                 pop_back
#define pf                                  push_front
#define fr                                  first
#define sc                                  second
#define uniq(v)                             (v).erase(unique(all(v)),(v).end())
using namespace std;

void __print(int x) {cerr << x;}void __print(long x) {cerr << x;}void __print(long long x) {cerr << x;}void __print(unsigned x) {cerr << x;}void __print(unsigned long x) {cerr << x;}void __print(unsigned long long x) {cerr << x;}void __print(float x) {cerr << x;}void __print(double x) {cerr << x;}void __print(long double x) {cerr << x;}void __print(char x) {cerr << '\'' << x << '\'';}void __print(const char *x) {cerr << '\"' << x << '\"';}void __print(const string &x) {cerr << '\"' << x << '\"';}void __print(bool x) {cerr << (x ? "true" : "false");}template<typename T, typename V>void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}void _print() {cerr << "]\n";}template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
// __builtin_popcountll(x);
// __builtin_clz(x) // Count Leading zeros
// __builtin_ctz(x) // Trailing zeros
const int MM = 998244353;
const int mod = 1e9 + 7;
const int INF = 1e9 + 5;
const int N = 1e5 + 5;

void solve()
{
    int n;
    cin >> n;
    vector<vector<ll>> a(n,vector<ll>(5));
    FOR(i,0,n) {
        FOR(j,0,5) {
            cin >> a[i][j];
        }
    }
    auto fine = [&](int mid) {
        set<int> masks;
        FOR(i,0,n) {
            int mask = 0;
            FOR(j,0,5) {
                if(a[i][j] >= mid) {
                    mask |= (1 << j);
                }
            }
            masks.insert(mask);
        }
        for(int first : masks) {
            for(int second : masks) {
                for(int third : masks) {
                    if((first | second | third) == ((1 << 5)-1)) {
                        return 1;
                    }
                }
            }
        }
        return 0;
    };
    ll L = 1,U = INF;
    ll ans = 1;
    while(L <= U) {
        ll mid = L + (U - L) / 2;
        if(fine(mid)) {
            amax(ans,mid);
            L = mid + 1;
        }
        else {
            U = mid - 1;
        }
    }
    cout << ans << '\n';
}

signed main()
{
    ios;
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #ifdef SIEVE
          sieve();
    #endif
    #ifdef NCR
          init();
    #endif
    int t;
    t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
}
/* stuff you should look for
  * int overflow, array bounds
  * special cases (n=1?)
  * remember out of bounds case mainly in if else
  * do smth instead of nothing and stay organized
  * WRITE STUFF DOWN
*/

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <math.h>
using namespace std;

//#define int long long
#define all(x) x.begin(), x.end()

const int maxn = 6e5 + 13;
const int INF = 2e18 + 1;
const int MOD = 1e9 + 7;

struct treap {
	struct node {
		int key, pr, sz;
		node* left = 0, * right = 0;
		node(int k) : key(k), sz(1), pr(rand()) {}
	};
	node* root = 0;
	typedef pair<node*, node*> pnn;
	void upd(node* &root) {
		if (root == 0)return;
		root->sz = 1;
		if (root->left != 0)
			root->sz += root->left->sz;
		if (root->right != 0)
			root->sz += root->right->sz;
	}
	pnn split(node* root, int k) {
		if (root == 0)return { 0, 0 };
		upd(root);
		if (k < root->key) {
			pnn sp = split(root->left, k);
			root->left = sp.second;
			upd(root);
			return { sp.first, root };
		}
		else {
			pnn sp = split(root->right, k);
			root->right = sp.first;
			upd(root);
			return { root, sp.second };
		}
	}
	node* merge(node* left, node* right) {
		upd(left);
		upd(right);
		if (left == 0 || right == 0)
			return left == 0 ? right : left;
		if (left->pr < right->pr) {
			right->left = merge(left, right->left);
			upd(right);
			return right;
		}
		else {
			left->right = merge(left->right, right);
			upd(left);
			return left;
		}
	}
	void insert(int k) {
		node* nn = new node(k);
		pnn sp = split(root, k);
		root = merge(sp.first, merge(nn, sp.second));
	}
	int lowest(int x) {
		pnn sp = split(root, x - 1);
		int res = sp.first == 0 ? 0 : sp.first->sz;
		root = merge(sp.first, sp.second);
		return res;
	}
	int highest(int x) {
		pnn sp = split(root, x);
		int res = sp.second == 0 ? 0 : sp.second->sz;
		root = merge(sp.first, sp.second);
		return res;
	}
};
int a[maxn];
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		a[i + n] = a[i];
	}
	treap dd;
	long long cur = 0;
	for (int i = 1; i <= n; ++i) {
		cur += dd.highest(a[i]);
		dd.insert(a[i]);
	}
	cout << cur << "\n";
	for (int i = 1; i < n; ++i) {
		int l = dd.lowest(a[i]);
		int h = dd.highest(a[i]);
		cur += h - l;
		cout << cur << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#define int long long
#define rep(i, x, y) for(int i = x; i <= y; ++ i)
using namespace std;
const int N = 800050;
const int M = 998244353;
//const int M = 1e9 + 7;

vector <int> v[N];
void add(int x, int y) { v[x].push_back(y); v[y].push_back(x); }
int fa[N], vs[N], n, a[N], b[N], vis[N], s, tg;
void dfs(int now, int las)
{
	s ++; vs[now] = 0;
	for(int i = 0; i < v[now].size(); ++ i)
		if(v[now][i] != las)
		{
			if(!vs[v[now][i]]) tg = 0;
			else dfs(v[now][i], now);
		}
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
signed main()
{
	int nn = 4e5, ans = 0;
	cin >> n;
	rep(i, 1, 800000) fa[i] = i;
	rep(i, 1, n)
	{
		scanf("%lld %lld", &a[i], &b[i]);
		add(a[i], b[i]);
		vs[a[i]] = vs[b[i]] = 1;
	/*	if(find(a[i]) == find(b[i]))
			ans += (vs[find(a[i])] ^ 1), vs[find(a[i])] = 1;
		else
		{
			if(vs[find(a[i])] < vs[find(b[i])])
				fa[find(a[i])] = find(b[i]);
			else fa[find(b[i])] = find(a[i]);
			ans ++;
		}*/
	}
	rep(i, 1, 800000)
		if(vs[i])
			tg = 1, s = 0, dfs(i, 0), ans += s - tg;
	cout << ans << "\n";
}
/*

*/

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
struct UnionFind {
  vector<int> par;  //par: -1*elements(root)  par(not root)
  vector<int> cntv;
  UnionFind(int n) {
    par = vector<int>(n, -1);
    cntv = vector<int>(n, 0);
  }
  int root(int x) { return par[x] < 0 ? x : (par[x] = root(par[x])); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[x] < par[y]) swap(x, y);
      par[y] += par[x];
      par[x] = y;
      cntv[y] += cntv[x];
    }
  }
  bool is_root(int x) { return root(x) == x; }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -par[root(x)]; }
  int get_count(int x) { return cntv[root(x)]; }
};
class Solver {
 public:
  int n;
  UnionFind uf;
  Solver(LL n) : n(n), uf(400000){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      if (a == b) {
        uf.cntv[uf.root(a)]++;
      } else {
        uf.unite(a, b);
        uf.cntv[uf.root(a)]++;
      }
    }
    int ans = 0;
    for (int i = 0; i < 400000; ++i) {
      if (uf.is_root(i)) {
        ans += min(uf.cntv[i], uf.size(i));
      }
    }
    cout << ans << endl;
  }
};

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;

double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
const ll MOD = 1000000007;

ll sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }

#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl;

void solve() {

}

int main()
{
    FAST_INP;
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt","r", stdin);
//    freopen("output.txt","w", stdout);
//    #endif

//    int tc; cin >> tc;
//    TC(tc) solve();
	int n, a; cin >> n;
	set<int> taken;
	int smallest = 0;
	REP(i,n) {
		cin >> a;
		while(a==smallest||taken.find(smallest)!=taken.end()) smallest++;
		OUT(smallest);
		if(a>=0) taken.insert(a);
	}
    return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<bool> p(200100, false);
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    int next;
    cin >> next;
    p[next] = true;
    while (p[ans])
      ++ans;
    cout << ans << '\n';
  }
}


#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#pragma comment(linker, "/stack:200000000")

#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 int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\n"
#define forn(i,n) for(int i=0;i<n;++i)
#define fore(i,l,r) for(int i=int(l);i<=int(r);++i)
#define rep(i,begin,end) for(__typeof(end) i=(begin);i!=(end);i++)
#define fill(a,value) memset(a,value,sizeof(a));
#define gcd(a,b) __gcd((a),(b))
#define watch1(x) cout<<(x)<<endl
#define watch2(x,y) cout<<(x)<<" "<<(y)<<endl
#define watch3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi 3.141592653589793
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;

const int INF = 9e18;
const int mod = 998244353;
const int N = 5005;

int n;

void solve() {
    cin >> n;
    cout << n - 1 << endl;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    fastio;
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
ll GD(ll num){//各桁の和
    ll digit=0;
    while(num!=0){
        digit+=num%10;
        num/=10;
    }
    return digit;
}
bool is_integer( long double x ){//整数判定
  return std::floor(x)==x;
}

int gcd(int aaa, int bbb)//最大公約数
{
   if (aaa%bbb == 0)
   {
       return(bbb);
   }
   else
   {
       return(gcd(bbb, aaa%bbb));
   }
}

int lcm(int aaa, int bbb)//最小公倍数
{
   return aaa / gcd(aaa, bbb)* bbb ;
}

int main() {
  string YYY="YES",yyy="Yes",NNN="NO",nnn="No";
  int N;cin>>N;
  if(N==1){
   cout<<0;
  }else{
   cout<<N-1;
  }
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
inline int read(){
	int res=0;
	bool zf=0;
	char c;
	while(((c=getchar())<'0'||c>'9')&&c!='-');
	if(c=='-')zf=1;
	else res=c-'0';
	while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
	if(zf)return -res;
	return res;
}
const int maxn=2e5+5,P=998244353;
int f[maxn],g[maxn];
int p[maxn],r;
int v[maxn],inv[25];
signed main(){
	int n=read(),m=read(),ans=1;
	inv[1]=1;
	for(register int i=2;i<=20;++i){
		inv[i]=1ll*inv[P%i]*(P-P/i)%P;
	}
	for(register int i=2,t;i<=m;++i){
		if(!v[i]){
			f[i]=n,g[i]=1,p[++r]=i;
		}
		for(register int j=1;j<=r&&(t=i*p[j])<=m;++j){
			v[t]=1;
			if(i%p[j]){
				f[t]=1ll*f[i]*n%P,g[t]=1;
			}
			else{
				f[t]=1ll*f[i]*(n+g[i])%P*inv[g[i]+1]%P,g[t]=g[i]+1;
				break;
			}
		}
		ans+=f[i];
		(ans>=P)&&(ans-=P);
	}
	printf("%d\n",ans);
	return 0;
}

// RedStone
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define endl '\n'
#define D(x) cerr << #x << " = " << (x) << '\n'
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;

const int N = 200010;
const int Mod = 998244353;

int n, m;
int smf[N];
int dp[N][100];

void init() {
  for(int i = 0; i <= m; i++)
    smf[i] = i;
  for(int i = 2; i * i <= m; i++) {
    if(smf[i] == i) {
      for(int j = 2 * i; j <= m; j += i) {
        smf[j] = min(smf[j], i);
      }
    }
  }
}

int calc(int it, int rem) {
  if(it == 0) return rem == 0;
  int& r = dp[it][rem];
  if(r != -1) return r;
  r = calc(it - 1, rem);
  if(rem > 0)
    r = (r + calc(it, rem - 1)) % Mod;
  return r;
}

int solve(int m) {
  int ans = 1;
  while(m > 1) {
    int f = smf[m];
    int fr = 0;
    while(m % f == 0) {
      fr++;
      m /= f;
    }
    ans = (ans * 1LL * calc(n, fr)) % Mod;
  }
  return ans;
}

int main()
{
  memset(dp, -1, sizeof dp);

  cin >> n >> m;
  init();

  int ans = 1;
  for(int i = 2; i <= m; i++) {
    ans = (ans + solve(i)) % Mod;
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
using namespace std;
const long long N=100005, INF=2000000000000000000;
vi g[N];
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
vi prime;
bool isprime[N];
void pre()
{
    for(int i=2;i<N;i++)
    {
        if(isprime[i])
        {
            for(int j=i*i;j<N;j+=i)
            isprime[j]=false;
            prime.pb(i);
        }
    }
    return;
}
int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;

        // If the element is present at the middle
        // itself
        if (arr[mid] == x)
            return mid;

        // If element is smaller than mid, then
        // it can only be present in left subarray
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        // Else the element can only be present
        // in right subarray
        return binarySearch(arr, mid + 1, r, x);
    }

    // We reach here when element is not
    // present in array
    return -1;
}
bool isPowerOfTwo(int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int gcd(int a, int b)
{
    if(a < b)
        return gcd(b, a);
    else if(a%b == 0)
        return b;
    else return gcd(b, a%b);
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    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 spf[N+1];
void spfFun(){
    for(int i=0;i<=N;i++) spf[i]=i;
    for(int i=2;i*i<=N;i++){
        if(spf[i]==i){
            for(int j=i*i;j<=N;j+=i){
                spf[j] = min(spf[j],i);
            }
        }
    }
}

bool isPerfectSquare(long double x)
{
  // Find floating point value of
  // square root of x.
  long double sr = sqrt(x);

  // If square root is an integer
  return ((sr - floor(sr)) == 0);
}
void print(bool n){
    if(n){
        cout<<"YES";
    }else{
        cout<<"NO";
    }
}

int n,k;
int cal(int a){
    if(a>n){
        return 2*n - a + 1;
    }
    return a-1;
}

int32_t main()
{
    IOS;
    // pre();
    // fill(isprime,true);
    cin>>n>>k;
    unsigned int ans=0;
    for(int i=2;i<=2*n;i++){
        int other = i-k;
        // cout<<i<<" "<<cal(i)<<"\n";
        if(other<=1 || other>2*n) continue;
        ans+=cal(i)*cal(other);
    }
    cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using vi  = vector<int64_t>;
using vvi = vector<vi>;
using pii = pair<int64_t, int64_t>;
int64_t INF = 1e9;
int64_t mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  if (k<0) k *= -1;

  vi count(2*n+10, 0);
  for (int i = 2; i <= 2*n; i++) {
    if (i <= n+1) count[i] = i-1;
    else count[i] = 2*n - i + 1;
  }
  int64_t ans = 0;
  for (int i = k + 2; i <= 2*n; i++) {
    ans += count[i] * count[i-k];
  }
  cout << ans << endl;
}


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

int M;
struct modint {

  int val = 0;

  modint() {}

  modint(int a) : val(a) {
    while (val < 0) {
      val += M;
    }
    while (val >= M) {
      val -= M;
    }
  }

  modint(long long a) : val(a % M) {
    if (val < 0) {
      val += M;
    }
  }

  modint operator+=(modint oth) {
    val += oth.val;
    if (val >= M) {
      val -= M;
    }
    return *this;
  }

  modint operator-=(modint oth) {
    val -= oth.val;
    if (val < 0) {
      val += M;
    }
    return *this;
  }

  modint operator*=(modint oth) {
    val = 1LL * val * oth.val % M;
    return *this;
  }

  void operator++() {
    ++val;
    if (val == M) {
      val = 0;
    }
  }

  void operator--() {
    --val;
    if (val == -1) {
      val = M - 1;
    }
  }

  modint operator-() {
    modint res;
    res.val = (val == 0) ? 0 : M - val;
    return res;
  }

  int mod() {
    return M;
  }

  modint pow(int e) {
    modint base = val;
    modint res = 1;
    while (e > 0) {
      if (e % 2 == 1) {
        res *= base;
      }
      base *= base;
      e /= 2;
    }
    return res;
  }

  modint inv() {
    return pow(M - 2);
  }

  friend modint operator+(modint a, modint b) {
    return modint(a) += b;
  }

  friend modint operator-(modint a, modint b) {
    return modint(a) -= b;
  }

  friend modint operator*(modint a, modint b) {
    return modint(a) *= b;
  }

  friend bool operator==(modint a, modint b) {
    return a.val == b.val;
  }

  friend bool operator!=(modint a, modint b) {
    return a.val != b.val;
  }

};

using mint = modint;
const int N = 100;
const int K = 1'000'000;

mint ways[N][K];

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

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

  ways[0][0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < K; ++j) {
      int prv = j - i;
      int prv_prv = j - i * (m + 1);

      ways[i][j] = ways[i - 1][j];
      if (prv >= 0) {
        ways[i][j] += ways[i][prv];
        if (prv_prv >= 0) {
          ways[i][j] -= ways[i - 1][prv_prv];
        }
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    mint ans = 0;
    for (int j = 0; j < K; ++j) {
      ans += ways[i][j] * ways[n - i - 1][j];
    }
    cout << (ans * (m + 1) - 1).val << "\n";
  }
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

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

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

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

struct Vec2 {
  ld x, y;

  void add(Vec2 v) {
    x += v.x;
    y += v.y;
  }

  void rorate(ld rad) {
    ld ox = x;
    ld oy = y;
    x = ox * cos(rad) - oy * sin(rad);
    y = ox * sin(rad) + oy * cos(rad);
  }

  ld dot(Vec2 v) {
    return x * v.x + y * v.y;
  }

  ld norm() {
    return sqrtl(x * x + y * y);
  }
};

signed main() {
  init();
  ll N;
  cin >> N;
  vll A(N), B(N), C(N), D(N);
  rep(i, N) cin >> A[i] >> B[i];
  rep(i, N) cin >> C[i] >> D[i];

  ld ju_a = (ld)SUM(A) / (ld)N;
  ld ju_b = (ld)SUM(B) / (ld)N;
  ld ju_c = (ld)SUM(C) / (ld)N;
  ld ju_d = (ld)SUM(D) / (ld)N;

  // debug(ju_a);
  // debug(ju_b);
  // debug(ju_c);
  // debug(ju_d);
  vector<Vec2> X, Y;
  rep(i, N) {
    Vec2 a = {(ld)A[i] - ju_a, (ld)B[i] - ju_b};
    Vec2 b = {(ld)C[i] - ju_c, (ld)D[i] - ju_d};
    X.pb(a);
    Y.pb(b);
  }

  rep(i, N) {
    rep(j, N) {
      ld tanX = atan2l(X[i].y, X[i].x);
      ld tanY = atan2l(Y[j].y, Y[j].x);
      bool ok = true;

      mll done;
      rep(k, N) {
        Vec2 t1 = X[k];
        t1.rorate(-tanX);
        bool o = false;
        rep(p, N) {
          if (done[p] == 1) continue;
          Vec2 t2 = Y[p];
          t2.rorate(-tanY);
          if (abs(t1.x - t2.x) < 0.000001 and abs(t1.y - t2.y) < 0.000001) {
            o = true;
            done[p] = 1;
            break;
          }
        }
        if (!o) {
          ok = false;
          break;
        }
      }

      if (ok) {
        Yes;
        return 0;
      }

      // debug(X[i].x);
      // debug(X[i].y);
      // debug(Y[i].x);
      // debug(Y[i].y);

      // debug(tanX);
      // debug(tanY);
    }
  }
  No;

  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}

char ff(char s, char t){
    if(s == t) return t;
    if(s != 'R' && t != 'R') return 'R';
    else if(s != 'B' && t != 'B') return 'B';
    else return 'W';
}

string g(string s){
    //cout << 1 csp si(s) csp s << endl;

    if(si(s) == 1) return s;

    int a = 1;
    int aa = a;
    while (a <= si(s)) {
        if((si(s)-1) % a == 0) chmax(aa,a);
        a *= 3;
    }

    string t = "";
    int b = 0;
    while (b+aa < si(s)) {
        t += ff(s[b],s[b+aa]);
        //cout << s[b] csp b csp s[b+aa] csp b+aa << " ";
        b += aa;
    }
    //cout << endl;
    if(si(t) == 1) return t;
    else return g(t);
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    string s;
    cin >> s;

    cout << g(s) << endl;

    return 0;
}

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

#define     F       first
#define     S       second
typedef pair<int, int>  pi;
typedef long long       ll;

void Solve() {
    int n, m;
    cin >> n >> m;

    vector< vector<char> > grid(n + 1, vector<char>(m + 1));

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

    auto hasTopSide = [&](int r, int c) {
        return grid[r][c] == '#' && grid[r - 1][c] == '.';
    };

    auto hasBottomSide = [&](int r, int c) {
        return grid[r][c] == '#' && grid[r + 1][c] == '.';
    };

    auto hasLeftSide = [&](int r, int c) {
        return grid[r][c] == '#' && grid[r][c - 1] == '.';
    };

    auto hasRightSide = [&](int r, int c) {
        return grid[r][c] == '#' && grid[r][c + 1] == '.';
    };

    int answer = 0;

    for (int i = 2; i < n; ++i) {
        for (int j = 2; j < m; ++j) {
            if (grid[i][j] == '.') {
                continue;
            }

            if (hasBottomSide(i, j) && !hasBottomSide(i, j - 1)) {
                ++answer;
            }

            if (hasTopSide(i, j) && !hasTopSide(i, j - 1)) {
                ++answer;
            }

            if (hasLeftSide(i, j) && !hasLeftSide(i - 1, j)) {
                ++answer;
            }

            if (hasRightSide(i, j) && !hasRightSide(i - 1, j)) {
                ++answer;
            }
        }
    }

    cout << answer;
}

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

    int t;
    //cin >> t;

    t = 1;

    while (t--) {
        Solve();
    }

    return 0;
}


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

int main()
{
    int a, b;
    cin >> a >> b;
    int suma = a/100 + (a%100)/10 + a%10;
    int sumb = b/100 + (b%100)/10 + b%10;

    if(suma > sumb) cout << suma << endl;
    else cout << sumb << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll LINF = 1e18;
const int INF = 1e9;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return 1;
  }
  return 0;
}
// Union-Find
struct UnionFind
{
  vector<int> par, siz;

  UnionFind(int n) : par(n, -1), siz(n, 1) {}

  // �������
  int root(int x)
  {
    if (par[x] == -1)
      return x;
    else
      return par[x] = root(par[x]);
  }

  // x �� y ��Ʊ�����롼�פ�°���뤫�ɤ��� (�������פ��뤫�ɤ���)
  bool issame(int x, int y)
  {
    return root(x) == root(y);
  }

  // x ��ޤ॰�롼�פ� y ��ޤ॰�롼�פȤ�ʻ�礹��
  bool unite(int x, int y)
  {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    par[y] = x;
    siz[x] += siz[y];
    return true;
  }

  // x ��ޤ॰�롼�פΥ�����
  int size(int x)
  {
    return siz[root(x)];
  }
};

int main()
{
  string N1, N2;
  cin >> N1 >> N2;
  int res1 = 0, res2 = 0;
  for (int i = 0; i < (int)N1.size(); i++)
  {
    res1+=N1[i]-'0';
  }
  for (int i = 0; i < (int)N2.size();i++){
    res2 += N2[i]-'0';
  }
  chmax(res1, res2);
  cout << res1 << endl;
}


#include <iostream>
using namespace std;
int main() {
string s; cin >> s;
cout << s[1] << s[2] << s[0];
}

#include <bits/stdc++.h>
    using namespace std;
    #define max(a,b) (a>b?a:b)
    #define min(a,b) (a<b?a:b)
    typedef long long int ll;
int main()
{ string s;
char temp;
cin>>s;
temp=s[0];
s[0]=s[1];
s[1]=s[2];
s[2]=temp;
cout<<s;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<ll, ll> p;

#define MOD 1000000007

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

	int N;
	cin >> N;

	bool c = false;

	ll ans = 1e15;

	for(int i = 0; i < N; i++) {
		ll A, P, X;
		cin >> A >> P >> X;

		X -= A;

		if(X > 0LL) {
			c = true;
			ans = min(ans, P);
		}
	}

	if(c) {
		cout << ans << endl;
		return 0;
	}

	cout << -1 << endl;
}

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

int main()
{
  long int T;
  cin >>T;

  for (int i = 0; i < T; i++) {
    long int L;
    long int R;
    cin >> L >> R;
    long int maxC = R-L;
    long int minC = L;
    if (minC > maxC) {
      cout << 0 << endl;
    } else {
    cout << (maxC-minC+1)*(maxC-minC+2)/2 << endl;
    }
  }

}


#line 2 "header.hpp"

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

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

// 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)

#include <cxxabi.h>
string demangle(const char * name) {
    size_t len = strlen(name) + 256;
    char output_buffer[len];
    int status = 0;
    return string(abi::__cxa_demangle(name, output_buffer, &len, &status));
}
template<class T> string type(T x){ return demangle(typeid(x).name()); }

typedef long long ll;
typedef long double ld;

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;

using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using iii = tuple<int, int, int>;

int solve() {
    ll n;cin>>n;
    vec<ll> x(n), y(n);
    ll ans = inf<ll>;
    rep(i, n){
        cin>>x[i]>>y[i];
        chmin(ans, x[i]+y[i]);
    }

    rep(i, n){
        rep(j, n){
            if (i == j) continue;
            chmin(ans, max(x[i], y[j]));
            dump(i, j, ans);
        }
    }
    cout << ans << endl;

    return 0;
}

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

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

    return 0;
}/*}}}*/

#include <bits/stdc++.h>
// #include <atcoder/all>
// #include <boost/multiprecision/cpp_int.hpp>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(a) a.begin(), a.end()
#define yn(p) cout << (p?"Yes":"No") << endl;
using namespace std;
// using namespace atcoder;
using ll = long long;
// using lll = boost::multiprecision::cpp_int;
using P = pair<int, int>;

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

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  solve();
  return 0;
}


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

int main(){
    int n, k = 0;
    cin >> n;
    int p[n + 1], c[n + 1] = {}, a[n + 1] = {}, gc = 0, ga = 0;
    vector<pair<int, int>> v;
    for(int i = 1; i <= n; i++){
        cin >> p[i];
        if(p[i] == i) k = -1;
        else if(p[i] > i){
            c[p[i]]--;
            c[i]++;
        }
        else{
            a[p[i]]++;
            a[i]--;
        }
        v.push_back(make_pair(p[i], i));
    }
    for(int i = 1; i < n; i++){
        gc += c[i];
        ga += a[i];
        if(ga * gc != 1) k = -1;
    }
    if(k == -1) cout << k << endl;
    else{
        sort(v.begin(), v.end());
        bool b[n] = {};
        for(int i = 0; i < n; i++){
            if(v[i].first < v[i].second){
                for(int j = v[i].second - 1; j >= v[i].first; j--){
                    if(b[j] == 0){
                        cout << j << endl;
                        b[j] = 1;
                    }
                }
            }
            else{
                for(int j = v[i].second; j < v[i].first; j++){
                    if(b[j] == 0){
                        cout << j << endl;
                        b[j] = 1;
                    }
                }
            }
        }
    }
}

#include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 1000000000
#define common cout << "Case #" << w+1 << ": "
#define MAXN 200000

vector<int>g;
void comb(vector<pair<int,int> >&cnt,int ind,int ans)
{
    if(ind==cnt.size())
    {
        g.push_back(ans);
        return;
    }
    int p=1;
    for(int i=0;i<=cnt[ind].second;i++)
    {
        comb(cnt,ind+1,ans*p);
        p*=cnt[ind].first;
    }
}

unordered_map<int,int> primefactors(int n)
{
    int f=0;
    unordered_map<int,int>m;
    while (n % 2 == 0)
    {
        f+=1;
        n = n/2;
    }
    if(f>0)
    {
        m[2]=f;
        f=0;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            f+=1;
            n = n/i;
        }
        if(f>0)
        {
            m[i]=f;
            f=0;
        }
    }
    if (n > 2)
        m[n]=1;
    return m;
}

map<int,vector<int> >m;

void func(int num)
{
    for(int i=0;i<g.size();i++)
        m[g[i]].push_back(num/g[i]);
    g.clear();
}

int gcd(int a,int b)
{
    if(b>a)
        swap(a,b);
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}

int main()
{
    fastio;
    int n;
    cin >> n;
    vector<int>v(n);
    for(int i=0;i<n;i++)
        cin >> v[i];
    sort(v.begin(),v.end());
    vector<vector<int> >vimp;
    for(int i=0;i<n;i++)
    {
        unordered_map<int,int>um=primefactors(v[i]);
        vector<pair<int,int> >t;
        unordered_map<int,int>:: iterator it;
        for(it=um.begin();it!=um.end();it++)
            t.push_back({it->first,it->second});
        comb(t,0,1);
        vimp.push_back(g);
        func(v[i]);
    }
    map<int,vector<int> >:: iterator it;
    map<int,int>mgcd;
    for(it=m.begin();it!=m.end();it++)
    {
        vector<int>t=it->second;
        int a=t[0];
        for(int i=1;i<t.size();i++)
            a=gcd(a,t[i]);
        mgcd[it->first]=a;
    }
    set<int>found;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<vimp[i].size();j++)
        {
            if(vimp[i][j]<=v[0] and mgcd[vimp[i][j]]==1)
                found.insert(vimp[i][j]);
        }
    }
    cout << found.size() << "\n";
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n, a[400020];
vector<pair<ll, ll>> v;
queue<int> pos;
string ans;

int main() {
    scanf("%lld", &n);
    ans.resize(2*n, 'p');
    for(int i = 0; i < 2*n; i++) {
        scanf("%lld", &a[i]);
        v.emplace_back(a[i], i);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n; i++) a[v[i].second] = 0;
    for(int i = 0; i < 2*n; i++) {
        if(pos.empty()) pos.push(i);
        else if((a[i] && a[pos.front()]) || (!a[i] && !a[pos.front()])) pos.push(i);
        else {
            ans[pos.front()] = '(';
            ans[i] = ')';
            pos.pop();
        }
    }
    printf("%s\n", ans.c_str());
}

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

int main() {
	int n;
	cin >> n;
	vector<ll> dp(2, 1);
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		vector<ll> p(2);
		swap(dp, p);
		if (s == "AND") {
			dp[0] = 2 * p[0] + p[1];
			dp[1] = p[1];
		} else {
			dp[0] = p[0];
			dp[1] = p[0] + 2 * p[1];
		}
	}
	cout << dp[1] << endl;
}


#include "bits/stdc++.h"
#define ll long long int
#define MOD 1000000007
#define oo 1000000000000000000
#define forr(i,n) for(int i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define FF first
#define SS second
#define mem(a,v) memset(a,v,sizeof(a))
#define pb push_back
#define popcount(x) __builtin_popcount(x)

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int power(long long x,long long n,const long long P){
    if(x==0) return 0;
    if(n==0 || x==1) return 1LL;
    x%=P;
    int res = 1;
    while(n>0){
        if(n&1) res = (ll) res * x %P;
        x = (ll) x * x % P;
        n/=2;
    }
    return res;
}
long long int power(long long int x,long long int n){
    if(x==0) return 0;
    else if(n==0 || x==1) return 1;
    long long res = 1;
    while(n>0){
        if(n&1) res *= x;
        x *= x;
        n /=2;
    }
    return res;
}
int inv(long long x){ return power(x,MOD-2,MOD);}
long long int gcd(const long long int a,const long long int b){
    if(b == 0) return a;
    return gcd(b,a%b);
}
long long int lcm(const long long int a,const long long int b){
    return (a*b)/gcd(a,b);
}

pair<int,int> dx[4] = {make_pair(-1,0),make_pair(1,0),make_pair(0,-1),make_pair(0,1)};

void __sol(){
    int n; cin >> n;
    double sum = 0;
    for(int i=1;i<=n-1;i++){
        sum+=(double)n/i;
    }
    cout << fixed << setprecision(10)<<sum;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    clock_t time_req = clock();
    fastio
    ll tc=1;  // cin >> tc;
    while(tc--) __sol();
    time_req = clock() - time_req;
    cerr << "Finished in: "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  cout << n - 1 << endl;

}


#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;
typedef long long ll;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template<class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;}cerr << "]\n";}
void _print() {cerr << "]\n";}
template<class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = [", _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
#define uid(x,y) uniform_int_distribution<int>(x,y)(rng)
int vp[400005], fa[400005];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    string s;
    cin >> n >> s;
    vp[0] = 0;
    fa[0] = 1;
    for (int q = 1; q < 400005; q++) {
        vp[q] = vp[q-1];
        fa[q] = fa[q-1];
        int qq = q;
        while (qq % 3 == 0) {
            vp[q]++;
            qq /= 3;
        }
        fa[q] = (fa[q]*qq)%3;
    }
    int tot = 0;
    for (int q = 0; q < n; q++) {
        //B = 0, R = 1, W = 2
        int x = 0;
        if (s[q] > 'B') x++;
        if (s[q] > 'R') x++;
        if (vp[n-1] == vp[q]+vp[n-1-q]) tot += x*fa[n-1]*fa[q]*fa[n-1-q];
    }
    tot = ((tot%3)+3)%3;
    if (n % 2 == 0) tot = (3-tot)%3;
    if (tot == 0) cout << "B\n";
    else if (tot == 1) cout << "R\n";
    else cout << "W\n";
    return 0;
}

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vz;
typedef vector<vz> vvz;
typedef pair<ll, ll> pll;
#define sz(x) ll((x).size())
#define fo(i,n) for(ll i=0; i<(n); i++)
#define FO(i,a,b) for(ll i=(a); i!=(b); i+=(b)>(a)?1:-1)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define os(x) cout<<x<<' '
#define on(x) cout<<x<<"\n"
#define nl cout<<"\n"
const ll P=998244353;
const ll N=1e5+100;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("C:\\Users\\ASISH\\Desktop\\coding\\input1.txt", "r", stdin);
	freopen("C:\\Users\\ASISH\\Desktop\\coding\\output1.txt", "w", stdout);
#endif

	int x, y;
	cin>>x>>y;
	if(abs(x-y)>=3) on("No");
	else on("Yes");
	cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "s\n";
	return 0;
}


#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
using namespace std;
//#pragma GCC optimize("Ofast")
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
#define lcin(x) ll x; cin >> x;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define irep(it, stl) for(auto it = stl.begin(); it != stl.end(); it++)
#define drep(i,n) for(int i = (n) - 1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define mp(p,q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;}
template <class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;}
struct io{io(){ios::sync_with_stdio(false);cin.tie(0);}};
const int INF = INT_MAX;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int main(){
    ll N;
    cin >> N;
    vs S(N);
    vll T(N);
    rep(i,N){
        cin >> S.at(i);
        cin >> T.at(i);
    }
    ll ma = 0;
    ll ma2 = 0;
    ll ma2in = -1;
    ll main = -1;
    rep(i,N){
        if(T.at(i) > ma){
            ma2in = main;
            main = i;
            ma2 = ma;
            ma = T.at(i);
        }else{
            if(T.at(i) > ma2){
                ma2in = i;
                ma2 = T.at(i);
            }
        }
    }
    cout << S.at(ma2in) << endl;

    return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

#include <algorithm>
#include <numeric>
#include <cmath>

using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;

static const int  MAX_INT  = std::numeric_limits<int>::max();
static const long MAX_LONG = std::numeric_limits<long>::max();
static const ll   MAX_LL   = std::numeric_limits<long long>::max();

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

///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////

struct mint {
    static const int mod = 1000000007;
    long long x; // typedef long long ll;
    mint(long long x = 0) :x((x%mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x; }
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x; }

void countNb(ll& res) {
    ll n, a, b; cin >> n >> a >> b;

    // ensure A <= B
    if (b < a) {
        ll tmp = a;
        a = b;
        b = tmp;
    }

    mint N(n), A(a), B(b);

    mint nbA = N - (A - 1); nbA = nbA * nbA;
    mint nbB = N - (B - 1); nbB = nbB * nbB;
    mint nbtotal = nbA * nbB;

    // dupulication: inside
    mint nbAb = B - (A - 1); nbAb = nbAb * nbAb;
    mint nbInside = nbAb * nbB;

    // cross
    mint cnt = 0;
    for (ll alpha = 1; alpha < a; ++alpha) {
        mint Alpha(alpha);
        mint nbCross = (B - A + Alpha * 2) * 4;
        mint nbIn = N - (B + Alpha - 1); nbIn = nbIn * nbIn;
        cnt += nbCross * nbIn;
    }

    mint count = nbtotal - nbInside - cnt;
    res = count.x;
}

void countNb2(ll& res) {
    mint N, A, B; cin >> N >> A >> B;
    ll nbSpace = N.x - A.x - B.x;
    mint x3 = (nbSpace >= 0) ? (N-A-B+2)*(N-A-B+1) : 0;
    mint x2 = (N-A+1)*(N-B+1) - x3;
    mint x1 = x2 * x2;
    mint total = (N-A+1)*(N-B+1) * (N-A+1)*(N-B+1);
    mint ans = total - x1;
    res = ans.x;
}

void Main() {
    ll T; cin >> T;
    vector<ll> res(T);
    for (ll i = 0; i < T; ++i) {
        countNb2(res[i]);
    }

    rep(i, T) {
        cout << res[i] << endl;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout << std::fixed << std::setprecision(15);
    Main();

    double tmp;
    cin >> tmp;

    return 0;
}


#include <iostream>
using namespace std;

int main() {
	int a, b, c;
  	cin >> a >> b >> c;
  	a  += c;
  	if (a > b) puts("Takahashi");
  	else puts("Aoki");
}

#include <bits/stdc++.h>
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int
using namespace std;

int main()
{
    fio;
    ll tc,n,a,b,m,x,y,i,j,k,c;
    //cin>>tc;
    //while(tc--)
    {
        cin>>a>>b>>c;
        if(a==b)
        {
            if(c==0)
                cout<<"Aoki\n";
            else
                cout<<"Takahashi\n";
        }
        else
        {
            if(a<b)
                cout<<"Aoki\n";
            else if(a>b)
                cout<<"Takahashi\n";
        }
    }
    return 0;
}


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

int main()
{
   int a,b,c,d;
   cin>>a>>b>>c>>d;

   if(b>=(d*c))
   cout<<(-1);
   else
   {  long long int n=0;
       while((a+(n*b))>(d*n*c))
       {
           n++;
       }
       cout<<n;
   }

}

#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 F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(c) c.begin(), c.end()
#define TR(c, it) for (auto it = c.begin(); it != c.end(); it++)
#define SZ(c) int(c.size())
#define present(c, e) (c.find(e) != c.end())
#define cpresent(c, e) (find(ALL(c),e) != c.end())
#define ffasstt() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define pii(T) pair<T, T>
#define v1(T) vector<T>
#define v2(T) vector< vector<T> >
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class T> void inline Scan(v1(T) &arr){ for(auto &i:arr) cin >> i; }
template<class T> void inline Print(v1(T) &arr){ for(auto &i:arr) cout << fixed << setprecision(7) << i << " "; }

void solve(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if(d * c - b <= 0){
        cout << -1;
        return;
    }
    cout << (int)ceil( (float) a / (float)(d * c - b));
}
int main()
{
    ffasstt();
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC <<" secs"<<endl;
    return 0;
}


//   |||   |||    |||||||    ||||    |||    |||    |||||||    ||||
//   ||| |||      |||        |||||   |||    ||         |||     |||
//   |||||        |||||||    ||| ||| |||           |||||||     |||
//   ||| |||          |||    |||   |||||           |||         |||
//   |||   |||    |||||||    |||    ||||           |||||||     |||
#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;

using ll = long long;

typedef priority_queue<int, vector<int>, greater<int>> pqg;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
    pbds;

#define F first
#define S second
#define ar array
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define INF 1000000000
#define pi acos(-1)
#define endl '\n'

void output_only(string filename)
{
#ifndef ONLINE_JUDGE
	freopen((filename + ".in").c_str(), "r", stdin);
	freopen((filename + ".out").c_str(), "w", stdout);
#endif
}

const ll MOD = 1e9+7;
const int mxN = 100005, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

/* DON'T GET STUCK ON ONE APPROACH */
void solve()
{
	ll a, b, c;
	cin >> a >> b >> c;

	if (c&1)
	{
		if (a >= 0 && b >= 0)
		{
			if (a == b) cout << "=" << endl;
			else if (a > b) cout << ">" << endl;
			else cout << "<" << endl;
		}
		else
		{
			if (a < 0 && b < 0) cout << (a < b ? ">" : "<") << endl;
	    	else cout << (a > b ? ">" : "<") << endl;
		}
	}
	else
	{
		a = abs(a), b = abs(b);
		if (a == b) cout << "=" << endl;
		else if (a > b) cout << ">" << endl;
		else cout << "<" << endl;
	}
}

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

	int t = 1;
    // cin >> t;

	while (t--) solve();

	return 0;
}

#line 1 "c.cpp"
/*
   Author   : MatsuTaku
   Date     : 01/02/21
*/

#include <bits/stdc++.h>
using namespace std;
#line 2 "/mnt/c/Users/tkmma/competitive_programming/CPBF/library/include/cpmacro.hpp"
#define rep(i, n)         for (int i = 0; i < (n); i++)
using lint = long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
void chmax(T& dst, T x) { dst = max(dst, x); }
template<typename T>
void chmin(T& dst, T x) { dst = min(dst, x); }
#line 9 "c.cpp"

class Solver {
 public:
  void solve();
};

void Solver::solve() {
  int n; cin>>n;
  unordered_set<string> S, T;
  rep(i, n) {
    string s; cin>>s;
    if (s[0] == '!') {
      string t = s.substr(1);
      if (T.count(t) > 0) {
        cout << t << endl;
        return;
      }
      S.insert(t);
    } else {
      if (S.count(s)) {
        cout << s << endl;
        return;
      }
      T.insert(s);
    }
  }
  cout << "satisfiable" << endl;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(10);
  Solver().solve();
  return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
#define tp make_tuple
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
typedef tuple<int,int,int> T;
typedef tuple<ll,ll,ll> TL;
ll mod = 1000000007;
ll mod2 = 998244353;

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

// セグ木 結合測と単位元の存在 and X × X' -> X モノイド
//遅延セグ木 作用付きモノイド

template <typename X>
struct SegTree {

    using FX = function<X(X, X)>; // X•X -> X となる関数の型
    int n;
    FX fx; // 演算
    const X ex; // 単位元
    vector<X> dat;

    SegTree(int n_, FX fx_, X ex_) : n(), fx(fx_), ex(ex_), dat(n_ * 4, ex_) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }

    void set(int i, X x) { dat[i + n - 1] = x; }

    void build() {
        for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
    }

    void update(int i, X x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }

    X query(int a, int b) { return query_sub(a, b, 0, 0, n); }

    X query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return ex;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return fx(vl, vr);
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,q;cin>>n>>q;
    auto fx = [](int x1, int x2) -> int { return x1 ^ x2; };
    int ex = 0;
    SegTree<int> rmq(n, fx, ex);

    rep(i,0,n){
        int a;cin>>a;
        rmq.set(i,a);
    }

    rmq.build();

    rep(i,0,q){
        ll t,x,y; cin>>t>>x>>y;
        if (t==1){
            x--;
            rmq.update(x, y ^ rmq.query(x,x+1));
        }else{
            x--;
            ll ans = rmq.query(x,y);
            cout<<ans<<endl;
        }
    }
}

#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;}

int main(){
    ll n,m;cin>>n>>m;
    vi a(n),b(m);
    rep(i,n)cin>>a[i];
    rep(i,m)cin>>b[i];
    vi ans;
    rep(i,n){
        bool ok=true;
        rep(j,m)if(a[i]==b[j])ok=false;
        if(ok)ans.pb(a[i]);
    }
    rep(i,m){
        bool ok=true;
        rep(j,n)if(a[j]==b[i])ok=false;
        if(ok)ans.pb(b[i]);
    }
    sort(all(ans));
    outv(ans);
}