//#include <atcoder/maxflow.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <bitset>
#include <fstream>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

//using namespace __gnu_pbds;
//using namespace atcoder;
using namespace std;

const ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//const ll modd = 998244353;

ll binary_search(function<bool(ll)> func, ll start, ll end) {
    /*      func:int ->bool
            returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ...
             */
    if (end <= start) {   return end;  }   // has to be here, otherwise func(end-1) in next line could be a problem
    if (!func(end-1)) {  return end;  }
    while (end-start>1) {
        ll mid = (start+end)/2;
        if (func(mid)) {  end = mid;  } else { start = mid;   }
    }
    if (func(start)) {  return start;  } else {  return end;   }
};

int main(int argc, char *argv[])   {

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

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

    cout.precision(12);

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n); readint(k);
        vector<vector<ll>> a;
        ll minn = modd*modd;
        forr(i,0,n) {
            a.push_back(vector<ll>());
            forr(j,0,n) {
                readll(aa);
                minn = min(minn, aa);
                a.back().push_back(aa);
            }
        }

        int bel = k*k - (k*k)/2;

        ll x = binary_search([&a,bel,k,n](ll med) {
            vector<vector<ll>> aa(n, vector<ll>(n, 0));
            forr(i,0,n) {
                forr(j,0,n) {
                    aa[i][j] = (a[i][j]<=med);
                }
            }
            vector<vector<ll>> summ(n+1, vector<ll>(n+1, 0));
            forr(i,1,n) {
                forr(j,1,n) {
                    summ[i][j] = -summ[i-1][j-1] + summ[i][j-1] + summ[i-1][j] + aa[i-1][j-1];
                }
            }

            bool ret = false;
            forr(i,0,n-k+1) {
                forr(j,0,n-k+1) {
                    ret |= (summ[i+k][j+k]-summ[i][j+k]-summ[i+k][j]+summ[i][j]>=bel);
                }
            }

            return ret;
        }, 0, modd);

        cout << x << endl;

    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> acum;

bool check_median(int median, int K,
		  vector<vector<int>> &A){
  int N = A.size() - 1;
  acum.assign(N+1, vector<int>(N+1, 0));

  for(int i = 1; i <= N; i++){
    for(int j = 1; j <= N; j++){
      acum[i][j] = A[i][j] <= median;
      acum[i][j] += acum[i][j-1] + acum[i-1][j] - acum[i-1][j-1];
    }
  }

  for(int i = K; i <= N; i++){
    for(int j = K; j <= N; j++){
      int cnt_less = acum[i][j];
      cnt_less -= acum[i-K][j] + acum[i][j-K] - acum[i-K][j-K];

      if(cnt_less >= (K*K + 1)/2){
	// printf("(%d, %d) -- %d\n", i, j, cnt_less);
	return true;
      }
    }
  }

  return false;
}

int main(){
  int N, K;
  while(scanf("%d%d", &N, &K) > 0){
    vector<vector<int>> A(N+1, vector<int>(N+1));

    for(int i = 1; i <= N; i++){
      for(int j = 1; j <= N; j++){
	scanf("%d", &A[i][j]);
      }
    }

    int lo = 0, up = 1e9, ans;

    while(lo <= up){
      int mid = (lo + up) >> 1;

      if(check_median(mid, K, A)){
	ans = mid;
	up = mid-1;
      } else {
	lo = mid+1;
      }
    }

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


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

//方向を決定する演算子的なもの
int di[] = {-1, 0, 1, 0}; //
int dj[] = {0, -1, 0, 1}; //

int main() {
  int H, W, X, Y;
  cin >> H >> W >> X >> Y;
  --X; //配列変数の関係上デクリメント
  --Y; //同様
  //H行W列のマスの入力
  vector <string> s(H);
  rep(i, H) cin >> s[i];
  int ans = 1; //自身がいるマスを考慮,初期値1
  rep(k, 4) {
    //k=0上方向,k=1左方向,k=2下方向,k=3右方向
  	int ni = X; int nj = Y;
    //マスから出るか、「#」にぶつかるまでkで与えた方向へ動かす
    while(true) {
    	ni += di[k];
      	nj += dj[k];
      	if(ni < 0 || nj < 0 || ni >= H || nj >= W) break;
      	if(s[ni][nj] == '#') break;
      	++ans;
    }
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*

*/
//UnionFind
vector<vector<ll>> G;
vector<bool> seen;
struct UnionFind{
  vector<ll> par,siz;

  UnionFind(ll N):par(N+1), siz(N+1, 1){
    for(int i = 0; i <= N; i++)
      par[i] = i;
  }

  void mkpar(ll n, ll set){//親はset=根
    for(auto next_n: G[n]){
      if(seen[next_n]) continue;
      seen[next_n] = true;
      par[next_n] = set;
      mkpar(next_n, set);
      siz[n] += siz[next_n];
    }
  }

  void mkpar2(ll n){//親は1個上。sizとれる。
    for (auto next_n: G[n]){
      if (seen[next_n]) continue;
      seen[next_n] = true;
      par[next_n] = n;
      mkpar2(next_n);
      siz[n] += siz[next_n];
    }
  }

  ll root(ll x){
    if(par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(ll x, ll y){
    ll rx = root(x), ry = root(y);
    if(rx == ry)
      return;
    par[ry] = rx;
    siz[rx] += siz[ry];
  }

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

  ll size(ll x){
    return siz[root(x)];
  }
};

ll N, M;
ll X[30];

ll ans=1;
ll tmp=0;
ll tsize=0;
ll cnt=0;

bool check(ll n){
  for(auto v: G[n]){
    if(X[n]==X[v]) return true;
  }
  return false;
}

void dfs2(ll n){
  for(ll i=1; i<=3; i++){
    //まず、隣り合ってないか確認
    X[n]=i;
    if(check(n)) continue;
    if(cnt==tsize)
      tmp++;
    else{
      for(auto v:G[n]){
        if(X[v]) continue;
        dfs2(v);
      }
    }
  }
  X[n]=0;
}

vector<ll> L;
void dfs(ll n){
  if(n==tsize){
    tmp++;
    return ;
  }
  ll v=L[n];
  if(n==0){
    X[v]=1;
    dfs(n+1);
  }
  else{
    for(ll j=1; j<=3; j++){
      X[v]=j;
      if(check(v)) continue;
      dfs(n+1);
    }
    X[v]=0;
  }
}

int main(){
  cin >> N >> M;
  G.assign(N+1, vector<ll>());
  seen.assign(N+1, false);
  rep(i, M){
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  UnionFind tree(N);
  for(ll i = 1; i <= N; i++){
    seen[i] = true;
    tree.mkpar(i, i);
  }
  for(int i = 1; i <= N; i++){
    if(tree.par[i]==i){
      tmp=0;
      L.assign(0, 0);
      for(int j = 1; j <= N; j++)
        if(tree.par[j]==i) L.push_back(j);
      tsize=L.size();
      if(tsize==1) ans*=3;
      else{
        dfs(0);
        ans *= tmp*3;
      }
    }
  }
  cout << ans << endl;
}


#include<iostream>
#include<vector>
#include<algorithm>

int main() {
	int n;
	std::cin >> n;
	std::vector<int> t(n),l(n), r(n);
	for (int i = 0; i < n; i++) {
		std::cin >> t[i] >> l[i] >> r[i];
	}

	int count = 0;
	for (int i = 0; i < n-1; i++) {
		for (int j = i + 1; j < n; j++) {
			//printf("%d %d %d %d\n",l[i],l[j],r[i],r[j]);
			if (std::min(r[i], r[j]) - std::max(l[i], l[j]) >= 0)count++;
			if (r[i] == l[j] || r[j] == l[i]) {
				if (l[i] < l[j]) {
					if(((t[i]==1)||t[i]==3)&&(t[j]==2||t[j]==1)){}
					else count--;
				}
				else {
					if (((t[j] == 1) || t[j] == 3) && (t[i] == 2 || t[i] == 1)) {}
					else count--;
				}

			}
		}
	}
	std::cout << count << std::endl;
}

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

#define maxn 105

string arr[maxn];
bool vis[maxn];
int n;

int dfs(int x) {
    int ret = 1;
    vis[x] =  true;
    for(int y = 0; y < n; y++) {
        if(!vis[y] && arr[y][x] == '1') {
            ret += dfs(y);
        }
    }
    return ret;
}

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    double ans = 0.0;
    for(int i = 0; i < n; i++) {
        memset(vis, 0, sizeof(vis));
        ans += 1.00 / dfs(i);
    }
    printf("%.12f\n", ans);
    return 0;
}


/* -*- compile-command: "g++ --std=c++17 -o sol sol.cpp" -*- */

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <stack>
#include <limits.h>
#include <math.h>
#include <memory>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

static auto speedup = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return nullptr;
}();

int main(int argc, char *argv[]) {

	int N, X;
	cin >> N >> X;

	int S = 0;
	int i = 0;
	X *=100;
	for(; i < N; ++i)
	{
		int v, p;
		cin >> v >> p;

		S += v*p;

		if(S > X)
		{
			break;
		}
	}

	if(i == N)
		cout << -1 << endl;
	else
		cout << i + 1 << endl;

  return 0;
}

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

//#include <atcoder/all>
// using namespace atcoder;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;

static const long long mod = 1000000007;

struct mint {
  ll x;  // typedef long long ll;
  mint(ll 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(ll 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; }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

class modutils {
  vector<mint> fact, invfact;

 public:
  modutils(int n = 200005) : fact(n + 1), invfact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;
    invfact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--) invfact[i - 1] = invfact[i] * i;
  }
  mint pow(mint x, ll n) { return x.pow(n); }
  mint comb(ll n, ll k) {
    if (n < 0 || k < 0 || n < k) return 0;
    return fact[n] * invfact[k] * invfact[n - k];
  }
  mint perm(ll n, ll k) {
    if (n < 0 || k < 0 || n < k) return 0;
    return fact[n] * invfact[n - k];
  }
  mint fac(ll n) { return fact[n]; }
};

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n, x;
  cin >> n >> x;
  x *= 100;
  ll now = 0;
  rep(i, n){
    ll v, p;
    cin >> v >> p;
    ll add = v*p;
    now += add;
    if(now > x){
      cout << i+1 << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
class Abc
{
public:
    int solve(int n)
    {
        int temp=0,temp1=0,p=0,q=0,f=0;
        p=n,q=n;
        while(p>0 && q>0)
        {
            temp=p%10;
            if(temp==7)
            {
                f=1;
                break;
            }
            temp1=q%8;
            if(temp1==7)
            {
                f=1;
                break;
            }
            p=p/10;
            q=q/8;
        }

        return f;
    }
};

int main()
{
    Abc ob;
    int n,i,cnt=0;
    cin>>n;
    for(i=1; i<=n; i++)
    {
        if(ob.solve(i)==1)
            cnt++;
    }
    cout<<n-cnt<<endl;
    return 0;
}

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

#define ll long long int
#define ld double
#define pb push_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define pre(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vll;
typedef vector<double>	vd;
typedef vector<bool>	vb;
typedef pair<ll,ll> 	pll;
#define br "\n"
#define ff first
#define ss second
#define gcd __gcd
#define debug(a...) cout<<#a<<": ";for(auto it:a)cout<<it<<" ";cout<<endl;
ll MAX = 1e6 + 1;
#define MAXIM 200005
ll mod = 1e9 + 7;

void solve(){
    ll n;
    cin >> n;
    ll ans = 0;
    rep(i,1,n + 1){
        ll t1 = i,t2 = i;
        bool f1 = true,f2 = true;
        while(t1 > 0){
            if(t1 % 10 == 7) f1 = false;
            t1 /= 10;
        }
        while(t2 > 0){
            if(t2 % 8 == 7) f2 = false;
            t2 /= 8;
        }
        if(f1 && f2) ans++;
    }
    cout << ans;
}

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

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

	ll t = 1;
    // cin >> t;
    // calc();
	rep(i,0,t){
        // cout << "Case #" << i + 1 << ": ";
        solve();
        // test();
	}
}


#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(){
  long n;
  cin >> n;
  vector<pll> d(n);
  //cout << n << endl;
  rep(i,n){
    cin >> d.at(i).first >> d.at(i).second;
  }
  sort(all(d));
  long ans = 0;
  //cout << n << endl;
  rep(i,n){
    REP(j,i+1,n){
      long xs = d.at(i).first;
      long ys = d.at(i).second;
      long xt = d.at(j).first;
      long yt = d.at(j).second;
      long xv = xt - xs;
      long yv = yt - ys;
      REP(k,j+1,n){
        long xm = d.at(k).first;
        long ym = d.at(k).second;
        //cout << xs << ys << xt << yt << xm << ym << endl;
        //cout << xv << yv << endl;
        if(xv == 0){
          if(xm == xs) {
            //cout << xs << ys << xt << yt << xm << ym << endl;
            ans++;
          }
        }else{
          if((xm - xs) * yv % xv != 0) continue;
          long cy = ys + (xm - xs) * yv / xv;
          if(cy == ym){
            ans++;
          }
        }
      }
    }
  }
  if(ans > 0) YES;
  else NO;
}

#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#define ll long long int
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define vct vector<long long int>
const ll INF = 1e18;
const ll NINF = -1e18;
#define f first
#define s second
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, m;
    cin >> n >> m;
    vector<ll> v, v1;
    for (int i = 0; i < n; i++)
    {
        ll a;
        cin >> a;
        v.pb(a);
    }
    for (int i = 0; i < m; i++)
    {
        ll b;
        cin >> b;
        v1.pb(b);
    }
    vector<ll> v2;
    if (m > n)
    {

        for (int i = 0; i <n; i++)
        {
              auto it = find(v1.begin(), v1.end(), v[i]);
              if(it ==v1.end()){
                  v2.pb(v[i]);
              }
              else{
                  v1.erase(it);
              }
        }
        v2.insert(v2.end(), v1.begin(), v1.end());
        sort(v2.begin(), v2.end());
        for (int i = 0; i < v2.size(); i++)
        {
            cout<<v2[i]<<" ";
        }

    }
    else{
       for (int i = 0; i <m; i++)
        {
              auto it = find(v.begin(), v.end(), v1[i]);
              if(it ==v.end()){
                  v2.pb(v1[i]);
              }
              else{
                  v.erase(it);
              }
        }
        // cout<<v2.size()<<endl;
        v2.insert(v2.end(), v.begin(), v.end());
        sort(v2.begin(), v2.end());
        for (int i = 0; i < v2.size(); i++)
        {
            cout<<v2[i]<<" ";
        }

    }

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first

typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;

typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

const int inf = 1e9, maxn = 2e5 + 48, mod = 998244353, N = 2e5 + 12;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = acos(-1);
const ll INF = 1e18;

bool was[N];
int n, f;
vi g[N];

void add (int &x, int y) {
	x += y;
	if (x >= mod)
		x -= mod;
}

void dfs (int v) {
	was[v] = 1;
	for (auto to : g[v])
		if (!was[to])
			dfs(to);
}

main () {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> f;
		g[i].pb(f);
		g[f].pb(i);
	}

	int ans = 1;
	for (int i = 1; i <= n; ++i)
		if (!was[i]) {
			add(ans, ans);
			dfs(i);
		}

	add(ans, -1);
	cout << ans << endl;
}

/*
  Author: EnderDeer
  Online Judge: Luogu
*/

#include<bits/stdc++.h>

#define int long long
#define mem(x) memset(x,0,sizeof(x))
#define mod 998244353

using namespace std;

int read(){
   int s = 0,w = 1;
   char ch = getchar();
   while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();}
   while(ch >= '0' && ch <= '9')s = s * 10 + ch - '0',ch = getchar();
   return s * w;
}

int n;
int pre[1000010];
int siz[1000010];
int ans = 1;

void init(){
	for(int i = 1;i <= n;i ++)pre[i] = i;
}

int find(int x){
	if(pre[x] == x)return x;
	else return pre[x] = find(pre[x]);
}

signed main(){
	cin>>n;
	init();
	for(int i = 1;i <= n;i ++){
		int x;
		x = read();
		pre[find(i)] = find(x);
	}
	for(int i = 1;i <= n;i ++)siz[find(i)] ++;
	for(int i = 1;i <= n;i ++){
		if(siz[i]){
			ans = (ans << 1) % mod;
		}
	}
	cout<<(ans + mod - 1) % mod;
	return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <atcoder/all>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
//using namespace atcoder;

typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 55;
const int K = 20;
const int mod = 1e9 + 7;
int d[N][N];
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, m;
    cin >> n >> m;
    vector <pair<int,int>> a(m);
    vector <int> f(m);
    vector <int> w(n), p(n);
    for (int i = 0; i < n; i++) cin >> w[i];
    for (int i = 0; i < n; i++) p[i] = i;
    for (int i = 0; i < m; i++){
        cin >> a[i].S >> a[i].F;
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < m; i++){
        if (i != 0) f[i] = max(f[i - 1], a[i].S);
        else f[i] = a[i].S;
    }
    for (int i = 0; i < n; i++){
        if (w[i] > a[0].F){
            cout << -1;
            return 0;
        }
    }
    int res = 1e9;
    for (int i = 0; i < 40320; i++){
        next_permutation(p.begin(), p.end());
        int cur = 0;
        int cur_w = 0;
        for (int j = 0; j < n; j++){
            for (int l = j + 1; l < n; l++){
                int cur_w = 0;
                for (int k = j; k <= l; k++) cur_w += w[p[k]];
                if (cur_w <= a[0].F) d[j][l] = 0;
                else{
                    int z = (int)(lower_bound(a.begin(), a.end(), make_pair(cur_w, 0LL)) - a.begin()) - 1;
                    d[j][l] = f[z];
                }
            }
        }
        vector <int> pos(n);
        for (int j = 1; j < n; j++){
            pos[j] = pos[j - 1];
            for (int l = 0; l < j; l++){
                int need = d[l][j] - pos[j] + pos[l];
                if (need > 0) pos[j] += need;
            }
        }
        res = min(res, pos[n - 1]);
    }
    cout << res;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int N;
int X[20];
int Y[20];
int Z[20];
long long dp[131072][17];
long long dist[17][17];
int main(){
  int i, mask;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(X[Lj4PdHRW]);
      rd(Y[Lj4PdHRW]);
      rd(Z[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(1<<N);i++){
    int j;
    for(j=(0);j<(N);j++){
      dp[i][j] = 4611686016279904256LL;
    }
  }
  for(i=(0);i<(N);i++){
    int j;
    for(j=(0);j<(N);j++){
      dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) +max_L(Z[j]-Z[i], 0);
    }
  }
  dp[0][0] = 0;
  for(mask=(0);mask<(1<<N);mask++){
    for(i=(0);i<(N);i++){
      if(dp[mask][i] < 4611686016279904256LL){
        int j;
        for(j=(0);j<(N);j++){
          if(!((mask) &(1<<(j)))){
            chmin(dp[mask|(1<<j)][j], dp[mask][i] + dist[i][j]);
          }
        }
      }
    }
  }
  wt_L(dp[(1<<N)-1][0]);
  wt_L('\n');
  return 0;
}
// cLay varsion 20201018-1

// --- original code ---
// int N, X[20], Y[20], Z[20];
// ll dp[131072][17], dist[17][17];
// {
//   rd(N,(X,Y,Z)(N));
//   rep(i,1<<N) rep(j,N) dp[i][j] = ll_inf;
//   rep(i,N) rep(j,N) dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) + max(Z[j]-Z[i], 0);
//   dp[0][0] = 0;
//   rep(mask,1<<N) rep(i,N) if(dp[mask][i] < ll_inf) rep(j,N) if(!BIT_ith(mask,j)){
//     dp[mask|(1<<j)][j] <?= dp[mask][i] + dist[i][j];
//   }
//   wt(dp[(1<<N)-1][0]);
// }


// Parasparopagraho Jīvānām

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

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

using namespace  __gnu_pbds ;
using namespace __gnu_cxx ;
using namespace std ;

typedef long long ll ;
typedef long double ldb ;

typedef pair<int, int> pii ;
typedef pair<int, ll> pil ;
typedef pair<ll,ll> pll ;

typedef vector<int> vi ;
typedef vector<ll> vll ;
typedef vector<pii> vpi ;
typedef vector<pll> vpl ;
typedef vector<bool> vb ;
typedef vector<pair<ll,int> > vpli ;
typedef vector<int,pil> edges ;
typedef vector<vi> matrix ;
typedef priority_queue<int> pqu ;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;

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

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define ins insert
#define bk back
#define con continue
#define lbd lower_bound
#define ubd upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

#define pr cout << // I don't like this but my laptop's 'O' key is not functioning well

/* Careful when using long long
__builtin_clz(int x) {} //: the number of zeros at the beginning of the number
__builtin_ctz(int x) {} //: the number of zeros at the end of the number
__builtin_popcount(int x) {} //: the number of ones in the number
__builtin_parity(int x) {} //: the parity (even or odd) of the number of ones */

const int mod1 = 1000000007 ;
const int mod2 = 998244353 ;
const ll  infl = 4e18 ;
const int infi = 2e9 ;
const int maxn = 1e6 + 5 ;
const int block = 500 ;
const int logn = 60 ;
const int alpha = 27 ;
const ldb pi = acos(-1) ;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
int rng_lr(int a, int b)
{
    int ret ;
    ret = uniform_int_distribution<int>(a, b)(rng) ;
    return ret ;
    // For shuffling use shuffle(all, rng) ;
}

ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; }
void upmin(int &a, int b) { if(a < b) { a = b ; } }
void relax(int &a, int b) { if(a > b) { a = b ; } }
ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return  a ; }
ll sub(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; }
ll mul(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; }
ll inverse(ll a, ll MOD) { a = modpow(a, MOD - 2, MOD) ; return a ; }
ll lcm(ll a, ll b) { ll ret ; ll g = __gcd(a, b) ; ret = a/g ; ret = ret*b ; return ret ; }
int icast(char ch) { return int(ch-'a') ; }
void hyn(int a, int b) { if(a == b) cout << "YES\n" ; else cout << "NO\n" ; }
void pyd(int a, int b) { if(a == b) cout << "First\n" ; else cout << "Second\n" ; }

// Check constraints + overflows + types in typedef

int main()
{
    ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    int n ;
    string s ;
    cin >> n >> s ;
    if(s[0] != s[n-1]) {
    	pr "1\n" ;
    	return 0 ;
    }
    rep(i,1,n-2) {
    	if(s[i] != s[0] && s[i + 1] != s[0]) {
    		pr "2\n" ;
    		return 0 ;
    	}
    }
    pr "-1\n" ;
    return 0 ;
}

#include <iostream>
#include <string>

using namespace std;
int main(){
    int i,n; cin >> n;
    string s; cin >> s;
    if(s[0]!=s.back()){
        cout << 1 << endl;
        return 0;
    }
    char c = s[0];
    for(int i=0;i<s.size();i++){
        if(s[i]!=c && s[i + 1]!=c){
            cout << 2 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> c(n, vector<ll>(n));
  REP(i, n) REP(j, n) { cin >> c[i][j]; }

  vector<ll> a(n, 0), b(n);
  REP(j, n) { b[j] = c[0][j]; }

  const ll INF = 1e18;
  FOR(i, 1, n) {
    // a[i]
    ll d = INF;
    REP(j, n) {
      ll e = c[i][j] - b[j];
      if (d == INF) {
        d = e;
      } else if (d != e) {
        cout << "No\n";
        return 0;
      }
    }
    a[i] = d;
  }

  // 非負制約
  ll mini = INF;
  REP(i, n) { mini = min(mini, a[i]); }
  if (mini < 0) {
    REP(i, n) { a[i] += -mini; }
  }
  REP(j, n) {
    if (mini < 0) {
      b[j] += mini;
    }
    if (b[j] < 0) {
      cout << "No\n";
      return 0;
    }
  }

  cout << "Yes\n";
  REP(i, n) { cout << a[i] << " \n"[i == n - 1]; }
  REP(i, n) { cout << b[i] << " \n"[i == n - 1]; }
}

#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <chrono>
// using namespace std::chrono;
// using namespace __gnu_pbds;

#define ff              first
#define ss              second
#define int             long long
#define double          long double
#define pb              push_back
#define mp              make_pair
#define pi              pair<int,int>
#define vtiii           vector<tuple<int,int,int>>
#define vi              vector<int>
#define vpii            vector<pair<int,int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int>>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define all(x) (x).begin(), (x).end()
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

const int N = 2e5 + 10;
const int mod = 1e9 + 7 ;

void solve(){
    int n;cin>>n;
    if(n%100 != 0){
        cout<<n/100 + 1;
    }else cout<<n/100;
}

int32_t main(){
    // auto start = high_resolution_clock::now()
    FIO;
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        freopen("Error.txt", "w", stderr);
    #endif
    // precalc();
    // int x; cin>>x; while(x--)
    solve();
    // auto stop = high_resolution_clock::now();
    // auto duration = duration_cast<microseconds>(stop - start);
    // cout << duration.count() << endl;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),i##end=(b);i<i##end;i++)
#define REP(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++)
#define RFOR(i,a,b) for(int i=(a),i##end=(b);i>i##end;i--)
#define RREP(i,a,b) for(int i=(a),i##end=(b);i>=i##end;i--)
typedef long long LL;

const int maxn=1000+5;

std::queue<std::pair<int,int> > Q;
int distance[maxn][maxn],n,m;
std::vector<int> G[maxn][26];
int main() {
	memset(distance,-1,sizeof(distance));
	while(!Q.empty()) Q.pop();
	scanf("%d%d",&n,&m);
	REP(u,1,n) FOR(i,0,26) G[u][i].clear();
	REP(u,1,n) Q.push(std::make_pair(u,u)),distance[u][u]=0;

	while(m--) {
		int x,y,c; scanf("%d%d",&x,&y);
		while(!isalpha(c=getchar()));
		c-=97;
		G[x][c].push_back(y);
		G[y][c].push_back(x);
		if(distance[x][y]<0) distance[x][y]=distance[y][x]=1,Q.push(std::make_pair(x,y));
	}

	while(!Q.empty()) {
		std::pair<int,int> p=Q.front(); Q.pop();
		int u=p.first,v=p.second;
		if((u==1&&v==n)||(u==n&&v==1)) { printf("%d\n",distance[1][n]); return 0; }
		FOR(c,0,26) FOR(i,0,G[u][c].size()) FOR(j,0,G[v][c].size()) {
			int us=G[u][c][i],vs=G[v][c][j];
			if(distance[us][vs]>=0) continue;
			distance[us][vs]=distance[vs][us]=distance[u][v]+2;
			Q.push(std::make_pair(us,vs));
		}
	}
	printf("-1\n");
	return 0;
}

#include<bits/stdc++.h>
//#include <atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())

using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;

template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; }

const ll MOD = 1e9 + 7;
const ll INF = 1e16;

struct Edge{
    int to;
    char col;
};

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<Edge>> G(n);
    vector<int> a(m), b(m);
    rep(i, m){
        char c;
        cin >> a[i] >> b[i] >> c;
        a[i]--;b[i]--;
        G[a[i]].pb(Edge{b[i], c});
        G[b[i]].pb(Edge{a[i], c});
    }

    vector<vector<int>> nG(n * n);
    //グラフを再構築する
    auto push = [&](int ua, int ub, int va, int vb){
        nG[ua * n + ub].pb(va * n + vb);
    };
    rep(a, n){
        rep(b, n){
            for(auto ea : G[a]){
                for(auto eb : G[b]){
                    if(ea.col == eb.col)push(a, b, ea.to, eb.to);
                }
            }
        }
    }

    vector<ll> d(n * n, INF);
    priority_queue<P, vector<P>, greater<P>> pq;
    d[n - 1] = 0;
    pq.push(P(0, n - 1));

    while(sz(pq)){
        auto [cost, v] = pq.top();
        pq.pop();
        if(d[v] < cost)continue;
        for(auto nv : nG[v]){
            if(d[nv] > d[v] + 1){
                d[nv] = d[v] + 1;
                // cout << nv << " " << d[nv] << endl;
                pq.push(P(d[nv], nv));
            }
        }
    }
    ll res = INF;
    // rep(i, n)cout << d[i * n + i] << endl;
    rep(i, n)chmin(res, 2 * d[i * n + i]);
    rep(i, m){
        chmin(res, 2 * d[a[i] * n + b[i]] + 1);
        chmin(res, 2 * d[b[i] * n + a[i]] + 1);
    }
    if(res == INF)res = -1;
    cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

constexpr int N = 1000000;

bool is_radical[N + 1];
bool prime[N + 1];
int pf[N + 1];

int main() {
	long long cur, ans, l, r, n, d;

	scanf("%lld%lld", &l, &r);

	memset(is_radical, true, sizeof(is_radical));
	memset(prime, true, sizeof(prime));
	prime[0] = prime[1] = false;
	ans = 0;

	for (d = 2; d <= r; d++) {
		cur = 0;

		for (n = d; n <= r; n += d) {
			if (prime[d]) {
				pf[n]++;

				if (n > d) {
					prime[n] = false;
				}
			}

			if (l <= n and n <= r) {
				cur++;

				if (n > d and l <= d and d <= r) {
					ans -= 2;
				}
			}
		}

		for (n = d * d; n <= r; n += d * d) {
			is_radical[n] = false;
		}

		if (is_radical[d]) {
			if (pf[d] % 2) {
				ans += cur * (cur - 1);
			}
			else {
				ans -= cur * (cur - 1);
			}
		}
	}

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

	return 0;
}

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

typedef long long ll;
typedef pair<int,int> ii;

ll x;
map<ll, ll> sv;

ll f(ll y){
	if (sv.find(y) != sv.end()) return sv.find(y)->second;
	sv[y] = 0LL;
	ll &ans = sv[y];
	if (y>x){
		if (y&1){
			ll ri = (y+1LL);
			ll le = (y-1LL);
			ll directtarget = y-x;
			return ans = min(f(ri)+1LL, min(f(le)+1LL, f(x)+directtarget));
		}else{
			ll ab = y-x;
			ll divi = y/2;
			ll opsdivi = 1LL + abs(divi - x);
			if (opsdivi <= ab){
				return ans = 1LL + f(y/2LL);
			}else{
				return ans = ab + f(x);
			}
		}
	}else if (y<x){
		return ans = x-y + f(x);
	}else return ans = 0LL;
}

void solve(){
	ll y; cin >> x >> y;
	cout << f(y) << '\n';
}

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

	int t = 1; //cin >> t;
	while(t--) solve();

	return 0;
}
// NK


#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;

int main() {
  // freopen("#.in", "r", stdin); // @
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  int n; cin >> n;
  i64 sum = 0;
  vector<i64> a;
  rep(r, 0, n + 1) {
    a.push_back(sum);
    if (r == n) {break;}
    int x; cin >> x; r & 1 ? sum += x : sum -= x;
  }
  sort(all(a));
  i64 res = 0;
  for (int l = 0, r = 0; l < sz(a); l = r) {
    for (; r < sz(a) and a[r] == a[l]; ++r);
    res += 1ll * (r - l) * (r - l - 1) / 2;
  }
  cout << res << '\n';
  return 0;
}

/* 📌https://www.cnblogs.com/George1123/p/tips.html */

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

unordered_map <long long, int> mp;

int main() {
	int n; scanf("%d", &n);
	long long sum = 0, ans = 0;
	mp[0] = 1;
	for (int i = 0; i < n; ++i) {
		int x; scanf("%d", &x);
		if (i & 1)
			sum -= x;
		else
			sum += x;
		ans += mp[sum]++;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;

    for(int i = 3, c1 = 1 ; i <= 1e18 ; i*=3, c1++)
    {
    	for(int j = 5, c2 = 1 ; j <= 1e18 ; j*=5, c2++)
    	{
    		if(i+j == N)
    		{
    			cout << c1 << " " << c2 << '\n';
    			return 0;
    		}
    	}
    }

    cout << -1 << '\n';

    return 0;
}

#include"bits/stdc++.h"
using namespace std;
using ll=long long;
template<class T=long long>inline T in(istream&is=cin){T ret;is>>ret;return ret;}
template<class T,class Size=typename vector<T>::size_type>inline auto vector_2d(Size h,Size w,T v){return vector<vector<T>>(h,vector<T>(w,v));}
template<class T>auto comp_pairs_by_second(){return[](const T&p1,const T&p2){return p1.second<p2.second||(p1.second==p2.second&&p1.first<p2.first);};}

int main()
{
    vector<ll>p3{3},p5{5};
    while(p3.back()<static_cast<ll>(1e+18))p3.push_back(p3.back()*3);
    while(p5.back()<static_cast<ll>(1e+18))p5.push_back(p5.back()*5);
    ll n=in();
    for(int i=0;i<p3.size();++i){
        for(int j=0;j<p5.size();++j){
            if(p3[i]+p5[j]==n){
                cout<<i+1<<' '<<j+1<<endl;
                return 0;
            }
        }
    }
    cout<<"-1"<<endl;
}


/*
* Author: JophieQu
* Date: 06/06/21 19:53
* Welcome to follow my sister's CODEFORCES account:@Cccc23333
*/
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
const int N=2e3+7;
const int mod=1e9+7;
using namespace std;
ll ans=0;
vector<ll>e[N+1];
int vis[N];
void dfs(int ii,int fa,int f){
	if(ii==fa&&f) return ;
	for(int i=0;i<e[ii].size();++i){
		if(!vis[e[ii][i]]){
			vis[e[ii][i]]=1;
			++ans;
		//	cout<<fa<<" "<<ii<<" "<<e[ii][i]<<" "<<ans<<endl;
			dfs(e[ii][i],fa,1);
		}
	}
}
void solves(){
	int n,m;cin>>n>>m;
	while(m--){
		int a,b;cin>>a>>b;
		e[a].push_back(b);
	}
	for(int i=1;i<=n;++i){
		memset(vis,0,sizeof(vis));
		vis[i]=1;
		dfs(i,i,0);
	}
	cout<<ans+n<<endl;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int OwwwO=1;
//	cin>>OwwwO;
	while(OwwwO--){
		solves();
	}
}

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep3(i, a, b) for (ll i = a; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define nl '\n'
#define endl '\n'
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
#define pcnt(x) __builtin_popcountll(x)
#define df(x) ll x = in();
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vs = vector<string>;
using P = pair<ll, ll>;
using gt = greater<P>;
template <class T> using minq = priority_queue<T, vector<T>, greater<T>>;
using vP = vector<P>;
inline ll in() {
  ll x;
  scanf("%lld", &x);
  return x;
}
template <class T> void takeUnique(vector<T> &v) {
  auto last = std::unique(v.begin(), v.end());
  v.erase(last, v.end());
}
template <class T> void print(const initializer_list<T> &il) {
  for (auto x : il) {
    cout << x << " ";
  }
  cout << "\n";
}
inline void priv(vll a) {
  rep(i, (int)a.size() - 1) cout << a[i] << " ";
  cout << a.back() << nl;
}
inline void priv(vi a) {
  rep(i, (int)a.size() - 1) cout << a[i] << " ";
  cout << a.back() << nl;
}
const ll LINF = 8e18L + 1;
const int INF = 8e9 + 1;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1};
// g++ -std=c++17 -stdlib=libc++
#define _GLIBCXX_DEBUG
// This slows down the execution; even the time complexity, since it checks if
// std funcs such as lower_bound meets prereqs
vi to[2005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(16);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
  }
  int ans = 0;
  auto f = [&](int v) {
    vi vis(n);
    vis[v] = 1;
    queue<int> q;
    q.push(v);
    while (q.size()) {
      int u = q.front();
      q.pop();
      for (int nx : to[u]) {
        if (vis[nx])
          continue;
        q.push(nx);
      }
      vis[u] = true;
    }
    rep(i, n) {
      if (vis[i])
        ans++;
    }
  };
  rep(i, n) { f(i); }
  cout << ans << nl;

  return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);

	int N;
    int x = 0;
    vector<int> A;
    vector<int> X;
    vector<vector<int>> dp;

    cin>>N>>x;

    A.resize(N);
    X.resize(N);
    dp.resize(N+1, vector<int>(2));

    for(int i = 0; i < N; i++){
        cin>>A[i];
    }

    for(int i = N-1; i >= 0; i--){
        X[i] = x/A[i];
        x %= A[i];
        // cout<<"X = "<<X[i]<<endl;
    }

    dp[0][0] = 1;

    for(int i = 0; i < N; i++){
        if(X[i] == 0) {
            dp[i+1][0] += dp[i][0];
            dp[i+1][0] += dp[i][1];
            dp[i+1][1] += dp[i][1];
        } else if(i + 1 != N && X[i] == A[i+1]/A[i] - 1) {

            dp[i+1][0] += dp[i][0];

            dp[i+1][1] += dp[i][0];
            dp[i+1][1] += dp[i][1];
        } else {
            dp[i+1][0] += dp[i][0];
            dp[i+1][1] += dp[i][0];

            dp[i+1][0] += dp[i][1];
            dp[i+1][1] += dp[i][1];
        }
        // cout<<"i = "<<i<<endl;
        // cout<<dp[i][0]<<endl;
        // cout<<dp[i][1]<<endl;
        // cout<<endl;
    }

    // cout<<dp[N][0]<<endl;
    // cout<<dp[N][1]<<endl;
    cout<<dp[N-1][0] + dp[N-1][1]<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(40)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

ll A[300005];

template<typename T>
struct SegmentTree {
    using F = function<T(T, T)>;

    SegmentTree(F func, T id)
            : merge(func), identity(id) {}

    void init(ll n) {
        offset = 1;
        while (offset < n)
            offset <<= 1;
        node.assign(offset << 1, identity);
    }

    void build(const vector<T> &v) {
        int n = static_cast<int>(v.size());
        init(n);
        for (int i = 0; i < n; ++i) {
            node[offset + i] = v[i];
        }
        for (int i = offset - 1; i >= 0; --i) {
            node[i] = merge(node[(i << 1) | 0], node[(i << 1) | 1]);
        }
    }

    void set(int index, T value) {
        index += offset;
        node[index] = value;
        while (index >>= 1) {
            node[index] = merge(node[(index << 1) | 0], node[(index << 1) | 1]);
        }
    }

    //! [left, right]
    T get(int left, int right) {
        left += offset;
        right += offset + 1;

        T val = identity;
        while (left < right) {
            if (left & 1)
                val = merge(val, node[left++]);
            if (right & 1)
                val = merge(val, node[--right]);

            left >>= 1;
            right >>= 1;
        }
        return val;
    }

    T identity;
    F merge;
    vector<T> node;
    int offset;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, Q;
    cin >> N >> Q;

    SegmentTree<ll> seg([](ll a, ll b) { return a ^ b; }, 0);
    seg.init(N);
    REP(i, N) {
        ll a;
        cin >> a;
        seg.set(i, a);
    }

    REP(_, Q) {
        ll t, a, b;
        cin >> t >> a >> b;
        a--;
        if (t == 1) {
            ll p = seg.get(a, a);
            seg.set(a, (p ^ b));
        }
        else {
            b--;
            cout << seg.get(a, b) << endl;
        }
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define endl '\n'
#define FASTINOUT ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int N=3e5+9;
ll a[N],tree[4*N];
void build(int node, int start, int end)
{

    if (start == end)
    {
        tree[node] = a[start];
    }
    else
    {
        int mid = (start + end) / 2;

        build(2 * node, start, mid);

        build(2 * node + 1, mid + 1, end);

        tree[node] = (tree[2 * node]^tree[2 * node + 1]);
    }

}
long long query_range(int node, int start, int end, int l, int r)
{

    if (start > r or end < l)
        return 0;
    if (start >= l and end <=r)
        return tree[node];
    int mid = (start + end) / 2;
    long long p1 = query_range(node * 2, start, mid, l, r);
    long long p2 = query_range(node * 2 + 1, mid + 1, end, l, r);
    return (p1^p2);

}

void update(int node, int start, int end, int position, long long val)
{

    if (start > position or end < position)
        return;
    if (start==end)
    {
        a[start]=val^a[start];
        tree[node]=a[start];
        return;
    }
    int mid = (start + end) / 2;
    update(node * 2, start, mid, position, val);
    update(node * 2 + 1, mid + 1, end, position, val);
    tree[node] =  (tree[node * 2] ^ tree[node * 2 + 1]);

}
int main()
{
    FASTINOUT;
    int x,q;
    cin>>x>>q;
    for (int i=1; i<=x; i++)
        cin>>a[i];
    build(1,1,x);
    while (q--)
    {
        ll type,l,r;
        cin>>type>>l>>r;
        if (type==1)
        {
            update(1,1,x,l,r);
        }
        else
        {
            cout<<query_range(1,1,x,l,r)<<endl;
        }
    }
    return 0;
}


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

int main(){
    vector<long long> ans;
    int a, b;
    cin >> a >> b;
    if (a == b){
      for (int i = 1; i <= a; i++){
        ans.push_back(i);
        ans.push_back(-i);
      }
    }else if (a > b){
      for (int i = 1; i <= a; i++){
        ans.push_back(i);
      }
      for (int i = 1; i < b; i++){
        ans.push_back(-i);
      }
      long long sum = accumulate(ans.begin(), ans.end(), 0);
      ans.push_back(-sum);
    }else{
      for (int i = 1; i < a; i++){
        ans.push_back(i);
      }
      for (int i = 1; i <= b; i++){
        ans.push_back(-i);
      }
      long long sum = accumulate(ans.begin(), ans.end(), 0);
      ans.push_back(-sum);
    }

    for (int i = 0; i < ans.size(); i++) cout << ans.at(i) << " ";
    cout << endl;
    return 0;
}

#pragma region Macros
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i)
#define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME
#define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__)
#define RREP2(i, n) for (int i = (n - 1); i >= 0; --i)
#define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i)
#define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME
#define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__)
#define IN(type, n) type n; cin >> n
#define INALL(type, v, s) vector<type> v(s); for (auto &e : v) { cin >> e; }
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl
#else
#define DEBUG(x)
#endif

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; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
#pragma endregion

int main() {
  int query = 1000;

  REP(q, query) {
    IN(int, si);
    IN(int, sj);
    IN(int, ti);
    IN(int, tj);
    int now_i = si;
    int now_j = sj;
    string order = "";

    while (true) {
      if (ti == now_i && tj == now_j) break;
      if (rand() % 2) {
        if (ti > now_i) {
          order += "D";
          now_i++;
        } else if (ti < now_i) {
          order += "U";
          now_i--;
        }
      } else {
        if (tj > now_j) {
          order += "R";
          now_j++;
        } else if (tj < now_j) {
          order += "L";
          now_j--;
        }
      }
    }

    cout << order << endl;
    IN(ll, score);
  }

  return 0;
}


// #include <atcoder/all>
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
/*-------------------------------------------*/

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

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

    cin >> n;
    int c[200]{};
    rep(i, n) {
        cin >> a[i] >> b[i];
        if(a[i] != -1) c[--a[i]]++;
        if(b[i] != -1) c[--b[i]]++;
    }

    rep(i, 2 * n) if(c[i] > 1) {
        cout << "No\n";
        return 0;
    }

    int type[200]{};
    int dp[200];
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    rep(i, n) {
        if(a[i] != -1) type[a[i]] = -(i + 1);
        if(b[i] != -1) type[b[i]] = (i + 1);
        if(a[i] == -1 && b[i] == -1) dp[0]++;
    }

    rep(i, n + 1) {
        rep(j, i) {
            int cnt = i - j;
            bool ok = true;
            FOR(k, j * 2, j + i) if(type[k] != 0) {
                if(type[k] > 0)
                    ok = false;
                else if(type[k + (i - j)] != 0 && type[k] != -type[k + (i - j)])
                    ok = false;
                cnt--;
            }
            FOR(k, j + i, i * 2) if(type[k] != 0) {
                if(type[k] < 0) ok = false;
                if(type[k - (i - j)] == 0) cnt--;
            }
            if(!ok) continue;
            chmax(dp[i], dp[j] - cnt);
        }
    }

    cout << (dp[n] >= 0 ? "Yes" : "No") << "\n";

    return 0;
}

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

void fail(){
    cout << "No" << endl;
    exit(0);
}

int main(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    vector<pair<int, int>> p2n(2*N+1, {-1, -1});
    for(int i=0; i<N; i++){
        cin >> A[i] >> B[i];
        if(A[i] != -1 && B[i] != -1 && A[i] >= B[i]) fail();
        if(A[i] != -1){
            if(p2n[A[i]].first != -1) fail();
            p2n[A[i]] = {i, 0};
        }
        if(B[i] != -1){
            if(p2n[B[i]].first != -1) fail();
            p2n[B[i]] = {i, 1};
        }
    }

    auto check = [&](int L, int R)->bool{
        int d = (R-L)/2;
        for(int l=L; l<L+d; l++){
            int r = l+d;
            auto [li, lc] = p2n[l];
            auto [ri, rc] = p2n[r];
            if(li != -1){
                if(lc != 0) return false;
                if(B[li] != -1 && B[li] != r) return false;
            }
            if(ri != -1){
                if(rc != 1) return false;
                if(A[ri] != -1 && A[ri] != l) return false;
            }
            if(li != -1 && ri != -1 && li != ri) return false;
        }
        return true;
    };

    vector<bool> ok(2*N+2);
    ok[1] = true;
    for(int i=1; i<=2*N; i+=2) if(ok[i]) for(int r=i+2; r<=2*N+1; r+=2){
        if(check(i, r)) ok[r] = true;
    }

    cout << (ok[2*N+1] ? "Yes" : "No") << endl;
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;

#define S 600020

struct dinic {
	struct edge {int b, cap, flow, flip;};
	vector<edge> g[S+2];
	int ans=0, d[S+2], ptr[S+2];

	void add_edge (int a, int b, int cap) {
		g[a].push_back({b, cap, 0, g[b].size()});
		g[b].push_back({a, 0, 0, g[a].size()-1});
	}

	int dfs (int u, int flow=INT_MAX) {
		if (u==S+1 || !flow) return flow;
		while (++ptr[u] < g[u].size()) {
			edge &e = g[u][ptr[u]];
			if (d[e.b] != d[u]+1) continue;
			if (int pushed = dfs(e.b, min(flow, e.cap-e.flow))) {
				e.flow += pushed;
				g[e.b][e.flip].flow -= pushed;
				return pushed;
			}
		}
		return 0;
	}

	void calc() {
		do {
			vector<int> q {S};
			memset(d, 0, sizeof d);
			int i = -(d[S] = 1);
			while (++i<q.size() && !d[S+1])
				for (auto e: g[q[i]])
					if (!d[e.b] && e.flow<e.cap) {
						q.push_back(e.b);
						d[e.b] = d[q[i]]+1;
					}

			memset(ptr, -1, sizeof ptr);
			while(int pushed=dfs(S)) ans+=pushed;
		} while (d[S+1]);
	}
};

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

	int n;
	cin>>n;
	set<int> colors;
	for (int i=0; i<n; ++i) {
		int a, b;
		cin>>a>>b;
		flow.add_edge(S, i, 1);
		flow.add_edge(i, a+n, 1);
		flow.add_edge(i, b+n, 1);
		colors.insert(a);
		colors.insert(b);
	}
	for (auto c:colors) {
		flow.add_edge(c+n, S+1, 1);
	}
	flow.calc();
	cout<<flow.ans<<endl;
}


#include<bits/stdc++.h>
#include<cmath>
#define SZ(x) ((int)x.size())
#include<vector>
#include<set>
#include<map>
#define ll long long
#define lli long long int
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#include<string>
#include<cctype>
#define F first
#define rep(i,a,b)    for(int i=a;i<b;i++)
#define S second
#include<queue>
#include <sstream>
#define lli long long int
#define pairs pair<int,int>
#define ld long double
#define mod 1000000007;
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
//first try to write all condition on copy then go for implement
ll pows(ll a,ll n,ll m)
{
	a=a%mod;
	ll res=1;
	while(n)
	{
	if(n%2!=0)
	{
		res=(res*a)%m;
		n--;
		}
		else
		{
			a=(a*a)%m;
			n=n/2;
			}
	}
	return res%m;
}
int  gcd(int a,int b)
{
	if(b==0)
	return a;
	else
	return gcd(b,a%b);
}
bool isprime(ll n)
{
	if(n==1)
	{
		return false;
	}
	for(ll i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			return false;
		}
	}
	return true;
}
bool istrue(string s)
{
	int i=0;
	int j=s.size()-1;
	while(i<j)
	{
	if(s[i]==s[j])
	{
		i++;
		j--;
		}
		else
		{
		return false;
			}
	}
	return true;
}

const int N=2005;

string s[N];
ll dp[N][N];
ll phor[N][N];
ll pver[N][N];
ll pdia[N][N];

bool wall(int x,int y){
	if(x < 1 || y < 1 || s[x-1][y-1] == '#') return true;
	return false;
}

int main()
{
ios::sync_with_stdio(false);
	 cin.tie(0);
    cout.tie(0);
int h,w;
	cin >> h >> w;
	rep(i,0,h){
		cin >> s[i];
	}

	dp[1][1] = 1;
	phor[1][1] = 1;
	pver[1][1] = 1;
	pdia[1][1] = 1;

	rep(i,1,h+1){
		rep(j,1,w+1){
			if(i == 1 && j == 1) continue;
			if(wall(i,j)) continue;
			dp[i][j] = (phor[i-1][j] + pver[i][j-1] + pdia[i-1][j-1]) % mod;

			phor[i][j] = dp[i][j];
			pver[i][j] = dp[i][j];
			pdia[i][j] = dp[i][j];
			phor[i][j] += phor[i-1][j];
			pver[i][j] += pver[i][j-1];
			pdia[i][j] += pdia[i-1][j-1];

			phor[i][j] %= mod;
			pver[i][j] %= mod;
			pdia[i][j] %= mod;
		}
	}

	cout << dp[h][w]<<endl;
}

#include <bits/stdc++.h>

using namespace std;

const long long int MOD = 1e9 + 7;
int h,w;
long long int dp[2001][2001];
char board[2001][2001];
long long int up[2001];
long long int diag[2001];

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

    cin >> h >> w;

    for(int i=1;i<=h;i++)
    {
    	for(int j=1;j<=w;j++)
    	{
    		cin >> board[i][j];
    	}
    }

    memset(diag,0,sizeof(diag));
    memset(up,0,sizeof(up));

    dp[1][1] = 1;

    for(int i=1;i<=h;i++)
    {
    	long long int left = 0;
    	for(int j=1;j<=w;j++)
    	{
    		if(board[i][j]=='#')
    		{
    			left = 0;
    		}
    		else
    		{
    			if(i==1 && j==1)
    			{
    				left += 1;
    			}
    			else
    			{
    				dp[i][j] = ((dp[i][j]%MOD) + (left%MOD)%MOD);
    				dp[i][j]%=MOD;
    				dp[i][j] = ((dp[i][j]%MOD) + (up[j]%MOD)%MOD);
    				dp[i][j]%=MOD;
    				if(j>1)
    				{
    					dp[i][j] = ((dp[i][j]%MOD) + (diag[j-1]%MOD)%MOD);
    					dp[i][j]%=MOD;
    				}
    				left = ((left%MOD) + (dp[i][j]%MOD)%MOD);
    				left%=MOD;
    			}
    		}
    	}
    	long long int temp[2001];
    	//memcpy(temp,diag,sizeof(diag));
    	for(int j=1;j<=w;j++)
    	{
    		if(board[i][j]=='#')
    		{
    			up[j] = 0;
    		}
    		else
    		{
    			up[j] = ((up[j]%MOD) + (dp[i][j]%MOD)%MOD);
    			dp[i][j]%=MOD;
    		}
    	}
    	for(int j=w;j>=1;j--)
    	{
    		if(j==1)
    		{
    			diag[j] = dp[i][j];
    		}
    		else
    		{
    			if(board[i][j]=='#')
    			{
    				diag[j] = 0;
    			}
    			else
    			{
    				diag[j] = ((diag[j-1]%MOD) + (dp[i][j]%MOD)%MOD);
    				diag[j]%=MOD;
    			}
    		}
    	}
    }

    cout << dp[h][w] << '\n';

    return 0;
}


#include<bits/stdc++.h>
typedef double          D;
typedef long long int   LL;
typedef long double     LD;
#define OR              ||
#define AND             &&
#define nl              '\n'
#define S               string
#define inf             INT_MAX
#define SQR(a)          (a) * (a)
#define pb              push_back
#define GCD(a, b)       __gcd(a, b)
#define PI              2.0*acos(0.0)
#define pii             pair<int,int>
#define pll             pair<long long,long long>
#define LCM(a, b)       (a * b) / GCD(a, b)
#define mem(a,b)        memset(a,b,sizeof(a))
#define srtv(v)         sort(v.begin(),v.end())
//#define T               int t; cin>>t; while(t--)
#define Rep(i,a,b)      for(int i = a; i <= b; i++)
#define rep(i,a,b)      for(int i = a; i >= b; i--)
#define boost           ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inout           freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)

#define si(x)           scanf("%d",&x)
#define pi(x)           printf("%d",x)
#define ss(str)         scanf("%s",str)
#define pl(x)           printf("%lld",x)
#define sl(x)           scanf("%lld",&x)
#define sii(x,y)        scanf("%d %d",&x,&y)
#define sll(x,y)        scanf("%lld %lld",&x,&y)
#define siii(x,y,z)     scanf("%d %d %d",&x,&y,&z)
#define slll(x,y,z)     scanf("%lld %lld %lld",&x,&y,&z)
using namespace std;

/*
int bs(int l,int r,int n)
{
    int mid,res=inf;

    while(l<=r)
    {
        mid=l+(r-l)/2;

        if(check(mid,n))
        {
            r=mid-1;
            res=min(res,mid);
        }
        else
            l=mid+1;

    }

    return res;
}
*/

//--------------------------code starts here---------------------------------------
const int mx=100005;
vector<int> adj[mx];
bool vis[mx];
int clr[mx];
int now[mx];
bool isGood[mx];

void dfs(int u)
{
    vis[u]=true;
    now[clr[u]]++;
    if(now[clr[u]]==1)
        isGood[u]=true;
    for(int i=0;i<adj[u].size();i++)
    {
        if(!vis[adj[u][i]])
        {
            //now[clr[u]]++;
            dfs(adj[u][i]);
            //now[clr[u]]--;
        }

    }
    now[clr[u]]--;
}

int main()
{
    int n;
    si(n);

    Rep(i,1,n)
    {
        si(clr[i]);
    }
    int u,v;
    Rep(i,1,n-1)
    {
        sii(u,v);
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(1);

    Rep(i,1,n)
    {
        if(isGood[i])
        {
            printf("%d\n",i);
        }
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
//*******************************************Defines***************************************************************************
#define all(x) (x).begin(),(x).end()
#define sort(x) sort(x.begin(),x.end())
#define maxin(x) *max_element(x.begin(),x.end())
#define minin(x) *min_element(x.begin(),x.end())
#define lint long long int
#define nl '\n'
#define INF (lint)1e18
#define EPS (double)1/(INF)
//#define MOD (lint)998244353
#define MOD (lint)(1e9+7)
#define debug(x) cout<<#x<<"\t=\t"<<(x)<<"\n"
// ********************************************** Templates********************************************************************
template <typename T> inline T powmod(T a, T b){ if(b==0) return 1; T half = powmod(a,b/2); return (((half*half)%MOD)*(b%2?a:1))%MOD;}
template <typename T> inline T gcd(T a, T b) { while (b != 0) swap(b, a %= b); return a;}
template <typename T> inline T lcm(T a, T b) { return ((a/gcd(a,b))*b);}
template <typename T> inline T raise_pow(T base, T exp){
        if(exp==0) return (T)1;
        T half = raise_pow(base,exp/2);
        if(exp%2) return (half*half*base);
        return half*half;
}
template <typename T> inline T max_pow(T base, T exp){
        if(base<exp) return 0;
        return 1+max_pow(base/exp,exp);
}
void solve(){
        int n;
        cin>>n;
        vector<lint> arr(n);
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr);

        int r = (n/2);
        long double ans = 0;

        for(int i=0;i<n;i++){
            if(i+1>=r+1)
                ans+=arr[i];
            if(i+1==r+1 && n%2==1)
                ans=ans-((long double)arr[i])/2;
        }
        cout<<setprecision(25)<<ans/n;
}
int main()
{       ios_base::sync_with_stdio(0);cin.tie(0);lint testnum=1,totalnum=1;
        //cin>>totalnum;
        for(testnum=1;testnum<=totalnum;++testnum){
                //cout<<"Case #"<<testnum<<": ";
                solve();
                cout<<nl;
        }
        bool time=false;
        //time=true;
        if(time){
                std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
                std::chrono::duration<double> time_taken = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
                cout << "\nTime Taken: " <<setprecision(10)<<time_taken.count() << " s";
        }
return 0;}

/*
    created on: 12.06.2021

*/


#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// int num = atoi(string.c_str()); 文字列の整数変換
int keta(ll n){int ans=0;while(n>0){n /= 10; ans++;}return ans;}
ll _pow(ll a,ll b){ll ans = 1;rep(i,b){ ans *= a;}return ans;}

int main() {
string a;cin>>a;
map<int,int> m;
vector<int> v;
rep(i,a.size()){
   int cnt = a[i] - '0';
   m[cnt]++;
   v.push_back(cnt);
}
bool flag = false;
if(v.size() == 1){
if(v[0] == 8)flag = true;
}
else if(v.size() == 2){
   if((v[0]*10+v[1])%8==0 || (v[1]*10+v[0])%8==0)flag = true;
}
else{
   rep(i,10){
   rep(j,10){
      rep(k,10){
         map<int,int>M;
         M[i]++;M[j]++;M[k]++;
         int cnt = i*100 +j*10 +k;
         if(cnt % 8 !=0 || cnt == 0) continue;
         if(M[i]<=m[i]&&M[j]<=m[j]&&M[k]<=m[k]) flag = true;
      }
   }
}

}
if(flag){
   cout<<"Yes"<<endl;
}else{
   cout<<"No"<<endl;
}
return 0;
   }

#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;

int main() {
	string s;
	cin>>s;
	ll n=s.size();

	if(n==1){
		ll num=s[0]-'0';
		if(num%8)cout<<"No";
		else cout<<"Yes";
		return 0;
	}

	if(n==2){
		ll num=(s[0]-'0')*10+(s[1]-'0');
		if(num%8==0){
			cout<<"Yes";
			return 0;
		}

		num=(s[1]-'0')*10+(s[0]-'0');

		if(num%8==0){
			cout<<"Yes";
			return 0;
		}

		cout<<"No";
		return 0;
	}

	map<char,ll> help;
	for(ll i=0;i<n;i++) help[s[i]]++;

	vector<string> a;

	for(ll i=0;i<=999;i+=8){
		if(i%8==0){
			string h=to_string(i);
			ll to_add=max(0ll,3-(ll)h.size());

			while(to_add--) h='0'+h;

			map<char,ll> cn;
			cn[h[0]]++;
			cn[h[1]]++;
			cn[h[2]]++;
			if(cn[h[0]]<=help[h[0]] && cn[h[1]]<=help[h[1]] && cn[h[2]]<=help[h[2]]){
				cout<<"Yes";
				return 0;
			}
		}
	}
	cout<<"No";
	return 0;

}


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

// edit

void solve() {
    ll R, X, Y;
    cin >> R >> X >> Y;
    ll norm = X * X + Y * Y;
    if (norm == R * R) {
        cout << 1 << endl;
    } else if (norm < R * R) {
        cout << 2 << endl;
    } else {
        ll k = 1;
        while (norm > k * k * R * R) {
            k++;
        }
        cout << k << endl;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();

    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                 \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);                    \
	cout.tie(NULL);
#define Open_Sesame Open()
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define watch(x) cout << #x << " = " << x << endl;
#define endl "\n"
#define double long double
#define INF 0x3f3f3f3f3f3f3f3fLL
const int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
const int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
typedef long long ll;
typedef unsigned long long ull;
void Open()
{
#ifndef ONLINE_JUDGE
	freopen("standard.in", "r", stdin);
	freopen("standard.out", "w", stdout);
#else
	//freopen("eliminating.in", "r", stdin);
	//freopen("standard.out", "w", stdout);
#endif
}
const ll mod = int(1e9 + 7), N = 500 + 5, oo = int(1e9), M = N * 104 * 2;
//"The secret of getting ahead is getting started". Mark Twain

//if you get WR, don't try to debug the code too much: think about another idea;
//don't be afraid of letters,letter G doesn't mean  the problem is hard;
//don't stuck in one problem after you gets alot WR ,just jump to the next one

int main()
{
	Ma7moud_7amdy;
	//Open_Sesame;
	ll r, x, y;
	cin >> r >> x >> y;

	double dis = sqrt(x * x + y * y);
	if (dis == r) {
		cout << 1 << endl;
	}
	else if (dis < r)cout << 2 << endl;
	else {
		ll cnt = ceil(dis / r);
		int add = 0;// (dis / r) != (ceil(dis / r) * r);
		cout << cnt + add << endl;
	}
}
//"There's a way to do it better - find it". Thomas A. Edison


#line 1 "main.cpp"

/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <stack>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"

template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
  struct iter {
    std::size_t itr;
    constexpr iter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { ++itr; }
    constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  struct reviter {
    std::size_t itr;
    constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { --itr; }
    constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  const iter first, last;

public:
  constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
  constexpr iter begin() const noexcept { return first; }
  constexpr iter end() const noexcept { return last; }
  constexpr reviter rbegin() const noexcept { return reviter(*last - 1); }
  constexpr reviter rend() const noexcept { return reviter(*first - 1); }
};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

#include <type_traits>
#include <iterator>
#line 6 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

template <class T>
class rev_impl {
public:
  using iterator = decltype(std::rbegin(std::declval<T>()));

private:
  const iterator M_begin;
  const iterator M_end;

public:
  constexpr rev_impl(T &&cont) noexcept: M_begin(std::rbegin(cont)), M_end(std::rend(cont)) { }
  constexpr iterator begin() const noexcept { return M_begin; }
  constexpr iterator end() const noexcept { return M_end; }
};

template <class T>
constexpr decltype(auto) rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */
#line 18 "main.cpp"

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  usize N;
  std::cin >> N;
  std::vector<u32> S(N), T(N);
  for (auto &x: S) {
    char c;
    std::cin >> c;
    x = c - '0';
  }
  for (auto &x: T) {
    char c;
    std::cin >> c;
    x = c - '0';
  }
  std::stack<usize> one;
  {
    u32 count = 0;
    for (auto i: rev(range(0, N))) {
      if (S[i] == 1) {
        count += 1;
        one.push(i);
      }
      if (T[i] == 1) {
        if (count == 0) {
          std::cout << "-1\n";
          return 0;
        }
        count -= 1;
      }
    }
    if (count % 2 == 1) {
      std::cout << "-1\n";
      return 0;
    }
  }
  u64 ans = 0;
  for (auto i: range(0, N)) {
    if (T[i] == 1) {
      while (one.top() < i) {
        usize x = one.top();
        one.pop();
        usize y = one.top();
        one.pop();
        ans += y - x;
      }
      ans += one.top() - i;
      one.pop();
    }
  }
  while (!one.empty()) {
    usize x = one.top();
    one.pop();
    usize y = one.top();
    one.pop();
    ans += y - x;
  }
  std::cout << ans << '\n';
  return 0;
}

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

int n;
string s, t;
set<int> have;

int nxt(int idx)
{
	auto it = have.upper_bound(idx);
	if(it == have.end()){
		cout<<-1<<'\n';
		exit(0);
	}
	int res = *it;
	have.erase(it);
	s[res] = '0';
	return res;
}

void solve()
{
	int a = 0, b = 0;
	for(int i=0;i<n;++i){
		a+=(s[i]=='1');
		b+=(t[i]=='1');
	}
	if(a%2!=b%2 || a<b){
		cout<<-1<<'\n';
		return;
	}
	for(int i=0;i<n;++i)if(s[i]=='1')have.insert(i);
	ll ans = 0;
	for(int i=0;i<n;++i){
		if(s[i]!=t[i]){
			if(s[i]=='1'){
				ans+=1+(nxt(i)-(i+1));
			}else{
				ans += (nxt(i)-i);
			}
		}
	}
	cout<<ans<<'\n';
}

int main()
{
	cin >> n >> s >> t;
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
	T f=0,x=0;char c=getchar();
	while(!isdigit(c)) f=c=='-',c=getchar();
	while(isdigit(c)) x=x*10+c-48,c=getchar();
	return f?-x:x;
}
#define int long long
namespace run{
	const int N=4e5+9;
	int head[N],nex[N],to[N],cnt;
	inline void add(int u,int v){
		nex[++cnt]=head[u];
		head[u]=cnt,to[cnt]=v;
	}

	int n,m,a[N],b[N],vis[N],tota,totb;
	inline void dfs(int u){
		vis[u]=1,tota+=a[u],totb+=b[u];
		for(int i=head[u];i;i=nex[i])
			if(!vis[to[i]]) dfs(to[i]);
	}
	int main(){
		n=read<int>(),m=read<int>();
		for(int i=1;i<=n;i++) a[i]=read<int>();
		for(int i=1;i<=n;i++) b[i]=read<int>();
		for(int i=1;i<=m;i++){
			int u=read<int>(),v=read<int>();
			add(u,v),add(v,u);
		}

		for(int i=1;i<=n;i++) if(!vis[i]){
			tota=totb=0,dfs(i);
			if(tota!=totb) puts("No"),exit(0);
		}
		puts("Yes");
		return 0;
	}
}
#undef int
int main(){
#ifdef my
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
#endif
	return run::main();
}

#include<bits/stdc++.h>
#define ll  int64_t
#define pp pair<int,int>
#define ppn pair<pp,int>
#define F first
#define S second
#define N 1000005
#define mod 1000000007
#define pi 3.14159265358979323846

using namespace std;

vector<int>graph[N];
int vis[N], vis1[N];
ll a[N],b[N], n, m;

void dfs(int s, int r)
{
    vis[s]=1;
    for(int i=0; i<graph[s].size(); i++)
    {
        int d =graph[s][i];
        if(!vis[d])
        {
            dfs(d,s);
        }
    }

    if(a[s]!=b[s] && r!=-1){
        if(b[s]>a[s]){
                ll k = b[s]-a[s];
                a[s]+=k;
                a[r]-=k;
            }
            else{
                ll k = a[s]-b[s];
                a[s]-=k;
                a[r]+=k;
                //cout<<k<<endl;
            }
    }
}

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

    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i];
    for(int i=1; i<=n; i++)cin>>b[i];
    int u, v;
    for(int i=0; i<m; i++)
    {
        cin>>u>>v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    for(int i=1; i<=n; i++)
    {
        if(!vis[i])
        {
            dfs(i,-1);
        }
    }
    int f = true;
    for(int i=1; i<=n; i++)
    {
        //cout<<a[i]<<" "<<b[i]<<endl;
        if(a[i]!=b[i])
        {
            f = false;
            break;
        }
    }
    cout<<(f?"Yes":"No")<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long n,l,r,mid;
	scanf("%lld",&n),l=1,r=n;
	while(l<r){
		mid=(l+r+1)>>1;
		if(mid+1<=((n+1)<<1)/mid)l=mid;
		else r=mid-1;
	}printf("%lld\n",n+1-r);
	return 0;
}

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

#define For(i,a,b) for(int (i) = (a);(i) < (b);i++)
#define Rep(i,n) For((i),0,(n))
#define All(a) (a).begin(),(a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9

typedef long long ll;
const ll MOD = 1000000007;
const ll MOD_9 = 998244353;

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

int main(){
    int N;
    cin >> N;
    double ans = 0;
    for(int i = 1;i < N;i++){
        ans += (double)1/i;
        //cout << i << sp << E[i] << endl;
    }
    cout << fixed << setprecision(8) <<  N*ans << endl;
    cin >> N;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N; cin >> N;
	V<bool> is(200010);
	int j=0;
	rep(i,N){
		int x; cin >> x; is[x]=true;
		while(is[j])j++;
		cout<<j<<endl;
	}
}

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG

using namespace std;

using ll = long long;
using ld = long double;
const int INF = (INT_MAX >> 1);
const ll LLINF = (LLONG_MAX >> 1);
#define all(x) x.begin(), x.end()

#define rep(i, s, e) for(ll i = s; i < e; ++i)
#define repr(i, s, e) for(ll i = s; i > e; --i)

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

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

    ll a, b, w; cin >> a >> b >> w;
    w *= 1000;
    ll cnt_min = LLINF, cnt_max = -LLINF;

    rep(cnt, 1, w + 1){
        if (a * cnt <= w && w <= b * cnt){
            cnt_max = max(cnt, cnt_max);
            cnt_min = min(cnt, cnt_min);
        }
    }

    if (cnt_max == -LLINF && cnt_min == LLINF) cout << "UNSATISFIABLE" << endl;
    else cout << cnt_min << " " << cnt_max << endl;

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;

vvint g;

P dfs(int v, int p = -1) {
    vint tmp;
    int score = 1, cnt = 1, penalty = 0;
    for (int n_v : g[v]) {
        if (n_v == p) continue;
        auto p = dfs(n_v, v);
        cnt += p.second;
        if (p.first < 0 && p.second % 2 == 0) score += p.first;
        else if (p.first >= 0 && p.second % 2 == 0) penalty += p.first;
        else tmp.push_back(p.first);
    }
    sort(all(tmp));
    int turn = 1;
    rep(i,tmp.size()) {
        if (turn == 1) score += tmp[i];
        else score -= tmp[i];
        turn *= -1;
    }
    if (turn == 1) score += penalty;
    else score -= penalty;
    return P(score, cnt);
}

int main() {
    int n; cin >> n;
    g.resize(n);
    REP(i,n) {
        int p; cin >> p;
        p--;
        g[i].push_back(p);
        g[p].push_back(i);
    }
    auto p = dfs(0);
    cout << (p.second + p.first) / 2 << endl;
}

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

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

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

	FOR(y,20) {
		FOR(x,20) cout<<(char)('A'+rand()%8);
		cout<<endl;
	}
}

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


// clang-format off
#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;

#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;

#define INF 1000000000000000000
#define inf 1000000000
#define MOD  1000000007
// #define MOD 998244353
#define PI 3.1415926535897932385

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }

ld modp(ld b, ll e) {
    ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }

void solve()
{
    // START
    ll n,m;
    cin>>n>>m;
    vi arr(m);
    rep(i,m)cin>>arr[i];
    sort(all(arr));
    vi vec;
    arr.pb(n+1);
    if(arr[0]>1){
        vec.pb(arr[0]-1);
    }
    fr(i,1,m){
        if(arr[i]-arr[i-1]>1)
            vec.pb(arr[i]-arr[i-1]-1);
    }
    ll mn=INF;
    for(auto k:vec)mn=min(mn, k);
    ll ans=0;
    for(auto k:vec){
        ans+=ceil((k*1.0)/mn);
    }
    cout<<ans<<"\n";
    // END
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);   cout<<fixed;
    #ifdef godfather
        // cin.exceptions(cin.failbit);
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    fr(i,1,t)
    {
        solve();
    }
    end_routine();
}

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

int main()
{
    int n,m;
    cin>>n>>m;
    if(m==0){
        cout<<1;
        return 0;
    }
    if(n==m){
        cout<<0;
        return 0;
    }
    vector<int> v;
    for(int i=0;i<m;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    int gap=1e9;
    v.push_back(0);
    v.push_back(n+1);
    int l=v.size();
    sort(v.begin(),v.end());
    for(int i=0;i<l-1;i++){
        if(v[i+1]-v[i]-1==0)
        continue;
        gap=min(gap,v[i+1]-v[i]-1);
    }
    int count=0;
    for(int i=0;i<l-1;i++){
        count+=(v[i+1]-v[i]-1+gap-1)/gap;
    }
    cout<<count;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
const int inf = 0x3f3f3f3f;
int mod = 1000000007;
bool remender(ll a , ll b){return a%b;}
int p[200005],siz[200005];

int get(int x){
	return p[x]=(x==p[x] ? x : get(p[x]));
}

void unio(int u , int v){
	int a = get(u);
	int b=get(v);
	if(a==b)return;
	if(siz[a]>siz[b])swap(a,b);
	p[b]=a;
	siz[a]+=siz[b];
}

void solve(){
	ll n;ll k;
	ll d=0;
	cin>>n>>k;
	bool bb = 0;
	for(int i = 0; i < n; i++){
		ll a,b;
		cin>>a>>b;
		if((a*b)%100LL){
			d+=(a*b)%100LL;
		}
		k-=(a*b)/100LL;
		if(d >= 100LL){
			d%=100LL;
			k--;
		}
		if(k < 0 || (k==0 && d)){
			if(bb==0){
			cout<<i+1<<'\n';
			bb=1;
		}
		}
	}
	if(bb){
		return;
	}
	cout<<-1<<'\n';
}

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

//#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <unistd.h>
#define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL)
#define all(v) (v).begin(),(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

typedef long long Long;
typedef long double Double;
typedef unsigned long long ULong;
typedef pair<Long, Long> Pair;

const int N = 1e6;
const Long INF = 1e18;
const Double EPS = 1e-9;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
Long random(Long a, Long b) { return uniform_int_distribution<Long> (a, b) (rng); }

void Solve(void) {
  Long n, x;
  cin >> n >> x;
  vector<Pair> a(n);
  for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
  Long cur = 0;
  for (int i = 0; i < n; i++) {
    cur += a[i].first * a[i].second;
    if (cur > 100LL * x) {
      cout << i + 1 << '\n';
      return;
    }
  }
  cout << -1 << '\n';
}

int main(void) {
  FAST_IO;
  int t = 1;
  //cin >> t;
  while (t--) Solve();
  return 0;
}


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

int main(){
    cout << fixed << setprecision(15);

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

    long ans1=0;
    for(int i=0;i<n;i++)ans1+=abs(x[i]);
    cout << ans1 << endl;

    long ans2=0;
    for(int i=0;i<n;i++)ans2+=abs(x[i])*(long)abs(x[i]);
    cout << sqrt(ans2) << endl;

    int ans3=0;
    for(int i=0;i<n;i++)ans3=max(ans3,abs(x[i]));
    cout << ans3 << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long int n,sum=0;
   long double sum1=0;
    cin>>n;
    long long int a[n];
    for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i]<0)
    a[i]=-a[i];
    sum=sum+a[i];
    sum1=sum1+a[i]*a[i];
    }
    sort(a,a+n);
    cout<<sum<<endl<<setprecision(16)<<sqrt(sum1)<<endl<<a[n-1];
}


#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9+10;
const ll LINF = 1LL*INF*INF;
const int MAXN = 1e4+10;
const int MAXM = 5e3+10;

priority_queue<int> pq;
vector<vector<int> > graph;
queue<int> que;

int A[MAXN];
int dp[MAXN][110];
int fac[110];

int main()
{
    int n,m,k,a,b,x,y,q;
    int sum = 0;
    int cnt = 0;
    int mx = 0;
    int mn = INF;
    int cur = 0, idx = -1;
    int tc;

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

    cin>>n;

    for(int i=1;i<=n;i++)
    {
        cin>>A[i];
        sum += A[i];
    }

    if(sum&1){
        cout<<0<<"\n";
        return 0;
    }

    dp[0][0] = 1;

    for(int t=1;t<=n;t++){

        cur = A[t];

        for(int i=sum;i>=cur;i--){
            for(int j=t;j>=1;j--){
                dp[i][j] += dp[i-cur][j-1];
                if(dp[i][j]>=PMOD)dp[i][j]-=PMOD;
            }
        }
    }

    fac[0] = 1;

    for(int i=1;i<=n;i++){
        fac[i] = (1LL*i*fac[i-1])%PMOD;
    }

    ll res = 0;
    ll curv;

    for(int i=1;i<n;i++){
        curv = dp[(sum>>1)][i];
        curv = (curv*(ll)fac[i])%PMOD;
        curv = (curv*(ll)fac[n-i])%PMOD;
        res += curv;
        if(res>=PMOD)res-=PMOD;
    }

    cout<<res<<"\n";

    return 0;
}

#include <bits/stdc++.h>

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define size_t unsigned long long
#define ll long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repr(i, a) for (int i = (int)(a)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using namespace std;
int si()
{
  int x;
  scanf("%d", &x);
  return x;
}
long long sl()
{
  long long x;
  scanf("%lld", &x);
  return x;
}
string ss()
{
  string x;
  cin >> x;
  return x;
}
void pi(int x) { printf("%d ", x); }
void pl(long long x) { printf("%lld ", x); }
void pd(double x) { printf("%.15f ", x); }
void ps(const string &s) { printf("%s ", s.c_str()); }
void br() { putchar('\n'); }

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;

struct mint
{
  int n;
  mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b)
{
  a.n += b.n;
  if (a.n >= MOD)
    a.n -= MOD;
  return a;
}
mint operator-(mint a, mint b)
{
  a.n -= b.n;
  if (a.n < 0)
    a.n += MOD;
  return a;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

typedef pair<int, int> P;

const ll N = 1e5 + 5;
string s;
ll n, result, q;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> s >> q;
  vector<int> t(q), a(q), b(q);
  rep(i, q)
  {
    cin >> t[i] >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  vector<string> str(2);
  int ini = 0;
  str[0] = s.substr(0, n);
  str[1] = s.substr(n, n);
  rep(i, q)
  {
    if (t[i] == 2)
      ini = (ini + 1) % 2;
    else
    {
      if (a[i] < n && b[i] < n)
        swap(str[ini][a[i]], str[ini][b[i]]);
      else if (a[i] < n)
        swap(str[ini][a[i]], str[(ini + 1) % 2][b[i] - n]);
      else if (b[i] < n)
        swap(str[ini][b[i]], str[(ini + 1) % 2][a[i] - n]);
      else
        swap(str[(ini + 1) % 2][a[i] - n], str[(ini + 1) % 2][b[i] - n]);
    }
  }
  cout << str[ini] + str[(ini + 1) % 2] << endl;
}