#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

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

int main(){
    int N; long long K; std::cin >> N >> K;
    std::vector<std::vector<long long>> dp(4, std::vector<long long>(3*N + 8));
    dp[0][0] = 1;
    for(int i{0}; i < 3; ++i){
        for(int j{0}, j_len{i*N}; j <= j_len; ++j){
            dp[i+1][j+1] += dp[i][j];
            dp[i+1][j+N+1] -= dp[i][j];
        }
        for(int j{0}, j_len{(i+1) * N}; j < j_len; ++j){
            dp[i+1][j+1] += dp[i+1][j];
        }
    }

    int sm;
    for(int i{3}, i_len{3*N}; i <= i_len; ++i){
        if(K <= dp[3][i]){
            sm = i; break;
        }else{
            K -= dp[3][i];
        }
    }
    for(int i{1}; i <= N; ++i){
        int x{sm - i};
        int jmx{std::min(N, x-1)}, jmn{std::max(x-N, 1)};
        if(jmx < jmn) continue;
        if(K > jmx - jmn + 1){
            K -= jmx - jmn + 1;
            continue;
        }
        int j{int(jmn + K - 1)};
        std::cout << i << " " << j << " " << (x - j) << std::endl;
        return 0;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const ll mod = 1e9 + 7;
const int maxn = 1e5+5;

void go() {
    int n;
    cin>>n;
    string s = "    ";
    cin>>s[0]>>s[1]>>s[2]>>s[3];
    if (n==2) {puts("1");
        return;
    }
    if (s=="AAAA"||s=="AAAB"||s=="AABA"||s=="AABA"||s=="AABB"||s=="ABAB"||s=="ABBB"||s=="BBAB"||s=="BBBB") return puts("1"),void();
    if (s=="ABAA"||s=="BABA"||s=="BABB"||s=="BBAA") {
        int ans = 1;
        for (int i=1; i<=n-3; ++i) ans = ans*2%mod;
        printf("%d\n", ans);
        return;
    }
    int dp[n+5] = {};
    dp[1] = 0, dp[2] = 1;
    for (int i=3; i<=n; ++i) dp[i] = (dp[i-1]+dp[i-2])%mod;
    printf("%d\n", dp[n]);

}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        go();
    }
}


#include<bits/stdc++.h>
using ll = int_fast64_t;
#define REP(i,b,e) for(ll i=b; i<e; i++)

int main(){
	ll n, k;
	scanf("%ld %ld", &n, &k);
	ll ans = 0;
	REP(ab, 2, 3*n){
		ll cd = ab-k;
		if(cd<2) continue;
		ll l1 = std::max(0l, ab-n), r1 = std::min(ab, 0+n);
		ll l2 = std::max(0l, cd-n), r2 = std::min(cd, 0+n);
		ans += std::max(0l, std::min(ab-1, r1-l1+1)) * std::max(0l, std::min(cd-1, r2-l2+1));
	}
	printf("%ld\n", ans);
	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
#define rep(i,n) for(ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const long double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const ll INF = 1e9;
ll gcd(ll a, ll b) {if(a%b==0)return a;else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

int main() {
  ll n,k;
  cin >> n >> k;

  if(k < 0) k = -k;

  vector<ll> f(3*n);
  for(ll i = 2; i <= 2*n; i++) {
    f[i] = min(i-1, 2*n+1-i);
  }
  ll ans = 0;
  for(ll i = 2; i <= 2*n; i++) {
    if(i-k < 2) continue;
    ans += f[i] * f[i-k];
  }

  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define reps(i, s, n) for(int i = (s); i < (n); i++)
#define rrep(i, n) for(int i = (n) - 1; i >= 0; i--)
#define rreps(i, s, n) for(int i = (n) - 1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)

int main(){
    int N;
    cin >> N;
    int minprice = INF;
    rep(i, N){
        int a, p, x;
        cin >> a >> p >> x;
        if(a < x){
            minprice = min(minprice, p);
        }
    }
    if(minprice == INF) minprice = -1;
    cout << minprice << endl;
}

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

using namespace std;
#define ll long long

int main() {
	int a, b, w, min, max, goal, dif;
	bool stop = false;
	cin >> a >> b >> w;
	w *= 1000;
	if (w < a) cout << "UNSATISFIABLE";
	else if (a == b) {
		if (w % a == 0) cout << w / a << ' ' << w / a << '\n';
		else cout << "UNSATISFIABLE\n";
	}
	else {
		dif = b - a;
		goal = w;
		min = goal /b;
		goal %= b;
		if (goal > 0) {
			++min;
			goal = b - goal;
		}

		goal = goal - min * dif;
		if (goal > 0) {
			stop = true;
			cout << "UNSATISFIABLE\n";
		}

		if (!stop) {
			goal = w;
			max = goal / a;
			goal %= a;
			goal = goal - max * dif;
			if (goal > 0) {
				stop = true;
				cout << "UNSATISFIABLE\n";
			}
			if (!stop) cout << min << ' ' << max << '\n';
		}
	}

	return 0;
}


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

int main(){
  int n;
  cin >> n;
  int t[n];
  int st = 0;
  rep(i,n) {
    cin >> t[i];
    st += t[i];
  }
  vector<int> dp(MAX);
  dp[0] = 1;
  rep(i,n) {
    vector<int> dpn(MAX);
    rep(j,MAX) {
      if(dp[j]) dpn[j]++;
      if(j+t[i] < MAX and dp[j]) dpn[j+t[i]]++;
    }
    swap(dp,dpn);
  }
  int ans = MAX;
  rep(i,MAX) {
    if(dp[i]) ans = min(ans,max(i,st-i));
  }
  cout << ans << endl;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 105
#define M 100001
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}
int n,a[N],sum;
bool f[2][200003];
int main()
{
	n=read();
	for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i];
	int x=1,y=0;
	f[x][M+0]=1;
	for(int i=1;i<=n;++i)
	{
		memset(f[y],0,sizeof(f[y]));
		for(register int j=-100000;j<=100000;++j)
		    if(f[x][M+j]) f[y][M+j+a[i]]=f[y][M+j-a[i]]=1;
		swap(x,y);
	}
	for(int i=0;i<=100000;++i)
	    if(f[x][M+i] || f[x][M-i]){printf("%d\n",(sum+i)/2);return 0;}
	return 0;
}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */
#include <bits/stdc++.h>
using namespace std;

const bool ready = [](){
    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(12);
    return true;
}();

using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)

#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }

using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;

#define endl "\n"

using t3=tuple<int,int,int>;

/* dijkstra */
const int INF=1e18;
void solve() {
    cini(n);
    cini(m);
    cini(x); x--;
    cini(y); y--;

    vector<vector<t3>> adj(n);
    for(int i=0; i<m; i++) {
        cini(a); a--;
        cini(b); b--;
        cini(t);
        cini(k);
        adj[a].emplace_back(b,t,k);
        adj[b].emplace_back(a,t,k);
    }

    vi dp(n, INF);
    dp[x]=0;
    priority_queue<pii> q;  /* <-time, vertex> */
    q.emplace(0, x);
    while(q.size()) {
        auto [tt,v]=q.top();
        q.pop();
        if(dp[v]!=-tt)
            continue;

        for(auto [chl,t,k] : adj[v]) {
            int wait=dp[v]%k;
            if(wait==0)
                wait=k;
            wait=k-wait;
            int next=dp[v]+wait+t;
            if(dp[chl]>next) {
                dp[chl]=next;
                q.emplace(-dp[chl], chl);
            }
        }
    }

    if(dp[y]==INF)
        cout<<-1<<endl;
    else
        cout<<dp[y]<<endl;

}

signed main() {
    solve();
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

#define n50 250000

int f(int r,int c){
  return r*500+c;
}

int main(){
  int R,C; cin >> R >> C;
  vvi g(n50);
  vvi co(n50);
  rep(r,R) rep(c,C-1){
    int in; cin >> in;
    int ff=f(r,c);
    g[ff].pb(ff+1);
    g[ff+1].pb(ff);
    co[ff].pb(in);
    co[ff+1].pb(in);
  }
  rep(r,R-1) rep(c,C){
    int in; cin >> in;
    int ff=f(r,c);
    g[ff].pb(ff+500);
    co[ff].pb(in);
  }
  rep(c,C){
    rep(r,R){
      rep(s,r){
        int ff=f(r,c);
        g[ff].pb(f(s,c));
        co[ff].pb(r-s+1);
      }
    }
  }
  vector<int> D(n50, 1e9);
  vector<bool> used(n50, false);
  priority_queue<pair<int,int>,
                 vector<pair<int,int>>,
                 greater<pair<int,int>>> q;
  q.push({0,0});
  while(!q.empty()){
    int d = q.top().first; int to = q.top().second; q.pop();
    if(used[to]) continue;
    used[to] = true; D[to] = d;
    rep(i,sz(g[to])){
      int nxt=g[to][i];
      int nd = D[to] + co[to][i];
      if(D[nxt] <= nd) continue;
      D[nxt] = nd;
      q.push({nd, nxt});
    }
  }

  int ans = D[f(R-1,C-1)];
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop(i, a) for (int i = 0; i < (a); ++i)
#define cont(i, a) for (int i = 1; i <= (a); ++i)
#define circ(i, a, b) for (int i = (a); i <= (b); ++i)
#define range(i, a, b, c) for (int i = (a); (c) > 0 ? i <= (b) : i >= (b); i += (c))
#define parse(it, x) for (auto &it : (x))
#define pub push_back
#define pob pop_back
#define emb emplace_back
#define mak make_pair
#define mkt make_tuple
typedef long long ll;
typedef long double lf;
const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;

int n;
int a[200005], b[200005], p[200005];
int dy[200005];
map<int, int> lss;
vector<int> pos[200005];

struct Fenwick {
	ll dt[1 << 18];
	void inline add(int a, ll x) { while (a <= n) dt[a] += x, a += a & -a; }
	ll inline sum(int a) { ll res = 0; while (a) res += dt[a], a -= a & -a; return res; }
} bit;

int main() {
    scanf("%d", &n);
    cont(i, n) scanf("%d", a + i), a[i] += i, lss[a[i]];
    cont(i, n) scanf("%d", b + i), b[i] += i;
    int lsc = 0;
    parse(it, lss) it.second = ++lsc;
    cont(i, n) {
        a[i] = lss[a[i]];
        if (!lss.count(b[i])) return puts("-1"), 0;
        b[i] = lss[b[i]];
        pos[b[i]].pub(i);
    }
    cont(i, lsc) reverse(all(pos[i]));
    cont(i, n) {
        if (!SZ(pos[a[i]])) return puts("-1"), 0;
        p[i] = pos[a[i]].back();
        pos[a[i]].pob();
    }
    ll ans = 0;
    range(i, n, 1, -1) {
        ans += bit.sum(p[i]);
        bit.add(p[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
// using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

bool isPrime(int N){
    for(int i=2;i*i<=N;i++){
        if(N%i==0){
            return false;
        }
    }
    return true;
}

signed main(){
    int N; cin>>N;
    vec_int a(N-1), b(N-1); rep(i,N-1)cin>>a.at(i)>>b.at(i);
    int Q; cin>>Q;
    vec_int t(Q), e(Q), x(Q);
    rep(i,Q)cin>>t.at(i)>>e.at(i)>>x.at(i);

    vector<vec_int> G(N+1);
    rep(i,N-1){
        G.at(a.at(i)).push_back(b.at(i));
        G.at(b.at(i)).push_back(a.at(i));
    }

    vector<int> node_depth(N+1,-1);

    queue<P>  que;
    que.emplace(1,0);

    while(!que.empty()){
        int node, depth; tie(node, depth) =  que.front(); que.pop();
        node_depth.at(node) = depth;
        for(auto next_node: G.at(node)){
            if(node_depth.at(next_node)>=0)continue;
            que.emplace(next_node, depth+1);
        }
    }

    vec_int score(N+1, 0);
    rep(q, Q){
        int from_node = a.at(e.at(q)-1);
        int to_node = b.at(e.at(q)-1);
        if(t.at(q)==2){
            int temp = from_node;
            from_node = to_node;
            to_node = temp;
        }
        if(node_depth.at(to_node)>node_depth.at(from_node)){
            score.at(1) += x.at(q);
            score.at(to_node) -= x.at(q);
        }else{
            score.at(from_node) += x.at(q);
        }
    }

    vec_int ans(N+1, -1);
    queue<P>  que2;
    que2.emplace(1, 0);
    while(!que2.empty()){
        int node, parent_score; tie(node, parent_score)= que2.front(); que2.pop();
        // cout<<node<<" parent_score:"<<parent_score<<endl;
        int temp = parent_score+score.at(node);
        ans.at(node)  = temp;

        for(auto next_node: G.at(node)){
            if(ans.at(next_node)>=0)continue;
            que2.emplace(next_node, temp);
        }
    }

    rep(i,N){
        cout<<ans.at(i+1)<<endl;
    }

    return 0;
}


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

int main(){
  string s;
  cin>>s;
  reverse(s.begin(),s.end());
  for(int i=0; i<s.size(); i++){
    if(s.at(i) == '6')s.at(i)='9';
    else if(s.at(i) == '9')s.at(i)='6';
  }
  cout<<s<<endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define int long long
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define endl "\n"
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MAX = 2e5 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll power(ll x, ll n) {
    if (!n) {
        return 1;
    }
    return (n & 1 ? mul(x, power(mul(x, x), n / 2)) : power(mul(x, x), n / 2));
}

void solve() {
    string s;
    cin >> s;
    reverse(all(s));
    for (auto ch : s) {
        cout << (ch == '6' ? '9' : ch == '9' ? '6' : ch);
    }
}

signed main() {
    fastio;

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

    while (t--) {
        solve();
    }
    return 0;
}


///****In the name of ALLAH, most Gracious, most Compassionate****//

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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

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

#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl

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

const int N=3e5+5;
const int oo=1e9+7;

vector<pii> g[N];

int ans[N];

int n;

void dfs(int u)
{
    for(pii vc: g[u])
    {
        if(ans[vc.first]==-1)
        {
            if(vc.second == ans[u])
                ans[vc.first] = (vc.second %n)+1;
            else
                ans[vc.first] = vc.second;
            dfs(vc.first);
        }
    }
}

int32_t main()
{
    FastIO;
    int m;
    cin>>n>>m;
    while(m--)
    {
        int u,v,c;
        cin>>u>>v>>c;
        g[u].push_back({v,c});
        g[v].push_back({u,c});
    }

    memset(ans , -1 , sizeof ans) ;

    ans[1] =1;
    dfs(1);
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<"\n";

}

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

#define FOR(i,s,e) for(int i=(s);(i)<(int)(e);++(i))
#define REP(i,e) FOR(i,0,e)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(o) (o).begin(), (o).end()

typedef long long ll;
const int N = 200000;
int n;
int a[N];

class union_find_tree {
  private:
    vector<int> v;
    vector<int> connected_size;
    map<int,int> parent;
    map<int,int> rank;

  public:
    union_find_tree(int m) {
      v.resize(m);
      connected_size.resize(m);
      for (int i=0; i<m; i++) v[i] = i;
      for (int i=0; i<(int)v.size(); i++) {
        parent[v[i]] = v[i];
        rank[v[i]] = 1;
        connected_size[v[i]] = 1;
      }
    }

    union_find_tree(vector<int> _v) {
      v = _v;
      for (int i=0; i<(int)v.size(); i++) {
        parent[v[i]] = v[i];
        rank[v[i]] = 1;
        connected_size[v[i]] = 1;
      }
    }

    ~union_find_tree() {
      v.clear();
      parent.clear();
      rank.clear();
      connected_size.clear();
    }

    int root (int x) {
      return parent[x] == x ? x : parent[x] = root(parent[x]);
    }

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

    void unite(int x, int y) {
      x = root(x);
      y = root(y);

      if (x == y) return;

      if (rank[x] < rank[y]) {
        parent[x] = y;
        connected_size[y] += connected_size[x];
      } else {
        parent[y] = x;
        connected_size[x] += connected_size[y];
        if (rank[x] == rank[y]) rank[x]++;
      }
      return;
    }

    int get_connected_size(int x) {
      return connected_size[x];
    }
};

int main() {
  fscanf(stdin, "%d ", &n);
  REP(i,n) {
    fscanf(stdin, "%d ", a+i);
    a[i]--;
  }
  union_find_tree u(N);
  REP(i,n/2) u.unite(a[i],a[n-1-i]);
  int res = 0;
  REP(i,N) {
    if (u.root(i) != i) continue;
    res += u.get_connected_size(i) - 1;
  }
  fprintf(stdout, "%d\n", res);
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

struct BIT{
    vector<LL> node;
    int size;

    BIT(int n){
        size=n;
        node.resize(size,0);
    }

    void add(int a,int x){
        for(int i=a;i<size;i|=i+1) node[i]+=x;
    }

    LL sum(int a){
        LL res=0;
        for(int i=a-1;i>=0;i=(i&(i+1))-1){
            res+=node[i];
        }
        return res;
    }
};

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

    int h,w,m;
    cin>>h>>w>>m;

    vector<int> x(m),y(m);
    for(int i=0;i<m;i++){
        cin>>x[i]>>y[i];
        x[i]--,y[i]--;
    }

    int clsh=h,clsw=w;
    for(int i=0;i<m;i++){
        if(x[i]==0) clsw=min(clsw,y[i]);
        if(y[i]==0) clsh=min(clsh,x[i]);
    }

    vector<int> rih(clsh,w);
    for(int i=0;i<m;i++){
        if(x[i]<clsh) rih[x[i]]=min(rih[x[i]],y[i]);
    }
    vector<int> riw(clsw,h);
    for(int i=0;i<m;i++){
        if(y[i]<clsw) riw[y[i]]=min(riw[y[i]],x[i]);
    }

    LL res=0;
    for(int i=0;i<clsh;i++) res+=rih[i];
    for(int i=0;i<clsw;i++) res+=riw[i];

    vector<vector<int>> dels(w+1);
    for(int i=0;i<clsh;i++) dels[rih[i]].push_back(i);

    BIT bt(h);
    for(int i=0;i<clsh;i++) bt.add(i,1);
    for(int i=0;i<clsw;i++){
        for(int d:dels[i]) bt.add(d,-1);
        res-=bt.sum(riw[i]);
    }

    cout<<res;

    return 0;
}

#include <bits/stdc++.h>
#define ll long long

using namespace std;
#define MAX 1000000007

int nCrModpDP(int n, int r, int p)
{
    // The array C is going to store last row of
    // pascal triangle at the end. And last entry
    // of last row is nCr
    int C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1; // Top row of Pascal Triangle

    // One by constructs remaining rows of Pascal
    // Triangle from top to bottom
    for (int i = 1; i <= n; i++)
    {
        // Fill entries of current row using previous
        // row values
        for (int j = min(i, r); j > 0; j--)

            // nCj = (n-1)Cj + (n-1)C(j-1);
            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

// Lucas Theorem based function that returns nCr % p
// This function works like decimal to binary conversion
// recursive function.  First we compute last digits of
// n and r in base p, then recur for remaining digits
int nCrModpLucas(int n, int r, int p)
{
   // Base case
   if (r==0)
      return 1;

   // Compute last digits of n and r in base p
   int ni = n%p, ri = r%p;

   // Compute result for last digits computed above, and
   // for remaining digits.  Multiply the two results and
   // compute the result of multiplication in modulo p.
   return (nCrModpLucas(n/p, r/p, p) * // Last digits of n and r
           nCrModpDP(ni, ri, p)) % p;  // Remaining digits
}
int main(){

	ios::sync_with_stdio(0), cin.tie(0);
    ll n,s,d;
    cin>>n>>s>>d;
    ll x,y;
    ll test=0;
    for(int i=0;i<n;i++)
    {
        cin>>x>>y;
        if((x<s)and(y>d))
            test=1;
    }
    if(test==1)
        cout<<"Yes";
    else
        cout<<"No";
    return(0);
}


#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)

#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))

#define MAXN 1000005
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)

#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())

using namespace std;
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) {
	while(L < R) cerr << *L << " \n"[next(L) == R],++L;
}
void kout() {cerr << endl;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {
	cerr << a << " ", kout(e...);
}
#else
#define print(...) 0
#define vprint(...) 0
#endif
int n;
vector<int> a;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	a.assign(n + 1, 0);
	rep(i, 1, n) cin >> a[i];
	int ans = -1, cnt = 0;
	rep(i, 2, 1000) {
		int cur = 0;
		rep(j, 1, n)
			cur += bool(a[j] % i == 0);
		if(cur > cnt) ans = i, cnt = cur;
	}
	cout << ans << "\n";
	return 0;
}

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

int main()
{
  int n;
  cin >> n;
  vector<int> v(n);
  for(auto& a: v){
    cin >> a;
  }

  int ret = 0;
  int maxcount = 0;
  for(int i = 2; i <= 1000; ++i){
    int count = 0;
    for(auto& a : v){
      if(a % i == 0){
		++count;
      }
    }
    if(maxcount < count){
      maxcount = count;
      ret = i;
    }
  }
  cout << ret << endl;
  return 0;
}


//#define localsystem 1

#ifdef localsystem
#include"mytimer.h"

#define mytest 1

#ifdef mytest
#include<fstream>
#endif

#endif

#define min(x, y) ((x < y) ? x : y)
#define max(x, y) ((x > y) ? x : y)

#include<iostream>
#include<string>

using namespace std;

int main() {

#ifdef mytest
	ifstream mycin("Sample.txt");
	ofstream mycout("Test resutl.txt");
	if (!mycin.is_open() || !mycout.is_open()) {
		cout << "File can not be opened";
		return 0;
	}
	cin.rdbuf(mycin.rdbuf());
	cout.rdbuf(mycout.rdbuf());
#endif
#ifdef localsystem
	mytimer time_taken{};
#endif

int t{ 1 };
//cin >> t;

while (t--) {

	string s;
	cin >> s;
	int chc[26] = { 0 };
	char pc = '-';
	long long int ans{};

	for (int i = s.size() - 1; i >= 0; --i) {
		if (pc == s[i]) {
			ans += ((s.size() - i - 2) - chc[s[i] - 'a'] + 1);
			for (int k = 0; k < 26; ++k) chc[k] = 0;
			chc[s[i] - 'a'] = s.size() - i;
		}
		else chc[s[i] - 'a']++;
		pc = s[i];
	}
	cout << ans << "\n";
}

#ifdef localsystem
	time_taken.timetaken();
#endif
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

    while (s.back() == '0')
    {
        s.pop_back();
    }

    string t = s;
    reverse(t.begin(), t.end());

    if (s == t)
        cout << "Yes\n";
    else
        cout << "No\n";
}


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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 2e3 + 10;

int n, m;
int g[maxn][maxn];
ll dp[maxn][maxn];

void add(ll &x, ll y) {
    x %= mod;
    y %= mod;
    x += y;
    x %= mod;
}

ll L[maxn];
ll U[maxn];
ll D[maxn+maxn];

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

    cin>>n>>m;
    for (int i=1; i<=n; i++) {
	string s;
	cin>>s;
	for (int j=1; j<=m; j++) {
	    if (s[j-1]=='#') g[i][j]=1;
	}
    }

    dp[1][1]=1;
    for (int i=1; i<=n; i++) {
	for (int j=1; j<=m; j++) {
	    if (g[i][j] == 0) {
		add(dp[i][j], U[j]);
		add(dp[i][j], L[i]);
		add(dp[i][j], D[i-j+maxn]);

		add(U[j], dp[i][j]);
		add(L[i], dp[i][j]);
		add(D[i-j+maxn], dp[i][j]);

	    } else {
		U[j] = 0;
		L[i] = 0;
		D[i-j+maxn] = 0;
	    }
	}
    }

    ll res = dp[n][m];
    res %= mod;
    res += mod;
    res %= mod;
    out(res);
    return 0;
}

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)

constexpr unsigned MOD = 1000000007;

inline void add(unsigned &x, const unsigned &y) {
    if (x + y >= MOD * 2) x = x + y - MOD * 2;
    else if (x + y >= MOD) x = x + y - MOD;
    else x += y;
}

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

    int H, W;
    cin >> H >> W;

    vector<unsigned> dp(W);
    vector<unsigned> dpu(W);
    vector<unsigned> dpn(W);
    dp[0] = 1;
    while (H--) {
        string S;
        cin >> S;
        vector<unsigned> nxt(W), dpr(W), nxtu(W), nxtn(W);
        REP(j, W) {
            if (S[j] == '#') {
                continue;
            }
            if (j + 1 < W) {
                add(dp[j + 1], dp[j] + dpr[j]);
                add(dpr[j + 1], dp[j] + dpr[j]);
                add(nxt[j + 1], dp[j] + dpn[j]);
                add(nxtn[j + 1], dp[j] + dpn[j]);
            }
            add(nxt[j], dp[j] + dpu[j]);
            add(nxtu[j], dp[j] + dpu[j]);
        }
        if (H == 0) {
            cout << dp.back() << '\n';
            return 0;
        }
        dp = nxt, dpu = nxtu, dpn = nxtn;
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define MOD 1000000007

int main(void) {
  ll k;
  cin >> k;
  string s,t;
  cin >> s >> t;
  ll taka=0,aoki=0;
  vector<int> cnt_taka(10,0),cnt_aoki(10,0);
  rep(i,4) cnt_taka[s[i]-'0']++;
  rep(i,4) cnt_aoki[t[i]-'0']++;
  rep(i,10) taka+=i*pow(10,cnt_taka[i]);
  rep(i,10) aoki+=i*pow(10,cnt_aoki[i]);
  //rep(i,10) printf("cnt_taka[%d]==%d\n",i,cnt_taka[i]);
  //rep(i,10) printf("cnt_aoki[%d]==%d\n",i,cnt_aoki[i]);
  //# patern
  ll tmp_t=taka,tmp_a=aoki;
  ll ans=0;
  rep2(i,1,10) {
    rep2(j,1,10) {
      tmp_t=taka,tmp_a=aoki;
      tmp_t+=i*pow(10,cnt_taka[i])*9;
      tmp_a+=j*pow(10,cnt_aoki[j])*9;
      //taka win
      if(tmp_a<tmp_t) {
        ll a,b;
        a=k-cnt_taka[i]-cnt_aoki[i];
        b=k-cnt_taka[j]-cnt_aoki[j];
        if(i==j) b--;
        if(a>0&&b>0) ans+=a*b;
        //printf("ans=%d i=%d j=%d\n",ans,i,j);
      }
    }
  }
  //cout << ans << endl;
  double res;
  res=(double)ans/((9*k-4*2)*(9*k-4*2-1));
  //cout << res << endl;
  printf("%.8lf\n",res);
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
const int INF = 1001001001;
const int BASE = 10000;

int main() {
  int n;
  cin >> n;
  vector<tuple<int, int, int>> input(n);
  map<tuple<int, int>, int> mp;
  rep(i, n) {
    int x, y, c;
    cin >> x >> y >> c;
    input.at(i) = {x, y, c};
    mp[{x, y}] = i;
  }

  // FILE *fp;
  // fp = fopen("out.txt", "w");

  vector<tuple<int, int, int, int>> ans(n);
  set<tuple<int, int, int, int>> se;
  int co = 0;
  rrep(bh, BASE)rrep(bw, BASE) {
    if(mp.find({bw, bh}) == mp.end())  continue;
    int idx = mp[{bw, bh}];
    auto [sx, sy, c] = input.at(idx);
    int ex = BASE, ey = BASE;
    set<pair<int, int>> pse;
    for(auto [px, py, pw, ph] : se) {
      int pex = px + pw, pey = py + ph;
      if(sx < pex && sy < py){
        if(px <= sx) ey = min(ey, py);
        else pse.insert({px, py});
      }
      if(sy < pey && sx < px){
        if(py <= sy) ex = min(ex, px);
        else pse.insert({px, py});
      }
    }
    int mnabs = INF;
    int rh = 1, rw = 1;
    if(idx % 2 == 0) {
      for(int h = 1; sy + h <= ey; h++) {
        for(int w = 1; sx + w <= ex; w++) {
          if(pse.find({sx+w, sy+h}) != pse.end()) {
            ex = sx + w;
          }
          int s = h*w;
          if(abs(c - s) < mnabs) {
            mnabs = abs(c - s);
            rh = h, rw = w;
          }
        }
      }
    } else {
      for(int w = 1; sx + w <= ex; w++) {
        for(int h = 1; sy + h <= ey; h++) {
          if(pse.find({sx+w, sy+h}) != pse.end()) {
            ey = sy + h;
          }
          int s = h*w;
          if(abs(c - s) < mnabs) {
            mnabs = abs(c - s);
            rh = h, rw = w;
          }
        }
      }
    }
    se.insert({sx, sy, rw, rh});
    ans.at(idx) = {sx, sy, sx+rw, sy+rh};
  }

  for(auto [a, b, c, d] : ans) {
    // fprintf(fp, "%d %d %d %d\n", a, b, c, d);
    cout << a << " " << b << " " << c << " " << d << endl;
  }
  return 0;
}


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

int main() {
    int N; cin>>N;
    ll M = 10000000000;
    string T; cin>>T;
    if(T=="1"){
        cout<<2*M<<endl;
        return 0;
    }else if(T=="11"){
        cout<<M<<endl;
        return 0;
    }

    int cnt = 0;
    int cur = 0;
    if(T[0]=='0'){
        cnt++;
        cur = 1;
    }else if(N>=2 && T.substr(0,2)=="10"){
        cnt++;
        cur = 2;
    }else if(N>=3 && T.substr(0,3)=="110"){
        ;
    }else{
        cout<<0<<endl;
        return 0;
    }

    for (; cur+2 < N; cur+=3){
        if(T.substr(cur,3)=="110")cnt++;
        else{
            cout<<0<<endl;
            return 0;
        }
    }

    if(cur<N){
        if(T.substr(cur)=="1" || T.substr(cur)=="11")cnt++;
        else{
            cout<<0<<endl;
            return 0;
        }
    }

    ll ans = M - cnt + 1;
    cout<<ans<<endl;

}

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    string T;
    cin >> T;
    string S;
    while(S.length() < T.length()) {
        S += "110";
    }

    ll ans = 0;
    for(size_t i=0; i+T.size() <= S.size(); i++) {
        bool ok = true;
        for(size_t j=0; j < T.size(); j++) {
            if(S[i+j] != T[j]) ok = false;
            // cout << i+j << ' ' << j << ' ' << S[i+j] << ' ' << T[j] << '\n';
        }
        if(ok) ans++;
    }
    if(ans == 0) {
        S += "110";
        for(size_t i=0; i+T.size() <= S.size(); i++) {
            bool ok = true;
            for(size_t j=0; j < T.size(); j++) {
                if(S[i+j] != T[j]) ok = false;
                // cout << i+j << ' ' << j << ' ' << S[i+j] << ' ' << T[j] << '\n';
            }
            if(ok) ans++;
        }
    }
    // cout << ans << '\n';
    ans *= 10000000000LL - S.size() / 3 + 1;
    // ans *= 5 - S.size() / 3 + 1;
    cout << ans << endl;

    return 0;
}


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

int main()
{
    ll x; cin >> x;
    cout << 100 - (x%100) << endl;
    return 0;
}

#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
using ll = long long;
const int MOD = 1000000007;
const int INF = 2147483647;

int main() {
    FAST;

    int i, s = 0, l = 0, ans = 0;
    ll m;
    cin >> m;

    vector<int> d(3,0);
    while (m) {
        int t = (m % 10) % 3;

        ++l;
        s += t;
        ++d[t];

        m /= 10;
    }

    ans = s % 3;
    if (ans == 1) {
        if ((l > 1) && (d[1] >= 1)) {}
            else if ((l > 2) && (d[2] >= 2)) { ans = 2; }
            else ans = -1;
    } else if (ans == 2) {
        if ((l > 1) && (d[2] >= 1)) { ans = 1; }
            else if ((l > 2) && (d[1] >= 2)) {}
            else ans = -1;
    }

    cout << ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

	vector <int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};

	vector <ll> dp(1 << 20, 0);
	dp[0] = 1;
	for(ll x = a; x <= b; x++) {
		int curr_mask = 0;
		for(int i = 0; i < 20; i++) {
			if(x % primes[i] == 0) curr_mask |= (1 << i);
		}

		for(int mask = 0; mask < (1 << 20); mask++) {
			if(mask & curr_mask) continue;
			dp[mask ^ curr_mask] += dp[mask];
		}
	}
	cout << accumulate(dp.begin(), dp.end(), 0LL);
}

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

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}
const int maxn = 73;
const int maxp = 21;
vector<int> primos;
vector<pair<ll,int>> C;
ll A,B,dp[2][(1<<maxp)];

//ll dp(int pos,int mask){
	//if(pos >= (int)C.size())return 1;
	//ll &res = memo[pos][mask];
	//if(res != -1)return res;
	//res = dp(pos+1,mask);
	//if(!(mask&C[pos].se)){
		//res += dp(pos+1,mask|C[pos].se);
	//}
	//return res;
//}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	forn(i,2,maxn){
		bool primo = true;
		forn(j,2,i)if(i%j == 0)primo = false;
		if(primo)primos.pb(i);
	}
	cin >> A >> B;
	for(ll i = A; i <= B; i++){
		int mask = 0;
		forn(j,0,(int)primos.size()){
			ll p = primos[j];
			if(i%p == 0)mask |= (1<<j);
		}
		C.pb({i,mask});
	}
	//debug(primos);
	//debug(C);
	forn(mask,0,(1<<maxp))dp[1][mask] = 1;
	forr(pos,(int)C.size()-1,0){
		forn(mask,0,(1<<maxp)){
			dp[0][mask] += dp[1][mask];
			if(!(mask&C[pos].se)){
				dp[0][mask] += dp[1][mask|C[pos].se];
			}
		}
		forn(mask,0,(1<<maxp)){
			dp[1][mask] = dp[0][mask];
			dp[0][mask] = 0;
		}
	}
	ll res = dp[1][0];
	//debug(dp[1][0],dp[1][C[0].se]);
	//forn(mask,0,(1<<maxp))res += dp[1][mask];
	cout << res << '\n';

	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


#include <bits/stdc++.h>

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

using namespace std;

using ll = long long int;

const ll MOD_NUM = 998244353;

// 二分累乗法
ll pow_mod(ll n, ll p, ll m) {
    const int N = 64;

    vector<ll> memory(N);
    memory[0] = n % m;
    rep(i, N) memory[i + 1] = memory[i] * memory[i] % m;

    ll ans = 1;
    rep(i, N) {
        ll k = p >> i;
        if (k == 0)
            break;
        else if (k & 1)
            ans = (ans * memory[i]) % m;
    }

    return ans;
}

int main() {
    // Input
    int N;
    cin >> N;
    map<int, int> f;
    rep(i, N) cin >> f[i + 1];

    // Process
    vector<bool> watched(N + 1, false);
    int watched_num = 0;

    int group_num = 0;
    int start = 1;
    while (watched_num < N) {
        while (watched[start]) start++;

        vector<int> watch_nums;
        int current;
        for (current = start; !watched[current]; current = f[current]) {
            watch_nums.emplace_back(current);

            watched[current] = true;
            watched_num++;
        }

        bool current_watch = false;
        for (int n : watch_nums) {
            if (n == current) current_watch = true;
        }
        if (current_watch) group_num++;
    }

    ll ans = pow_mod(2, group_num, MOD_NUM);
    ans = (ans + MOD_NUM - 1) % MOD_NUM;

    // Output
    cout << ans << endl;

    return 0;
}

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

int main()
{
    const int M = 200;

    int N;
    cin >> N;

    vector<int> A(N);
    for (int& a : A) { cin >> a; }

    vector<vector<int>> rest(M);
    vector<int> ans1, ans2;
    bool flag = false;
    for (int i = 0; i < N; ++i)
    {
        int r = A[i] % M;
        if (rest[r].empty()) { rest[r].push_back(i); }
        else
        {
            ans1 = rest[r];
            ans2.push_back(i);
            flag = true;
            break;
        }

        for (int m = 0; m < M; ++m)
        {
            if (rest[m].empty()) { continue; }

            auto it = find(rest[m].begin(), rest[m].end(), i);
            if (it != rest[m].end()) { continue; }

            vector<int> temp(rest[m]);
            temp.push_back(i);

            int j = (m + r) % M;
            if (rest[j].empty()) { rest[j] = temp; }
            else
            {
                ans1 = rest[j];
                ans2 = temp;
                flag = true;
                break;
            }
        }

        if (flag) { break; }
    }

    if (flag)
    {
        cout << "Yes" << endl;
        cout << ans1.size();
        for (int i : ans1) { cout << " " << (i + 1); }
        cout << endl;
        cout << ans2.size();
        for (int i : ans2) { cout << " " << (i + 1); }
        cout << endl;
    }
    else
    {
        cout << "No" << endl;
    }

    return 0;
}


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

const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int INF = 2e9;
const long long inf = 1LL<<62;

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<998244353> mint;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  mint ans = 0;
  for (int i=0; i<n; i++) {
    cin >> a[i];
    ans += mint(a[i])*mint(a[i]);
  }
  sort(a.begin(), a.end());
  vector<mint> acc(n+1, 0);
  for (int i=0; i<n; i++) {
    acc[i+1] = mint(2)*acc[i] + mint(a[i]);
  }
  for (int i=1; i<n; i++) {
    ans += mint(a[i])*acc[i];
  }
  cout << ans << endl;
}

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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef pair<ld,ld> state;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int MOD = 998244353;
	int add(int a, int b)
	{
		a+=b;
		while(a>=MOD) a-=MOD;
		return a;
	}
	void radd(int &a, int b)
	{
		a=add(a,b);
	}
	int mult(int a, int b)
	{
		return (a*1LL*b)%MOD;
	}
	void rmult(int &a, int b)
	{
		a=mult(a,b);
	}

int a[222222];
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
	int cur=a[0];
	int ans=0;
	for(int i=1;i<n;i++)
	{
		radd(ans,mult(a[i],cur));
		rmult(cur,2);
		radd(cur,a[i]);
	}
	for(int i=0;i<n;i++)
	{
		radd(ans,mult(a[i],a[i]));
	}
	cout<<ans<<'\n';
}


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

#define int long long
#define ll long long
#define ld long double
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(x) (int)(x.size())

const int modn = 998244353;

inline int add(int a, const int& b) {
	// ensure both arguments are in the range [0, modn)
	a += b;
	if(a >= modn) {
		a -= modn;
	}
	return a;
 }

inline void adds(int& a, const int& b) {
   	a += b;
 	if(a >= modn) {
 		a -= modn;
 	}
}

inline int mul(const int& a, const int& b) {
	return ((long long) a * b) % modn;
}

inline void muls(int& a, const int& b) {
	a = ((long long) a * b) % modn;
}

template<typename... Args>
inline int add(int a, Args... args) {
	return add(a, add(args...));
}

template<typename... Args>
inline int mul(int a, Args... args) {
	return mul(a, mul(args...));
}

inline int addm(const int& MOD, int a, const int& b) {
	a += b;
	if(a >= MOD) {
		a -= MOD;
	}
	return a;
}

inline int mulm(const int& MOD, const int& a, const int& b) {
	return ((long long) a * b) % MOD;
}

template<typename... Args>
inline int addm(const int& MOD, int a, Args... args) {
	return addm(MOD, a, addm(MOD, args...));
}

template<typename... Args>
inline int mulm(const int& MOD, int a, Args... args) {
	return mulm(MOD, a, mulm(MOD, args...));
}

int po(int a, int b) {
	int res = 1;
	while(b > 0) {
		if(b & 1) {
			muls(res, a);
		}
		muls(a, a);
		b >>= 1;
	}
	return res;
}

int pom(const int& MOD, int a, int b) {
	int res = 1;
	while(b > 0) {
		if(b & 1) {
			res = mulm(MOD, res, a);
		}
		a = mulm(MOD, a, a);
		b >>= 1;
	}
	return res;
}

inline int inv(int a) {
	return po(a, modn - 2);
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // code here
    int n, m, k;
    cin >> n >> m >> k;
    if(n == 1) {
        cout << po(k, m) << '\n';
        return 0;
    }
    if(m == 1) {
        cout << po(k, n) << '\n';
        return 0;
    }
    int ans = 0;
    for(int x = 1; x <= k; x++) {
        int cur = mul(po(k - x + 1, m), add(po(x, n), modn - po(x - 1, n)));
        adds(ans, cur);
    }
    cout << ans << '\n';
    return 0;
}

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

int main(){
    long long i,j,k,l,m,n,a,b,c;
    cin>>a>>b>>c;
    if (c&1) {
        if (a>b) {
            cout<<'>';
        }
        else if (a<b) {
            cout<<'<';
        }
        else {
            cout<<'=';
        }
    }
    else {
        a=abs(a);
        b=abs(b);
        if (a>b) {
            cout<<'>';
        }
        else if (a<b) {
            cout<<'<';
        }
        else {
            cout<<'=';
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,n) for(ll i=0;i<n;i++)
#define loopn(i,j,n) for(ll i=j;i<n;i++)
#define m_arr(arr, n) vector<ll>arr(n)
#define m_arre(arr) vector<ll>arr
#define m_set(sat) set<ll>sat
#define ppn(name) pair<ll,ll>name
#define pp pair<ll,ll>
#define sort_all(arr) sort(arr.begin(), arr.end())
#define pi 3.141592653589793238
//#define mod 1000000007

ll gcd(ll a, ll b){if(a==0)return b;if(b==0)return a;return gcd(b%a, a);}

ll power(ll a, ll b, ll mod)
{
    ll res=1;
    while(b>0)
    {
        if(b%2!=0)
            res = (res*1ll*a)%(mod*1ll*mod);
        b/=2;
        a=(a*1ll*a)%(mod*1ll*mod);
    }

    return res;
}

void solve()
{
    ll x, y, a, b;
    cin>>x>>y>>a>>b;

    if(x>a)
    {
        swap(x, a);
        swap(y, b);
    }

    double ans = (a*y + b*x)/double(y+b);
    cout<<fixed<<setprecision(9)<<ans<<endl;
    return;
}

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

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

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

    long double X, Y, Z;
    cin >> X >> Y >> Z;
    long double iti = Y / X;
    for(int i = 1; i <= 10000000; i++){
        if(Z*iti <= (long double)(i)){
            cout << i-1 << '\n';
            exit(0);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
//constexpr ll MOD = 1e9 + 7;
constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)

int main()
{
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
  {
    cin >> A.at(i);
  }

  vector<ll> S(N + 1, 0), M(N + 1, 0);
  for (ll i = 0; i < N; i++)
  {
    S.at(i + 1) = S.at(i) + A.at(i);
    M.at(i + 1) = max(M.at(i), A.at(i));
  }
  vector<ll> SS(N + 1, 0);
  for (ll i = 0; i < N; i++)
  {
    SS.at(i + 1) = SS.at(i) + S.at(i);
  }

  for (ll k = 0; k < N; k++)
  {
    ll ans = S.at(k + 1) + (k + 1) * M.at(k + 1) + SS.at(k + 1);
    cout << ans << '\n';
  }
}

#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
using ll = long long;
using namespace std;
ll N;

int main(int argc, char *argv[]) {
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 1LL << 60;
  for (ll i = 0; i < (1 << (N - 1)); i++) {
    ll tmp = 0;
    ll total = 0;
    for (ll j = 0; j < N; j++) {
      tmp |= A[j];
      if ((1 << j) & i) {
        total ^= tmp;
        tmp = 0;
      }
    }
    total ^= tmp;
    ans = min(ans, total);
  }
  cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
  int a,b;
  ll k;
  cin >> a >> b >> k;

  int n = a+b;
  string s(n, 'a');

  ll f1,f2,f3;
  for(int i=0;i<n;i++){
    ll sum = 1;
    ll j = 1, l = 1, m = 1;

    if(b == 0){
      s[i] = 'a';
      continue;
    }else if(a == 0){
      s[i] = 'b';
      continue;
    }

    while(1){
      if(j <= a+b-1){
        sum *= j;
        j++;
      }
      if(l <= a-1){
        if(sum % l == 0){
          sum /= l;
          l++;
        }
      }
      if(m <= b){
        if(sum % m == 0){
          sum /= m;
          m++;
        }
      }

      if(j > a+b-1 && l > a-1 && m > b) break;
    }

    if(k > sum){
      s[i] = 'b';
      k -= sum;
      b--;
    }else{
      s[i] = 'a';
      a--;
    }

  }

  cout << s << endl;
}

#include <bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define dl double

using namespace std;

const int N = 1e6 + 7;
const long long mod = 1e9 + 7;

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

int L,R;
int p[N];
vector<int> v[N];

void solve()
{
       cin >> L >> R;
       for(int i = 2; i < N; i++){
                if(!p[i]){
                        for(int j = i; j < N; j += i){
                                v[j].push_back(i);
                                p[j] = 1;
                        }
                }
       }
       ll ans = 0;
       vector<int> g((1 << 10), 1);
       for(int i = L; i <= R; i++){
                if(i > 1){
                        ans += R / i * 2;
                        ans--;
                }
                int l = (int)v[i].size();
                ll res = 0;
                g[0] = 1;
                for(int j = 0; j < l; j++){
                        for(int h = 0; h < (1 << j); h++){
                                g[(1 << j) + h] = g[h] * v[i][j];
                        }
                }
                for(int j = 1; j < (1 << l); j++){
                        if(__builtin_popcount(j) % 2)res += R / g[j] - (L - 1) / g[j];
                        else res -= R / g[j] - (L - 1) / g[j];
                }
                res = R - L + 1 - res;
                ans += res;
       }
       ll l = R - L + 1;
       cout << l * l - ans;
}

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

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


#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;
template <typename T> using vt = vector<T>;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define SZ(x) ((int)((x).size()))
#define EACH(x, a) for (auto& x: a)
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using ll = long long;
using pll = pair<ll, ll>;

mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(mt_rng);
}

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

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

template<class ForwardIterator> void read(ForwardIterator first,
		ForwardIterator last) {
	while (first != last) {
		cin >> (*first);
		++first;
	}
}

template<class T> void read(vector<T> &v) {
	read(v.begin(),v.end());
}

template<class ForwardIterator> void print(ForwardIterator first,
		ForwardIterator last, string sep = " ", string end = "\n") {
	bool ft = true;
	while (first != last) {
		if (!ft) {cout << sep;} else {ft = false;}
		cout << (*first);
		++first;
	}
	cout << end;
}

template<typename T> void print(vector<T> &v, string sep = " ", string end = "\n") {
	print(v.begin(),v.end(),sep,end);
}

template<typename T1, typename T2>
std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) {
	return o << p.F << " " << p.S;
}

template<typename T>
vector<int> argsort(const vector<T> &v, function<bool(T,T)> cmp) {
    vector<int> indices(v.size());
    iota(indices.begin(), indices.end(), 0);
    sort(indices.begin(), indices.end(),[&v,&cmp](int left, int right) -> bool {return cmp(v[left],v[right]);});
    return indices;
}

template<typename T>
vector<int> argsort(const vector<T> &v) {
    return argsort<T>(v, less<T>());
}

template<typename T, typename T2>
vector<int> argsort(const vector<T> &v, function<T2(T)> key) {
	vector<T2> v2(v.size());
	for(int i = 0; i < SZ(v); i++) v2[i]=key(v[i]);
    return argsort<T2>(v2);
}

inline ll floorDiv(ll x, ll y) {
	ll d = x / y;
	ll r = x % y;
	return r ? (d - ((x < 0) ^ (y < 0))) : d;
}

ll ceilDiv(ll x, ll y) {
	return -floorDiv(-x, y);
}

ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) {
	int sign = lo <= hi ? 1 : -1;
	lo *= sign;
	hi *= sign;
	hi++;
	while (lo < hi) {
		ll mid = lo + floorDiv(hi-lo, 2);
		if (!predicate(sign * mid)) {
			lo = mid + 1;
		} else {
			hi = mid;
		}
	}
	return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 200005;

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

	cout << fixed << setprecision(8);

	double a,b;
	cin >> a >> b;
	cout << (b*a/100) << "\n";

}

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

int main()
{
  int h, w, mini = -1, sum = 0;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w, 0));
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      cin >> a.at(i).at(j);
      if (mini == -1)
      {
        mini = a.at(i).at(j);
      }
      else
      {
        mini = min(mini, a.at(i).at(j));
      }
    }
  }
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      sum += a.at(i).at(j) - mini;
    }
  }
  cout << sum << endl;
}


#include<bits/stdc++.h>
using namespace std;
int N;
int a[300005];
map<long long,int>mp;
long long cnt,ans=0;
int main()
{
	cin>>N;
	for(int i=1;i<=N;i++)
	{cin>>a[i];
	mp[a[i]]++;
	}
	if(mp[a[1]]==N){cout<<"0";return 0;}//�״���
	for(auto &v:mp)ans+=(long long)v.second*(v.second-1)/2;//v.secondΪ���ͣ���ǿ��
	cnt=(long long)N*(N-1)/2-ans;//NΪ���ͣ���Ҫǿ��תΪlonglong
	cout<<cnt;
	return 0;
}

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

#define N               100004
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<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 mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

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

void c_p_c()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

}

int32_t main()
{
  c_p_c();
  int n; cin >> n;
  int *arr = new int[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  map<int, vector<int>>m;
  for (int i = 1; i <= n; i++)
    m[arr[i]].pb(i);
  int ans = 0;
  for (int i = 1; i <= n; i++)
  {
    int ind = m[arr[i]].end() - upper_bound(m[arr[i]].begin(), m[arr[i]].end(), i);

    ans += (n - i) - ind;
    //cout << ans << endl;
  }
  cout << ans << '\n';

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define in inline
#define get getchar()
#define ll long long
int main()
{
	ll n;
	cin>>n;
	if(n<=2) cout<<1<<endl;
	else
	{
		ll l=2,r=sqrt(2*n),mid,ans;
		while(l<=r)
		{
			mid=l+r>>1;
			if((2+mid)*(mid-1)/2<=n) ans=mid, l=mid+1;
			else r=mid-1;
		}
		cout<<2+((n-1)-ans)<<endl;
	}
}

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

#define ll long long int
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

int main(){
    FIO;
    ll t,n,k,i,j,ans=0;
    string s;
    cin >> s;

    int a[26]={0};
    t=0;

    n=s.size();

    for(i=n-2;i>=0;i--){
        if(s[i]==s[i+1]){
            ans+=t-a[s[i]-'a'];
            for(j=0;j<26;j++)
                if(j!=(s[i]-'a')){
                    a[s[i]-'a']+=a[j];
                    a[j]=0;
                }
        }
        a[s[i+1]-'a']++;
        t++;
    }

    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

long long extGcd(long long a, long long b, long long &p, long long &q) {
    if (b == 0) { p = 1; q = 0; return a; }
    long long d = extGcd(b, a%b, q, p);
    q -= a/b * p;
    return d;
}

pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
  long long p, q;
  long long d = extGcd(m1, m2, p, q);
  if ((b2 - b1) % d != 0) return make_pair(0, -1);
  long long m = m1 * (m2/d);
  long long tmp = (b2 - b1) / d * p % (m2/d);
  long long r = ((b1 + m1 * tmp) % m + m) % m;
  return make_pair(r, m);
}

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

    ll t; cin >> t;
    while(t--){
        ll x, y, p, q; cin >> x >> y >> p >> q;

        ll ans = -1;
        zep(i, x, x + y){
            zep(j, p, p + q){
                auto res = ChineseRem(i, 2 * (x + y), j, p + q);
                if(res.second != -1 && (ans == -1 || ans > res.first)){
                    ans = res.first;
                }
            }
        }
        if(ans == -1){
            print("infinity")
        }else{
            print(ans)
        }
    }

    return 0;
}

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

template<class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(const int &from = -1, const int &to = -1, const Cost &cost = 1)
        : from(from), to(to), cost(cost) {}

    bool operator<(const Edge<Cost> &rhs) const { return this->cost < rhs.cost; }
    bool operator>(const Edge<Cost> &rhs) const { return this->cost > rhs.cost; }
};

template<class Cost = int>
struct Graph : vector<vector<Edge<Cost>>> {
    int n;
    Graph(int _n = 0) : n(_n) { this->resize(n); }

    void add_edge(const int &src, const int &dst, const Cost &cost = 1) {
        assert(0 <= src && src < n && 0 <= dst && dst < n);
        (*this)[src].emplace_back(src, dst, cost);
    }
    void add_biedge(const int &src, const int &dst, const Cost &cost = 1) {
        add_edge(src, dst, cost), add_edge(dst, src, cost);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N, M, X, Y; cin >> N >> M >> X >> Y;
    using Info = pair<long long, long long>;
    Graph<Info> G(N+1);
    for (int i = 0; i < M; i++) {
        int a, b, t, k;
        cin >> a >> b >> t >> k;
        G.add_biedge(a, b, Info(t, k));
    }

    set<pair<long long, int>> S;
    vector<long long> T(N+1, 1e18);
    T[X] = 0;
    S.emplace(0, X);
    while (!S.empty()) {
        auto u = *S.begin();
        int C = u.second;
        long long now = u.first;
        S.erase(S.begin());
        if (T[C] < now) continue;
        for (auto &e: G[C]) {
            auto [t, k] = e.cost;
            long long nx = (now + k - 1) / k * k + t;
            if (T[e.to] > nx) {
                T[e.to] = nx;
                S.emplace(T[e.to], e.to);
            }
        }
    }
    if (T[Y] == 1e18) cout << -1 << "\n";
    else cout << T[Y] << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
using Edge = pair<int, ll>;
const ll INF = 1LL << 60;
const int MAX = 200000;
const int MOD = 1000000007;

int main()
{
    // cin高速化
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, a, b;
    cin >> n >> a >> b;
    cout << n - a + b << endl;
}

#include <bits/stdc++.h>
using namespace std;
signed main(void){
        int n,a,b;
        scanf("%d%d%d",&n,&a,&b);
        printf("%d",n-a+b);
}


#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){a = b; return true;}
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){a = b; return true;}
    return false;
}
template<class T, class S> inline bool exist(std::set<T> &s, const S &e){
    return (s.find(e) != std::end(s));
}

int main(){
    int L, R; std::cin >> L >> R;

    long long ans{};
    std::vector<long long> cnt(R+1, 0);
    for(int g{R}; g >= 2; --g){
        long long k{R/g - (L-1)/g}, c{k * k};
        for(int i{2*g}; i <= R; i += g){
            c -= cnt[i];
        }
        cnt[g] = c;
        if(std::max(2, L) <= g && g <= R){
            c -= 2 * k - 1;
        }
        ans += c;
    }
    std::cout << ans << std::endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

int main(void){

  string str;
  int num;
  int i,j,k;
  int a,b;
  int ans=0;
  int flag =0;

  cin >> a >> b;

  for(;;){

      a = a-b;
      if(a < 0){
          break;
      }

      ans++;
  }

  cout << ans << endl;

  return 0;
}


#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M; cin >> N >> M;
  vv(vi) es(N, vvi(26));
  F0R(i, M) {
    int a, b; char c; cin >> a >> b >> c;
    a--; b--;
    es[a][c-'a'].push_back(b);
    es[b][c - 'a'].push_back(a);
  }
  //
  int inf = INT32_MAX / 2;
  vvi dp(N, vi(N, inf));
  dp[0][N - 1] = 0;
  queue<pii> qu;
  qu.emplace(0, N - 1);
  int step = 0;
  while (sz(qu)) {
    queue<pii> nq;
    while (sz(qu)) {
      auto [u, v] = qu.front(); qu.pop();
      if (dp[u][v] < step) continue;
      //cout << (u + 1) << '-' << (v + 1) << newl;
      F0R(c, 26) {
        for (auto to : es[u][c]) {
          for (auto to2 : es[v][c]) {
            if (dp[to][to2] <= step + 1) continue;
            dp[to][to2] = step + 1;
            nq.emplace(to, to2);
          }
        }
      }
    }
    qu = move(nq);
    step++;
  }
  cout << (dp[N - 1][0] >= inf ? -1 : dp[N- 1][0]) << newl;
}
#endif

/// Containers Start
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
/// C Header Files
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <math.h>
#include <stdio.h>
/// Containers End

using namespace std;

/// Math Start
#define PI acos(-1.0)
#define Pi 3.141592653589793
#define EPS (1e-7)
#define INF (0x3f3f3f3f)
/// Math End

/// Extra Start
#define nn '\n'
#define pb push_back
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define sz(a) int((a).size())
#define space " "
#define All(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
/// Extra End

#define rep(i, n) for (int i = 0; i < n; i++)
#define Cini(a)                                                                \
    int a;                                                                     \
    cin >> a;
#define Cinii(a, b)                                                            \
    int a, b;                                                                  \
    cin >> a >> b;
#define Ciniii(a, b, c)                                                        \
    int a, b, c;                                                               \
    cin >> a >> b >> c;
#define Cins(s)                                                                \
    string s;                                                                  \
    cin >> s;
#define Cinss(s1, s2)                                                          \
    string s1, s2;                                                             \
    cin >> s1 >> s2;
#define Cinc(c)                                                                \
    char c;                                                                    \
    cin >> c;
/// Functions End

/// Debug Start
#define deb(x) cout << #x << ": " << x << endl
#define deb2(x, y) cout << #x << ": " << x << '\t' << #y << ": " << y << endl;
#define deb3(x, y, z)                                                          \
    cout << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": "   \
         << z << endl;
/// Debug End

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

/**>>>>>>>>>>>>>>>>>>> END <<<<<<<<<<<<<<<<<<**/

/// template

signed main(void) {
    cin.sync_with_stdio(false);
    cin.tie(0);
    Cini(N);
    vector<int> A(N,0);
    rep(i,N) cin >> A[i];
    stack<int> st;  // <高さ, index>
    st.push(-1);
    int maxAmount = 0;
    for(int i=0;i<N;i++){
        auto top = st.top();
        while(st.top() != -1 && A[st.top()]>=A[i]){
            top = st.top();
            st.pop();
            maxAmount = max(maxAmount, (i - st.top()-1) * A[top]);
        }
        st.push(i);
    }
    while(st.top()!=-1){
        auto top = st.top();
        st.pop();
        maxAmount = max(maxAmount, (N - st.top()-1) * A[top]);
    }
    cout << maxAmount << nn;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
#define V(a) vector
#define P(a,b) pair
#define A(a) set
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define in insert
#define bp __builtin_popcount
#define ppb pop_back
#define FOR(a,b,c) for(ll(a) = b; a < c; a++)
#define FORN(a,b,c) for(ll(a) = b; a <= c; a++)
#define FOREV(a,b,c) for(ll(a)=b; a > c; a--)
#define FOREVN(a,b,c) for(ll(a)=b; a >= c; a--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;

bool cprime(int x)
{
    if (x == 1)
        return false;
    if (x <= 3)
        return true;
    if (x % 2 == 0 || x % 3 == 0)
        return false;
    for (int i = 5; i <= sqrt(x); i += 6)
        if (x % i == 0 || x % (i + 2) == 0)
            return false;
    return true;
}

bool cpalin(string s)
{
    int n = s.length(), l;
    if (n == 1)
        return true;
    if (n % 2 == 0)
        l = n / 2 - 1;
    else
        l = (n - 1) / 2 - 1;
    for (int i = 0; i <= l; i++)
        if (s[i] != s[n - i - 1])
            return false;
    return true;
}
void sieveOfEratosthenes(int n)
{
    bool arr[n + 1];
    FOR(i, 0, n + 1)
    arr[i] = true;
    for (int i = 2; i * i <= n; i++)
    {
        if (arr[i] == true)
        {
            for (int j = i * i ; j <= n; j += i)
            {
                if (arr[j] == true)
                    arr[j] = false;
            }
        }
    }
}

ll ndivs(ll n)
{
    int N = 100;
    bool arr[N];
    FOR(i, 0, N)
    arr[i] = true;
    for (int i = 2; i * i <= N; i++)
    {
        if (arr[i] == true)
        {
            for (int j = i * i ; j <= N; j += i)
            {
                if (arr[j] == true)
                    arr[j] = false;
            }
        }
    }

    ll p = 1;
    FOR(i, 2, N)
    {
        if (arr[i] == true && n % i == 0)
        {
            ll m = 0; ll cp = n;
            while (cp % i == 0)
            {
                cp /= i;
                m++;
            }
            p *= (m + 1);
        }
    }
    return p;
}
bool compare( ll a,  ll b) // false return swap
{
    return a > b;
}

ll power( ll a, ll b)
{
    ll p = 1;
    while (b > 0)
    {
        if (b & 1)
            p *= a;
        a *= a;
        b >>= 1;
    }
    return p;
}

string dtobin(ll n)
{
    string s;
    while (n)
    {
        s = to_string((n % 2)) + s;
        n /= 2;
    }
    return s;
}

ll bintod(string s)
{
    ll n = 0;
    FOR(i, 0, s.length())
    if (s[i] == '0')
        continue;
    else
        n += pow(2, (s.length() - 1 - i));
    return n;
}

ll sdigs(ll n)
{
    int s = 0;
    while (n)
    {
        s += n % 10;
        n /= 10;
    }
    return s;
}

ll fact(ll p)
{
    int x = 1;
    FORN(i, 1, p)
    x *= i;

    return x;
}
ll ncr(ll n , ll r)
{
    ll ans = (fact(n) ) / (fact(r) * fact(n - r));
    return ans;
}

int main() {

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

    ll n;
    cin >> n;
    ll arr[n];
    ll s = 0;
    ll ss = 0;
    FOR(i, 0, n) {
        cin >> arr[i];
        s += arr[i];
        ss += arr[i] * arr[i];
    }
    ll ans = n * ss - s * s;
    cout << ans;
    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()

using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

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

void solve()
{
    ll n;
    cin >> n;
    ll sum = 1e19;
    for (int b = 60; b >= 0; --b)
    {
        ll a = n / (1LL << b);
        ll c = n - a * (1LL << b);
        sum = min(sum, a + b + c);
    }
    cout << sum << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifdef NOOBxCODER
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #else
    #define NOOBxCODER 0
    #endif
    //cin>>t;
    t=1;
    while(t--){
        cin>>n;
        ll a[n];
        fo(i,n)cin>>a[i];
        sort(a,a+n);
        map<ll,ll>m1;
        ll last=0;
        ll ans=1;
        fo(i,n){
            if(m1[a[i]]==0){
                ans*= (a[i]-last + 1);
                ans%=mod;
                last=a[i];
            }
            m1[a[i]]++;
        }
        cout<<ans;

    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); 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; }

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

using mint = Fp<MOD>;

int main(){
    int N; cin >> N;
    vector<int> A(N);
    rep(i, N){
        cin >> A[i];
    }
    whole(sort, A);

    mint res = A[0] + 1;
    rep(i, N-1){
        res *= (A[i+1] - A[i] + 1);
    }
    cout << res << ln;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin( ), v.end()
#define rep(i, n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define rrep(i, n) for(int i=(n); i>=0; 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; }

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
}
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
}
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
}
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
    }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
}
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
         return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

using mint = Fp<MOD>;

mint dp[202020][20];
int main(){
    string N; int K; cin >> N >> K;
    int mask = 0; //Nに使用された数字のマスクビット

    rep(i, N.size()){
        char c = N[i]; int y;
        if(c>='A' and c<='F')y=10+c-'A';
        else y=(c-'0');

        //直前まで(1つ以上の)0,初めて0以外の数字が出る
        if(i>0)dp[i+1][1] += 15;
        //直前までNと同一、初めてN未満になる
        rep(j, y){
            if(i==0 and j==0)continue; //初桁のみ0は選べない
            int x = __builtin_popcount(mask | 1<<j); //数字jを採用したときの種類数
            dp[i+1][x] += 1;
        }
        //その他
        rep(j, 17){
            dp[i+1][j] += dp[i][j] * j;
            dp[i+1][j+1] += dp[i][j] * (16 - j);
        }
        mask |= (1<<y);
    }

    mint res = dp[N.size()][K];
    if(__builtin_popcount(mask)  == K)res += 1;
    cout << res << ln;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>

typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;

#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;

template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
    if (_l < _b) {
        _l = _b;
        return true;
    }
    return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
    if (_l > _b) {
        _l = _b;
        return true;
    }
    return false;
}

template<typename _T>
void vdeb(const std::vector<_T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        std::cout << i << ' ';
        vdeb(bb[i]);
    }
    std::cout << '\n';
}

using namespace std;

int main() {
    int n; cin >> n;
    vector<Pll> da(n);
    ll lower = LLONG_MIN/2, upper = LLONG_MAX/2, offset = 0, up = LLONG_MAX/2, lo = LLONG_MIN/2;
    rip(i,n,0) {
        cin >> da[i].first >> da[i].second;
        if(da[i].second == 1) {
            lo += da[i].first;
            up += da[i].first;
            offset += da[i].first;
        }
        else if(da[i].second == 2) {
            chmax(up, da[i].first);
            chmax(lo, da[i].first);
            chmax(lower, da[i].first-offset);
        }
        else if(da[i].second == 3) {
            chmin(up, da[i].first);
            chmin(lo, da[i].first);
            chmin(upper, da[i].first-offset);
        }
    }

    int q; cin >> q;
    vector<ll> ans(q);
    rip(i,q,0) {
        int x; cin >> x;
        if(upper <= x) ans[i] = up;
        else if(x <= lower) ans[i] = lo;
        else ans[i] = x+offset;
    }
    for(auto &&i : ans) cout << i << endl;
}


#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define f first
#define s second
#define pb push_back

// clang-format off
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T_vector>void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << "(" << v.first << ":" << v.second << ")" << ","; os << "}"; return os; }

auto random_address = [] {char *p = new char; delete p;return uint64_t(p);};
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
// clang-format on

void solve() { }

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

     int n;
     cin >> n;
     vector<int> a(n);
     rep(i, 0, n) cin >> a[i];

     int cnt = 0;
     for (int j = 0; j < n; j++) {
          if (a[j] > 10) {
               cnt += a[j] - 10;
          }
     }
     cout << cnt;
     // int tests;
     // cin >> tests;
     // while (tests-- > 0)
     //      solve();
}

#include <iostream>

using namespace std;

int main (){
    long long n,a;
    cin >> n;
    long long accm(0);
    for (size_t i=0; i<n; i++){
        cin >> a;
        if (a <= 10)
            continue;
        accm += a - 10;
    }

    cout << accm << endl;
    return 0;
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

char cn[1200010];
char* ci = cn, * di = cn, ct;
char tc[200002], *d1 = tc + 100000, *d2 = tc + 200000;
const char d[2] = "0";

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

	rep(i, 100000) tc[i] = '0';
	rep(i, 100000) d1[i] = '1';
	d2[0] = '0';
	d2[1] = '\n';
	fread(cn, 1, 1200010, stdin);
	int T = 0;
	while ((ct = *ci++) >= '0') T = T * 10 + ct - '0';

	rep(t, T) {
		int N = 0;
		while ((ct = *ci++) >= '0') N = N * 10 + ct - '0';
		memcpy(di, d1 - N, N);
		di += N;
		memcpy(di, d2 - N, N + 2);
		di += N + 2;
		ci += 6 * N + 3;
	}
	fwrite(cn, 1, di - cn, stdout);

	Would you please return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
const int maxn=3010,mod=1e9+7;
inline bool ckmin(int &x,const int &y){return x>y?x=y,1:0;}
inline bool ckmax(int &x,const int &y){return x<y?x=y,1:0;}
struct modint{
	int val;
	inline modint(int val_=0):val(val_){}
	inline modint &operator=(int val_){return val=val_,*this;}
	inline modint &operator+=(const modint &k){return val=val+k.val>=mod?val+k.val-mod:val+k.val,*this;}
	inline modint &operator-=(const modint &k){return val=val-k.val<0?val-k.val+mod:val-k.val,*this;}
    inline modint &operator*=(const modint &k){return val=1ll*val*k.val%mod,*this;}
    inline modint &operator^=(int k){
        modint ret=1,tmp=*this;
        for(;k;k>>=1,tmp*=tmp)if(k&1)ret*=tmp;
        return val=ret.val,*this;
    }
    inline modint &operator/=(modint k){return *this*=(k^=mod-2);}
    inline modint &operator+=(int k){return val=val+k>=mod?val+k-mod:val+k,*this;}
    inline modint &operator-=(int k){return val=val<k?val-k+mod:val-k,*this;}
    inline modint &operator*=(int k){return val=1ll*val*k%mod,*this;}
    inline modint &operator/=(int k){return *this*=((modint(k))^=mod-2);}
    template<class T>friend modint operator+(modint a,T b){return a+=b;}
    template<class T>friend modint operator-(modint a,T b){return a-=b;}
    template<class T>friend modint operator*(modint a,T b){return a*=b;}
    template<class T>friend modint operator/(modint a,T b){return a/=b;}
    friend modint operator^(modint a,int b){return a^=b;}
    friend bool operator==(modint a,int b){return a.val==b;}
    friend bool operator!=(modint a,int b){return a.val!=b;}
    inline bool operator!(){return !val;}
    inline modint operator-(){return val?mod-val:0;}
    inline modint &operator++(int){return *this+=1;}
};
using mi=modint;
mi ans[maxn][maxn],sum[maxn][maxn];
int n;
ll a[maxn][maxn];
int main(){
	scanf("%d",&n);
	for(ri i=1;i<=n;++i){
		ll x;
		scanf("%lld",&x);
		for(ri j=1;j<=n;++j)a[i][j]=(a[i-1][j]+x)%j;
	}
	for(ri i=1;i<=n;++i)sum[i][a[n][i]]=1;
	for(ri i=n-1;i;--i){
		for(ri j=1;j<n;++j){
			sum[j][a[i][j]]+=sum[j+1][a[i][j+1]];
		}
	}
	printf("%d",sum[1][0]);
	return 0;
}


/* *****fireice**** */

#include<bits/stdc++.h>

#define ll long long int
#define ld long double
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define el '\n'
#define N 1000000007
#define M 2147483647
#define MM 1e18;
#define re return
#define F first
#define S second
#define L length
#define deb(x) cout<<#x<<" "<<x<<el;
#define mp make_pair
#define con continue
#define PI 3.14159265
#define all(a) (a).begin(),(a).end()
#define forup(i,n) for(int i=0;i<n;i++)
#define forf(i,x,n) for(int i=x;i<n;i++)
#define ford(i,n) for(int (i)=(n)-1;i>=0;(i)--)
#define take(a,n) forup(i,n) cin>>a[i];
using namespace std;

int gcdExtended(int a, int b, int *x, int *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    int x1, y1; // To store results of recursive call
    int gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll modInverse(int a, int m)
{
    int x, y;
    int g = gcdExtended(a, m, &x, &y);
        // m is added to handle negative x
    ll res = ((ll)x%m + (ll)m) % m;

    return res;
}

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

ll modularExponentiation(ll x,ll n,ll Z)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%Z;
        x=(x*x)%Z;
        n=n/2;
    }
    return result;
}

template<typename T>
T maxx(T a,T b)
{
  if(a<b) re b;
  re a;
}
template<typename T, typename... Args>
T maxx(T a,T b, Args... args)
{
    re(maxx(maxx(a,b),args...));
}

template<typename T>
T minn(T a,T b)
{
  if(a<b) re a;
  re b;
}
template<typename T, typename... Args>
T minn(T a,T b, Args... args)
{
    re(minn(minn(a,b),args...));
}

template<typename T>
void read(T& a)
{
  cin>>a;
}
template<typename T, typename... Args>
void read(T& a,Args&... args)
{
  cin>>a; read(args...);
}

template<typename T>
void write(T a)
{
  cout<<a<<' ';
}
template<typename T, typename... Args>
void write(T a,Args... args)
{
  cout<<a<<' '; write(args...);
}

inline ll min(ll a,ll b)
{
  if(a<b) re a;
  else re b;
}
inline ll max(ll a,ll b)
{
  if(a<b) re b;
  else re a;
}

ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a-b, b);
    else return gcd(a, b-a);
}

ll ncr(int n,int r)
{
  ll ans=1;
  r=min(r,n-r);
  forup(i,r)
  {
    ans=((ans*((n-i)%N))%N*modInverse(i+1,N))%N;
  }
  re ans;
}

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

  int t=1;
  //cin>>t;
  while(t--)
  {
    ll n,m,k;
    string s;
    cin>>n;
    cout<<(n+99)/100<<el;

  }

  return 0;
}

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

int main()
{
    int tc=1;
   // cin>>tc;
    while(tc--)
    {

ll n;
cin>>n;
cout<<ceil( (n*1.0)/100 )<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i,a,b) for (ll i = (a); i < (ll)(b); i++)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define MT make_tuple
#define fr first
#define sc second

using namespace std;
template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); }
template <class T> using vc = vector<T>;
template <class T> using vv = vc<vc<T>>;
template <class T> using vvv = vc<vv<T>>;
template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
using vs = vc<string>;
using vpl = vc<pll>;
using vpd = vc<pdd>;
using vvi = vv<int>;
using vvl = vv<ll>;
using vvpl = vv<pll>;

const ld pi = 3.141592653589793;
const ll linf = 1001001001001001001LL;
const int inf = 1001001001;

constexpr int MOD = 1e9+7;
// constexpr int MOD = 998244353;

struct mint {
long long x;
  mint(long long x = 0) noexcept : x((x%MOD + MOD) % MOD) {}

  // basis
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) noexcept { if((x += a.x) >= MOD) x -= MOD; return *this; }
  mint& operator-=(const mint a) noexcept { if((x += MOD-a.x) >= MOD) x -= MOD; return *this; }
  mint& operator*=(const mint a) noexcept { (x *= a.x) %= MOD; return *this; }
  mint operator+(const mint a) const noexcept { return mint(*this) += a; }
  mint operator-(const mint a) const noexcept { return mint(*this) -= a; }
  mint operator*(const mint a) const noexcept { return mint(*this) *= a; }
  mint mpow(long long t) const noexcept {
    if(!t) return 1;
    mint a = mpow(t>>1);
    a *= a;
    if(t&1) a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const noexcept { return mpow(MOD-2); }
  mint& operator/=(const mint a) noexcept { return *this *= a.inv(); }
  mint operator/(const mint a) const noexcept { return mint(*this) /= a; }

  // comparison
  bool operator==(const mint &a) const noexcept { return this->x == a.x; }
  bool operator!=(const mint &a) const noexcept { return this->x != a.x; }

  // I/O stream
  friend istream& operator>>(istream& is, mint& a) noexcept { return is >> a.x; }
  friend ostream& operator<<(ostream& os, const mint& a) noexcept { return os << a.x; }
};
// additional
mint mpow(const long long &a, long long n) noexcept { return mint(a).mpow(n); }
mint mpow(const mint &a, long long n) noexcept { return a.mpow(n); }

int main() {
  ll h, w;
  cin >> h >> w;
  vs S(h);
  rep(i,h) cin >> S[i];
  vvl R(h, vl(w, 0)), C(h, vl(w, 0));
  rep(i,h) {
    ll l = 0, r = 0;
    while (l < w) {
      while (r < w && S[i][r] == '.') r++;
      repr(j,l,r) R[i][j] = r-l;
      if (l == r) r++;
      l = r;
    }
  }
  rep(i,w) {
    ll l = 0, r = 0;
    while (l < h) {
      while (r < h && S[r][i] == '.') r++;
      repr(j,l,r) C[j][i] = r-l;
      if (l == r) r++;
      l = r;
    }
  }
  ll K = 0;
  rep(i,h)rep(j,w) K += S[i][j] == '.';
  mint ans = 0;
  rep(i,h)rep(j,w) {
    if (S[i][j] == '#') continue;
    ans += mpow(2, K-(R[i][j]+C[i][j]-1))*(mpow(2, R[i][j]+C[i][j]-1)-1);
  }
  cout << ans << endl;
}