#include<bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
void solve()
{
  int a,b,c,d,r;
  cin>>a>>b>>c>>d;
  r=(d*c)-b;
  if(r<=0)
    cout<<-1;
  else
  {
    db s=ceil(db(a)/db(r));
    cout<<int(s);
  }
  }
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repx(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++)
#define repr(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define reprx(i, x, n) for (ll i = ((ll)(n)-1); i >= (ll)(x); i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((ll)(x).size())
#define submit(x) {cout<<x<<endl; return 0;}
#define X first
#define Y second
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T>bool chmax(T &a,T b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a,T b) {if (b<a) {a=b; return 1;} return 0;}
template<class T>long long square(T a) {return a*a;}
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 60;
const double PI = 3.1415926535897932;

int main()
{
  ll x,y,a,b;
  cin>>x>>y>>a>>b;
  ll exp=0;
  ll change=0;
  ll copy_x=x;
  while(copy_x < b && copy_x*(a-1) < b)
  {
    copy_x *= a;
    change++;
  }
  y -= x;
  rep(i, change)
  {
    y -= x*(a-1);
    x *= a;
    if (y<=0) submit(exp);
    exp++;
  }
  exp += y/b - (y%b==0);
  submit(exp)
}


#include<iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <complex>
#include <iomanip>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <set>

#define ll long long
#define ld long double
#define HS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1e15
#define point complex <double>
#define all(a) a.begin(), a.end()
#define pi acos(-1)
#define file  freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);

using namespace std;

int main()
{
    HS;
    int t;
    cin>>t;
    long long sum=0;
    while(t--)
    {
        long long a,b;
        cin>>a>>b;
        sum+=((b*(b+1))/2)-(((a-1)*((a-1)+1))/2);

    }
    cout << sum << endl;
    return 0;
}

#include <iostream>
#include <string>
using namespace std;
int main(){
	int n; cin >> n;
	long sum = 0;
	for ( int i = 0; i < n; i++ ){
		int a, b; cin >> a >> b;
		for ( int j = a; j <= b; j++ ){
			sum = sum + j;
		}
	}
	cout << sum;
}


#include <bits/stdc++.h>
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll ;
#define int ll
#define endl '\n'
#define cwk     freopen("D:\\workplace\\CLion\\in.in","r",stdin),freopen("D:\\workplace\\CLion\\out.out","w",stdout)
using namespace std;
vector<int>a;
void solve(){
    int n,m,x;
    cin>>n>>m;
    if(m==0){
        cout<<"1";
        return;
    }
    while(m--){
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    int now=1;
    int minn=1e9;
    for(int i=0;i<a.size();i++){
        if(a[i]-now) minn=min(minn,a[i]-now);
        now=a[i]+1;
    }
    if(a[a.size()-1]!=n) minn=min(minn,n-a[a.size()-1]);
    int cnt=0;
    now=1;
    for(int i=0;i<a.size();i++){
        if(a[i]-now) cnt+=ceil((a[i]-now)*1.0/minn);
        now=a[i]+1;
    }
    cnt+=ceil((n-a[a.size()-1])*1.0/minn);
    cout<<cnt;

}
signed main() {
    //cout<<4368.0/17;
    //ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    //cwk;
    int _=1;
    //cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

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

// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define irreps(i, m, n) for (int i = ((int)(n)-1); i > (int)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (int)(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
using ld = long double;

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<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll LINF = 1e18;

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

  int m,h; cin>>m>>h;
  if(h%m==0) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rg register
const int maxn=1e5+5;
int a[maxn],n,mmax;
int main(){
	scanf("%d",&n);
	for(rg int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mmax=std::max(mmax,a[i]);
	}
	rg int cnt=0,jl;
	for(rg int i=2;i<=mmax;i++){
		rg int ncnt=0;
		for(rg int j=1;j<=n;j++){
			if(a[j]%i==0){
				ncnt++;
			}
		}
		if(ncnt>cnt){
			cnt=ncnt;
			jl=i;
		}
	}
	printf("%d\n",jl);
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

int main() {
  int n;
  scanf("%d",&n);
  vi a(n);
  cin>>a;
  P ans(-1,2);
  srep(x,2,1001) {
    int now = 0;
    rep(i,n) if (a[i]%x == 0) ++now;
    maxs(ans, P(now,x));
  }
  cout<<ans.se<<endl;
  return 0;
}


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

using namespace std;
typedef long long ll;

int main(void)
{
	ll n, k, distance = 0;
	cin >> n >> k;
	// friends[i] = {친구의 위치, 친구가 가지고 있는 돈}
	vector<pair<ll, ll> > friends(n);
	for (int i = 0; i < n; i++)
	{
		cin >> friends[i].first >> friends[i].second;
	}
	sort(friends.begin(), friends.end());
	ll prevFriend = 0;
	for (int i = 0; i < n; i++)
	{
		ll nextFriendDistance = friends[i].first - prevFriend;
		if (nextFriendDistance > k)
		{
			distance += k;
			k = 0;
			break;
		}
		else
		{
			distance += nextFriendDistance;
			k -= nextFriendDistance;
			k += friends[i].second;
			prevFriend = friends[i].first;
		}
	}
	distance += k;
	cout << distance << '\n';
	return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a;
  a.push_back(0);
  a.push_back(n + 1);
  rep(i, m) {
    int temp;
    cin >> temp;
    a.push_back(temp);
  }
  sort(a.begin(), a.end());
  // 差分のminを計算
  vector<int> a_sub;
  rep(i, a.size() - 1) {
    int temp = a[i + 1] - a[i];
    if (temp == 1) continue;
    a_sub.push_back(temp);
  }
  if (a_sub.empty()) {
    cout << 0 << endl;
    return 0;
  }
  sort(a_sub.begin(), a_sub.end());
  int a_sub_min = a_sub[0] - 1;
  int count = 0;
  for (int c : a_sub) {
    count += (c - 2) / a_sub_min + 1;
  }
  cout << count << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define M 1000000007
#define N 100001
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

ll n, arr[N];

int mulMod(ll, ll); int addMod(int, int);
void readArray(ll *, int); void printArray(int *, int);

int main() {
    cin>>n;
    readArray(arr, n);

    ll ans = arr[0];
    for(int i=1; i<n; i++)
        ans = __gcd(arr[i], ans);

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

int mulMod(ll a, ll b) {
    a *= b;
    a %= M;
    return (int)a;
}
int addMod(int a, int b) {
    long long c = a + b;
    if(c >= M) {
        c -= M;
    }
    return (int)c;
}
void readArray(ll *nums, int n) {
    for(int i=0; i<n; i++) cin>>nums[i];
}
void printArray(int *nums, int n) {
    for(int i=0; i<n; i++) cout<<nums[i]<<" ";
    cout<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define f first
#define s second

int n;

int32_t main() {
	cin >> n;
	int Y = 0;
	for (int i =0;i<n;i++) {
		int x; cin >> x;
		Y = __gcd(Y,x);
	}
	cout << Y;
}


//compiledef
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0;i<(n);i++)
#define per(i, n) for(int i=(n);i>0;i--)
#define repx(i, n, x) for(int i=(x);i<(n);i++)
#define xper(i, n, x) for(int i=(n);i>(x);i--)
#define pback push_back

//c++def
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;

int main(){
	int a, b, c, d;
	cin >> a>> b >> c >> d;
	cout << (a*d) -(b*c) << endl;
}

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

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << a * d - b * c << endl;
}


#include <bits/stdc++.h>

using namespace std;
#define set_precision(ans,l) cout << fixed << setprecision(l)<<ans;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define rev(str) reverse(str.begin(), str.end())
#define lb(v,x) lower_bound(v.begin(),v.end(),x)
#define ub(v,x) upper_bound(v.begin(),v.end(),x)
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<ll>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<ll>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
long double EPS = 1e-9;
typedef unsigned long long ull;
typedef long double lld;

/*struct comp {
  bool operator() (const pair<long long, long long> &a, const pair<long long, long long> &b)  const
  {return a.first*a.second>b.first*b.second;}
};*/

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

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

void _print(ull t) {cerr << t;}

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

ll cel(ll a,ll b){
    if(a==0)return 0;
    return((a-1)/b+1);
}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll MIN(ll a,int b){ll ans;(a>=b)?ans=b:ans=a;return ans;}
ll MAX(ll a,int b){ll ans;(a>=b)?ans=a:ans=b;return ans;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll po(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1){ans=(ans*x)%mod;}
        y>>=1;x=(x*x)%mod;
    }
    return ans;
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("Error.txt", "w", stderr);
    #endif
    FIO;
    ll n,t,x,y,k;
    cin>>t>>n;
    ll a = cel(n*100 ,t);
    cout<<(a*(100+t))/100-1;
    return 0;
}

#include <iostream> // cout, endl, cin
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <vector> //vector
using namespace std;
#define _LIBCPP_DEBUG 0
#define rep(i, n) for(int i = 0; i < (int)n; i++)
typedef long long ll;

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

    cout << (n * (100+t) + (t-1)) / t - 1 << endl;

}


#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, init, end) for(ll i = init; i < end; i++)
#define REP(i, init, end) for(ll i = init; i < end + 1; i++)
#define rev(i, end, init) for(ll i = init - 1; i >= end; i--)
#define REV(i, end, init) for(ll i = init; i >= end; i--)
#define PI 3.14159265359
#define EPS 0.0000000001
// #define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;

#define MOD 998244353

ll dp[10001][10001];
char g[10001][10001];

// 累乗mod
ll pmod(ll n, ll m){
    if(m <= 0) return 1;
    if(m == 1) return n % MOD;
    if(m % 2 == 0){
        return pmod(n * n % MOD, m / 2) % MOD;
    }
    return n * pmod(n * n % MOD, (m - 1) / 2) % MOD;
}

int main(){
    ll H, W, K;
    cin >> H >> W >> K;

    rep(i, 1, 10001){
        rep(j, 1, 10001){
            dp[i][j] = 0;
            g[i][j] = 'E';
        }
    }

    ll h, w;
    char c;
    rep(i, 0, K){
        cin >> h >> w >> c;
        g[h][w] = c;
    }
    if(g[H][W] != 'E'){
        dp[H][W] = 1;
    }else{
        dp[H][W] = 3;
    }

    ll counter = 0;
    ll multCount = 1;
    ll multEmpty;
    ll multElse;
    rev(d, 0, (H - 1) + (W - 1)){//cout << "test: " << d << endl;
        multCount = 0;

        REP(k, 0, d){
            if(d - k <= H - 1 && k <= W - 1 && g[d - k + 1][k + 1] == 'E'){
                multCount++;
            }
        }
        multEmpty = pmod(3, multCount - 1) * 2;
        multElse = pmod(3, multCount);
        REP(k, 0, d){
            counter = 0;
            if(d - k <= H - 1 && k <= W - 1){
                if(g[d - k + 1][k + 1] != 'R'){
                    counter += dp[d - k + 1 + 1][k + 1];
                }
                if(g[d - k + 1][k + 1] != 'D'){
                    counter += dp[d - k + 1][k + 1 + 1];
                }
                counter %= MOD;
                if(g[d - k + 1][k + 1] == 'E'){
                    dp[d - k + 1][k + 1] = (counter * multEmpty) % MOD;
                }else{
                    dp[d - k + 1][k + 1] = (counter * multElse) % MOD;
                }
                // cout << "update: i=" << d - k + 1 << ", j=" << k + 1 << ", dp=" << dp[d - k + 1][k + 1] << endl;
            }
        }
    }

    // REP(i, 1, 3){
    //     REP(j, 1, 3){
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    cout << dp[1][1] << endl;

    return 0;
}

#ifdef _DEBUG
#include "../../../library/src/debug_template.hpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <functional>

using namespace std;
using lint = long long;
constexpr int INF = 1010101010;
constexpr lint LINF = 1LL << 60;

struct init {
    init() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} init_;

template<const int &Modulo>
struct Mint {

    lint val;
    constexpr Mint(lint v = 0) noexcept: val(v % Modulo) { if (val < 0) val += Modulo; }

    constexpr Mint &operator+=(const Mint &r) noexcept {
        val += r.val;
        if (val >= Modulo) val -= Modulo;
        return *this;
    }
    constexpr Mint &operator-=(const Mint &r) noexcept {
        val -= r.val;
        if (val < 0) val += Modulo;
        return *this;
    }
    constexpr Mint &operator*=(const Mint &r) noexcept {
        val = val * r.val % Modulo;
        return *this;
    }
    constexpr Mint &operator/=(const Mint &r) noexcept {
        lint a{r.val}, b{Modulo}, u{1}, v{0};
        while (b) {
            lint t = a / b;
            a -= t * b;
            a ^= b, b ^= a, a ^= b;
            u -= t * v;
            u ^= v, v ^= u, u ^= v;
        }
        val = val * u % Modulo;
        if (val < 0) val += Modulo;
        return *this;
    }

    constexpr Mint operator+(const Mint &r) const noexcept { return Mint(*this) += r; }
    constexpr Mint operator-(const Mint &r) const noexcept { return Mint(*this) -= r; }
    constexpr Mint operator*(const Mint &r) const noexcept { return Mint(*this) *= r; }
    constexpr Mint operator/(const Mint &r) const noexcept { return Mint(*this) /= r; }

    constexpr Mint operator-() const noexcept { return Mint(-val); }

    constexpr bool operator==(const Mint &r) const noexcept { return val == r.val; }
    constexpr bool operator!=(const Mint &r) const noexcept { return !((*this) == r); }
    constexpr bool operator<(const Mint &r) const noexcept { return val < r.val; }

    constexpr friend ostream &operator<<(ostream &os, const Mint<Modulo> &x) noexcept { return os << x.val; }
    constexpr friend istream &operator>>(istream &is, Mint<Modulo> &x) noexcept {
        lint tmp{};
        is >> tmp;
        x = Mint(tmp);
        return is;
    }

    [[nodiscard]] constexpr Mint pow(lint n) const noexcept {
        Mint res{1}, tmp{*this};
        while (n > 0) {
            if (n & 1) res *= tmp;
            tmp *= tmp;
            n >>= 1;
        }
        return res;
    }
};

constexpr int MOD = 998244353;
using mint = Mint<MOD>;

int RMOD;
using rmint = Mint<RMOD>;

template<class T>
vector<T> make_vec(size_t s, T val) { return vector<T>(s, val); }
template<class... Size>
auto make_vec(size_t s, Size... tail) {
    return vector<decltype(make_vec(tail...))>(s, make_vec(tail...));
}

int main() {

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

    auto grid = make_vec(H, W, char{});
    auto dp = make_vec(H, W, mint{});
    for (int i = 0; i < K; i++) {
        int h, w;
        char c;
        cin >> h >> w >> c;
        h--, w--;
        grid[h][w] = c;
    }
    DMP(grid);

    dp[0][0] = mint(3).pow(H * W - K);
    constexpr mint z = mint(2) / 3;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            mint r = grid[i][j] == char{} ? z : 1;
            if (j + 1 < W && grid[i][j] != 'D') dp[i][j + 1] += r * dp[i][j];
            if (i + 1 < H && grid[i][j] != 'R') dp[i + 1][j] += r * dp[i][j];
        }
    }
    DMP(dp);

    cout << dp.back().back() << '\n';

    return 0;
}


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

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,a,b;

int main(int argc, char const *argv[]) {
	scanf("%d %d %d %d", &a, &b, &x, &y);
	if (y>2*x) y = 2*x;
	if (a > b) {
		if ((a-b)&1) {
			printf("%d\n", (a-b-1)*y+x);
		}
		else {
			printf("%d\n", (a-b-1)*y+x);
		}
	}
	else if (a == b) {
		printf("%d\n", x);
	}
	else {
		if ((b-a)&1) {
			printf("%d\n", (b-a)*y+x);
		}
		else {
			printf("%d\n", (b-a)*y+x);
		}
	}
	return 0;
}

#include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
#define endl "\n"

struct edge {
	int v;
	long long w;
	edge() {}
	edge(int v, long long w) : v(v), w(w) {};
};

vector<long long> dijkstra(int n, vector<vector<edge> >& G, int s) {
	vector<long long> d(n, 1e18);
	d[s] = 0;
	priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que;
	que.push(make_pair((long long)0, s));
	while (!que.empty()) {
		auto p = que.top();
		que.pop();
		int u = p.second;
		long long dist = p.first;
		if (dist > d[u]) continue;
		for (edge e : G[u]) {
			if (d[e.v] > d[u] + e.w) {
				d[e.v] = d[u] + e.w;
				que.push(make_pair(d[e.v], e.v));
			}
		}
	}
	return d;
}

int main() {
	int a, b, x, y;
	cin >> a >> b >> x >> y;
	a--;
	b--;
	b += 100;
	vector<vector<edge>> G(200);
	for (int i = 0; i < 100; ++i) {
		G[i].emplace_back(i + 100, x);
		G[i + 100].emplace_back(i, x);
	}
	for (int i = 1; i <= 99; ++i) {
		G[i].emplace_back(i + 99, x);
		G[i + 99].emplace_back(i, x);
	}
	for (int i = 0; i < 99; ++i) {
		G[i].emplace_back(i + 1, y);
		G[i + 1].emplace_back(i, y);
		G[i + 100].emplace_back(i + 101, y);
		G[i + 101].emplace_back(i + 100, y);
	}
	auto d = dijkstra(200, G, a);
	cout << d[b] << endl;
	return 0;
}


///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 2e5 + 10;
const ll INF = 1e18;
const ll LOG = 20;
ll n, k;
ll h[MXN], dis[MXN], Jad[LOG][MXN];
vector<ll> adj[MXN], Ver;
void DFS(ll u, ll par, ll d, ll m){
    if(d > m || d >= dis[u]) return;
    dis[u] = d;
    for(auto v : adj[u]){
        if(v != par) DFS(v, u, d + 1, m);
    }
}
void dfs(ll u, ll par){
    Jad[0][u] = par;
    for(int i = 1; i < LOG; i ++){
        Jad[i][u] = Jad[i - 1][Jad[i - 1][u]];
    }
    Ver.push_back(u);
    for(auto v : adj[u]){
        if(v != par) h[v] = h[u] + 1, dfs(v, u);
    }
}
ll K_Jad(ll u, ll k){
    for(int i = 0; i < LOG; i ++){
        if((k >> i) & 1LL) u = Jad[i][u];
    }
    return u;
}
ll check(ll x){
    ll ans = 0;
    for(int i = 1; i <= n; i ++) dis[i] = INF;
    for(auto u : Ver){
        if(dis[u] <= x) continue;
        ans ++;
        DFS(K_Jad(u, min(h[u], x)), 0, 0, x);
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i < n; i ++){
        ll u, v; cin >> u >> v;
        adj[u].push_back(v), adj[v].push_back(u);
    }
    dfs(1, 0);
    sort(Ver.begin(), Ver.end(), [&](const int& u, const int &v){
        return h[u] > h[v];
    });
    ll l = 0, r = n;
    while(r - l > 1){
        ll mid = (l + r) / 2;
        if(check(mid) <= k) r = mid;
        else                l = mid;
    }
    cout << r << '\n';
    return 0;
}
/*!
    HE'S AN INSTIGATOR,
    ENEMY ELIMINATOR,
    AND WHEN HE KNOCKS YOU BETTER
    YOU BETTER LET HIM IN.
*/
//! N.N

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

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// // using mint = modint998244353;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

template<class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &e : v) {
        is >> e;
    }
    return is;
}

bool print_space_enable = false;
void print() {
    std::cout << '\n';
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) std::cout << " ";
    std::cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << '\n';
}

template<class T>
vector<T> make_vec(size_t n, T val) {
    return vector<T>(n, val);
}

template<class... Ts>
auto make_vec(size_t n, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}

void fast_IO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

Int rec(vector<Int> &dp, Int state) {
    if (dp[state] != INF) return dp[state];
    for (Int bit = state & (state - 1); bit > 0; bit = (state & (bit - 1))) {
        dp[state] = min(dp[state], rec(dp, bit) + rec(dp, state - bit));
    }
    return dp[state];
}

void solve() {
    fast_IO();
    Int n, m;
    cin >> n >> m;
    vector<Int> a(m), b(m);
    auto c = make_vec(n, n, false);
    for (Int i = 0; i < m; i++) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        c[a[i]][b[i]] = c[b[i]][a[i]] = true;
    }
    auto dp = make_vec(1<<n, INF);
    dp[0] = 0;
    vector<bool> d(1<<n, true);
    for (Int bit = 1; bit < (1<<n); bit++) {
        for (Int i = 0; i < n - 1; i++) {
            for (Int j = i + 1; j < n; j++) {
                if ((bit & (1<<i)) and (bit & (1<<j)) and (not c[i][j])) {
                    d[bit] = false;
                }
            }
        }
        if (d[bit]) {
            dp[bit] = 1;
        }
    }
    print(rec(dp, (1<<n) -1));
}

int main() {
    solve();
    //Int _t; cin >> _t; while (_t--) solve();
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a-b+c;
    return 0;
}

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

using namespace std;

int findeven(std::vector<int> v)
{
    for(int i=0;i<v.size();i++)
    {
        if(v[i]%2==0)
            return i;
    }
    return -1;
}

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

    /* code */
int a,b,c;
cin>>a>>b>>c;

int a1,b1,c1;

a1 = 7-a;
b1 = 7-b;
c1 = 7-c;
cout<<a1+b1+c1;

return 0;

}


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

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()
#define ff first
#define ss second

const double pi=3.1415926535897932384626433832795;
const int inf=1e9;
const ll inf2=1e18;
const int mod=1e9+7;

void boost(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int adj[20][20],n;
vi dp(1<<20,inf);

void solve(){
	int m;
	cin>>n>>m;
	int u,v;
	fr(i,0,m){
		cin>>u>>v;
		u--;
		v--;
		adj[u][v]=adj[v][u]=1;
	}
	for(int i=1;i<(1<<n);i++){
		if(!(i&(i-1))){
			dp[i]=1;
			continue;
		}
		dp[i]=1;
		for(int j=0;j<n;j++){
			if((i>>j)&1){
				for(int k=j+1;k<n;k++){
					if((i>>k)&1){
						if(adj[j][k]==0)
							dp[i]=inf;
					}
				}
			}
		}
		for(int j=(i-1)&i;j;j=(j-1)&i){
			dp[i]=min(dp[i],dp[j]+dp[i^j]);
		}
	}
	cout<<dp[(1<<n)-1]<<endl;
	return;
}

int main()
{
	boost();

	int tc=1;
	//cin>>tc;
	while(tc--)
		solve();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
using pint = pair<ll, ll>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

//snukeさんの答え
int main() {
    vector<string> s(3);
    rep(i, 0, 3) cin >> s[i];
    map<char, ll> mp;
    set<char> heads;
    rep(i, 0, 3) {
        reverse(s[i].begin(), s[i].end());
        ll t = 1;
        if (i == 2) t = -1;
        for (char c : s[i]) {
            mp[c] += t;
            t *= 10;
        }
        reverse(s[i].begin(), s[i].end());
        heads.insert(s[i][0]);
    }
    if (mp.size() > 10) {
        cout << "UNSOLVABLE\n";
        return 0;
    }

    vector<int> p(10);
    iota(p.begin(), p.end(), 0);
    do {
        int i = 0;
        ll sum = 0;
        for (auto x : mp) {
            char c = x.first;
            ll t = x.second;
            sum += t * p[i];
            if (p[i] == 0 && heads.count(c) != 0) sum += 1e15;
            ++i;
        }
        if (sum == 0) {
            i = 0;
            for (auto &x : mp) {
                x.second = p[i];
                ++i;
            }
            // if (mp[s[0][0]] == 0 || mp[s[1][0]] == 0 || mp[s[2][0]] == 0) continue;
            rep(i, 0, 3) {
                rep(j, 0, s[i].size()) {
                    cout << mp[s[i][j]];
                }
                cout << "\n";
            }
            return 0;
        }
    } while (next_permutation(p.begin(), p.end()));

    cout << "UNSOLVABLE\n";
}


#include<iostream>
#include<bits/stdc++.h>
#define forf(i,start,n,increment) for(int i = start;i<n;i+=increment)
#define all(i) i.begin(),i.end()
#define traverse(container,it) for(typeof(container.begin()) it = container.begin();it != container.end();it++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int>v(3);
    cin>>v[0]>>v[1]>>v[2];
    sort(all(v));
    cout<<v[2]+v[1];
    return 0;
}

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

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

  	if((a >= b && b >= c) || (b >= a && a >= c) ) cout << a + b << endl;
  	else if((a >= c && c >= b) || (c >= a && a >= b) ) cout << a + c << endl;
  	else if((c >= b && b >= a) || (b >= c && c >= a) ) cout << b + c << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 1005
int n,k,tot,tt;
int a[maxn][maxn];
int vis[maxn][maxn];
int sum[maxn][maxn];
int you[maxn][maxn];
int num[maxn*maxn];
map<int,int>to,to1;
bool check(int x)
{
	memset(sum,0,sizeof(sum));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(a[i][j]<=x) vis[i][j]=1;
			else vis[i][j]=0;
			sum[j][i]=sum[j][i-1]+vis[i][j];
			you[j][i]=you[j][i-1]+(a[i][j]==x);
		}
	}
//	for(int i=1;i<=n;i++)
//	{
//		for(int j=1;j<=n;j++)
//		cout<<vis[i][j]<<' ';
//		cout<<endl;
//	}
//	for(int j=1;j<=n;j++)
//	{
//		for(int i=1;i<=n;i++)
//		cout<<sum[j][i]<<' ';
//		cout<<endl;
//	}
	int ret=0;
	for(int i=k;i<=n;i++)
	{
		int ans=0;
		int yo=0;
		for(int p=1;p<=k;p++)
		{
			ans+=sum[p][i]-sum[p][i-k];
			yo+=you[p][i]-you[p][i-k];
		}
		if(ans>=(k*k-k*k/2)) return 1;
		int l=1;
		for(int j=k+1;j<=n;j++)
		{
			ans-=sum[l][i]-sum[l][i-k];
			yo-=you[l][i]-you[l][i-k];
			l++;
			ans+=sum[j][i]-sum[j][i-k];
			yo+=you[j][i]-you[j][i-k];
			if(ans>=(k*k-k*k/2)) return 1;
		}
	}
	return 0;
}
int ef(int l,int r)
{
	if(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(mid)) return ef(l,mid);
		return ef(mid,r);
	}
	if(check(l)) return l;
	return r;
}
signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			cin>>a[i][j];
			num[++tt]=a[i][j];
		}
	sort(num+1,num+tt+1);
	for(int i=1;i<=tt;i++) if(!to[num[i]]) to[num[i]]=++tot,to1[tot]=num[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			a[i][j]=to[a[i][j]];
	int l=1,r=tot;
//	for(int i=1;i<=tot;i++) if(check(i)) {cout<<to1[i];return 0;}
	cout<<to1[ef(l,r)];
//	if(check(l)) cout<<to1[l];
//	else cout<<to1[r];
	return 0;
}

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

map<vector<int>, long long> dp;

long long gcd(long long x, long long y) {
  while (y) {
    long long t = x;
    x = y;
    y = t % y;
  }
  return x;
}

bool coprime[80][80];

long long f(vector<int> idxs) {
  if (idxs.size() == 0) return 1;
  if (dp.find(idxs) != dp.end()) return dp[idxs];
  long long ret = 1;
  for (int i = 0; i < idxs.size(); ++i) {
    vector<int> nidxs;
    for (int j = i + 1; j < idxs.size(); ++j) {
      if (coprime[idxs[i]][idxs[j]]) nidxs.push_back(idxs[j]);
    }
    ret += f(nidxs);
  }
  return dp[idxs] = ret;
}

int solve() {
  long long a, b;
  scanf("%lld %lld", &a, &b);
  vector<int> idxs(b - a + 1);
  for (int i = 0; i <= b - a; ++i) {
    for (int j = i + 1; j <= b - a; ++j) {
      coprime[i][j] = (gcd(a + i, a + j) == 1);
    }
    idxs[i] = i;
  }

  printf("%lld\n", f(idxs));
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  for (int tc = 0; tc < t; ++tc) {
    solve();
  }
  return 0;
}


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

#define fi first
#define se second
#define rng(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define per(i, b) gnr(i, 0, b)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define sz(x) (int)(x).size()
#define cinv(a) rep(i, sz(a)) cin >> a[i]
#define debug(a) rep(i, sz(a)) cout << a[i] << " \n"[i == sz(a) - 1]
#define show(x) cerr << #x << " = " << x << endl;
#define pb push_back
#define pf push_front
#define eb emplace_back

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;

template <typename T> bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T> bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int MOD = 998244353;
const int INF = 1e9 + 9;
const ll LINF = 1e18;
const long double eps = 1e-10;
const char nl = '\n';

ll power(ll a, ll b, int mod) { return b ? power(a * a % mod, b / 2, mod) * (b % 2 ? a : 1) % mod : 1; }

ll nCk(int n, int k, int mod)
{
    ll x = 1, y = 1;
    for (int i = 1; i <= k; ++i)
    {
        x = x * (n - i + 1) % mod;
        y = y * i % mod;
    }
    return x * power(y, mod - 2, mod) % mod;
}

struct UF
{
    vi d;
    UF(int n) : d(n, -1) {}
    int root(int x)
    {
        if (d[x] < 0) return x;
        return d[x] = root(d[x]);
    }
    bool unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -d[root(x)]; }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
    int n; cin >> n;
    vl A(n); cinv(A);
    sort(all(A));
    ll ans = 0;
    ll sum = 0;
    per(i, n)
    {
        ans += A[i] * (sum + A[i]) % MOD;
        ans %= MOD;
        sum = (2 * sum + A[i]) % MOD;
    }
    cout << ans << nl;
}

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/

constexpr lnt MOD = 998244353;
struct mint
{
	lnt v;
	mint():v(0){}
	mint(lnt v):v((v+MOD)%MOD){}
	mint operator-()const{ return mint(0) - *this; }
	mint& operator+=(const mint& a){ if((v+=a.v)>=MOD) v-=MOD; return *this; }
	mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; }
	mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; }
	mint& operator/=(const mint& a){ (*this) *= a.inv(); return *this; }
	mint operator+(const mint& a)const{ return mint(*this) += a; }
	mint operator-(const mint& a)const{ return mint(*this) -= a; }
	mint operator*(const mint& a)const{ return mint(*this) *= a; }
	mint operator/(const mint& a)const{ return mint(*this) /= a; }
	bool operator<(const mint& a)const{ return v < a.v; }
	bool operator==(const mint& a)const{ return v == a.v; }
	mint pow(lnt k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; }
	mint inv()const{ return pow(MOD-2); }
	static mint comb(lnt n, lnt k) { if(n-k<k) k=n-k; mint num(1), dom(1); for(int i=0;i<k;i++) { num*=n-i; dom*=i+1; } return num/dom; }
	static std::vector<mint> construct_comb(int n) {
		std::vector<mint> c(n+1); mint a = 1; c[0] = a; for(int i=1;i<=n;i++) { a = a*mint(n+1-i)/i; c[i] = a; } return c;
	}
	static std::vector<mint> construct_fact(int n) { std::vector<mint> f(n+1,1); for(int i=2;i<=n;i++) f[i]=f[i-1]*i; return f; }
};
std::istream& operator>>(std::istream&i,mint&a){ lnt t; i>>t; a=mint(t); return i; }
std::ostream& operator<<(std::ostream&o,const mint&a){ o<<a.v; return o; }

int main() {
	int n;
	std::cin >> n;
	V<int> f(n);
	fill(f);
	rep(i,n) f[i]--;
	int count=0;
	V<int> went(n,-1);
	rep(i,n) {
		if(went[i]==-1) {
			int ok=true;
			int o=i;
			while(went[o]==-1) {
				went[o]=i;
				o=f[o];
			}
			if(went[o]!=i) ok=false;
			if(ok) count++;
		}
	}
	print(mint(2).pow(count)-mint(1));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
  ll N;
  cin >> N;
  string S;
  ll P = 2;
  ll ans = 1;
  for(int i = 0; i < N; i++){
  	cin >> S;
    if(S == "OR"){
      ans += P;
    }
    P *= 2;
  }
  cout << ans << endl;

}

#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,n) repi(i,0,n)
#define repi(i,a,n) for(ll i=a;i<(ll)n;++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define Show(val) cout<<(val)<<" "
#define Showln(val) cout<<(val)<<endl

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
// using P = pair<int, int>;
void YN(bool a) { cout << (a ? "Yes" : "No"); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

ll ans[2][63][2];

void solve()
{
  ll n;
  cin >> n;
  string s;

  // ans[i][j][k] = 自分がiの時にj番目の計算をして、数値がkになるものの個数
  ans[0][0][0] = 1;
  ans[0][0][1] = 1;
  ans[1][0][0] = 0;
  ans[1][0][1] = 0;
  repi(i, 1, n + 1)
  {
    cin >> s;
    if (s == "AND")
    {
      ans[0][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0] + ans[0][i - 1][1] + ans[1][i - 1][1];
      ans[1][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0];
      ans[0][i][1] = 0;
      ans[1][i][1] = ans[0][i - 1][1] + ans[1][i - 1][1];
    }
    else // if (s == "OR")
    {
      ans[0][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0];
      ans[1][i][0] = 0;
      ans[0][i][1] = ans[0][i - 1][1] + ans[1][i - 1][1];
      ans[1][i][1] = ans[0][i - 1][0] + ans[1][i - 1][0] + ans[0][i - 1][1] + ans[1][i - 1][1];
    }

  }
  cout << (ans[0][n][1] + ans[1][n][1]) << endl;
}

int main()
{
  fastio;
  solve();

  return 0;
}


#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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 Kth_root(ll x,int k){
    if(x==1) return 1;
    if(k>64) return 1;
    ll l=0,r=numeric_limits<ll>::max();
    while(r-l>1){
        ll mid=(l+r)/(ll)2;
        ll d=mid;
        bool larger=false;
        rep(i,k-1){
            if(d>x/mid+(ll)1){
                larger=true;
                break;
            }
            d*=mid;
        }
        if(d>x) larger=true;
        if(larger) r=mid;
        else l=mid;
    }
    return l;
}

string x;
ll m;

void solve(){
    cin >> x;
    cin >> m;
    int n=x.length();
    int ans=0;
    int l=0;
    rep(i,n){
        chmax(l,x[i]-'0');
    }
    if(n==1){
        if(x[0]-'0'<=m) cout << 1 << endl;
        else cout << 0 << endl;
        return;
    }
    if(n==2){
        ll a=x[0]-'0',b=x[1]-'0';
        cout << max(0ll,(m-b)/a-l) << endl;
        return;
    }
    ll L=l,R=mod;
    while(R-L>1){
        ll mid=(L+R)/2;
        ll A=0,D=1;
        rep(i,n){
            A+=D*(x[n-1-i]-'0');
            if(i<n-1) D*=mid;
            if(D>m || D<0) break;
        }
        if(D>m || D<0 || A>m) R=mid;
        else L=mid;
    }
    cout << L-l << endl;
}

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

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int n;
  ll x;
  cin >> n >> x;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  unordered_map<ll, ll> v;
  v[x] = 1;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    unordered_map<ll, ll> nv;
    for (auto [c, f] : v) {
      if (i + 1 < n) {
        ll rem = c % a[i + 1];
        nv[c - rem] += f;
        if (rem > 0)
          nv[c + a[i + 1] - rem] += f;
      } else {
        if (c % a[i] == 0)
          nv[0] += f;
      }
    }
    v = move(nv);
  }
  cout << v[0];
}


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

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

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

#define fast			ios_base::sync_with_stdio(0);cin.tie(0)
#define f0(a,b,c) 		for(int a=b;a<c;a++)
#define re(a,b,c) 		for(int a=b;a>=c;a--)
#define trav(a,x)		for(auto &a:x)
#define ini(a,x)		memset(a,x,sizeof(a))
#define pb 				push_back
#define eb				emplace_back
#define fi 				first
#define se 				second
#define all(con) 		con.begin(),con.end()
#define rall(con)		con.rbegin(),con.rend()
#define ll 				long long
#define vi 				vector<int>
#define vll 			vector<ll>
#define MOD 			1000000007
#define pi 				pair<int,int>
#define pll 			pair<ll,ll>
#define mi 				map<int,int>
#define mll				map<ll,ll>
#define umi 			unordered_map<int,int>
#define umll 			unordered_map<ll,ll>
#define lb 				lower_bound
#define ub 				upper_bound
#define pq 				priority_queue<ll,ll>
#define st				stack<int>
#define qu				queue<int>
#define endl            "\n"
#define sz(a)			(int)a.size()
using ull = unsigned long long;

template<class T> int chkmax(T &a, T b) {
	if (b > a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class T> int chkmin(T &a, T b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class iterator> void output(iterator begin, iterator end, ostream& out = cerr) {
	while (begin != end) {
		out << (*begin) << " ";
		begin++;
	}
	out << endl;
}

template<class T> void output(T x, ostream& out = cerr) {
	output(x.begin(), x.end(), out);
}

ll powm(ll a , ll n)
{
	ll res = 1;

	while(n)
	{
		if(n & 1)
		res = (res * a);

		n >>= 1;
		a = (a * a);
	}

	return res;
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
	return bin_pow(x, MOD - 2);
}
struct{
    int x1,y1,x2,y2,x3,y3;
} m;
int main()
{
	fast;
	int t=1;
	//cin>>t;
	while(t--)
	{
	  ll n,x;
	  cin>>n>>x;
	  vll a(n);
	  f0(i,0,n) cin>>a[i];
	  int k=n/2;
	  ll ans=0;
	  set<ll,greater<ll>> possible;
	  f0(i,0,(1<<k))
	  {
	      ll temp=0;
	      f0(j,0,k)
	      {
	          if(i >> j & 1)
	           {
	               temp+=a[j];
	           }
	      }
	      if(temp<=x)
	      {
	          possible.insert(temp);
	          chkmax(ans,temp);
	      }
	  }
	  k=n-k;
	  f0(i,0,(1<<k))
	  {
	      ll temp=0;
	      f0(j,0,k)
	      {
	          if(i >> j & 1)
	          {
	              temp+=a[n/2+j];
	          }
	      }
	      if(temp<=x)
	      {
	          chkmax(ans,temp);
	      }
	      auto y=possible.lb(x-temp);
	      if(y!=possible.end())
	        chkmax(ans,temp + *y);
	  }
	  cout<<ans<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

int main() {
	int N,T;
	int a[45];
	cin>>N>>T;
	for(int i=0; i<N; i++) cin>>a[i];
	vector<ll> p(1<<N/2),q(1<<(N-N/2));
	for(int i=0; i<1<<(N/2); i++) {
		for(int j=0; j<N/2; j++)
		{
			if( i & 1<<j ) {
				p[i]+=a[j];
			}
		}
	}
	for(int i=0; i<1<<(N-N/2); i++) {
		for(int j=0; j<N-N/2; j++)
		{
			if( i & 1<<j ) {
				q[i]-=a[N/2+j];
			}
		}
	}
	sort(q.begin(), q.end());
	ll ans = 0;
	for(int i=0; i<p.size(); i++) {
		if(T<p[i]) continue;
		int L = 0; int R = q.size()-1;
		int idx = lower_bound(q.begin(), q.end(), -T+p[i]) - q.begin();
		ans = max(ans, -q[idx]+p[i]);
	}
	cout<<ans<<endl;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <chrono>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#else
#define File(s)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mkp make_pair
#define pb push_back
#define SP putchar(' ')
#define EL putchar('\n')

mt19937 rnd(chrono :: steady_clock :: now().time_since_epoch().count());
mt19937_64 rnd64(chrono :: steady_clock :: now().time_since_epoch().count()) ;

template<class T> inline void gi(T &x){
  char ch; x = 0;int f = 1;
  while(isspace(ch = getchar()));
  if(ch == '-') ch = getchar(), f = -1;
  do x = x * 10 + ch - '0'; while(isdigit(ch = getchar()));
  x *= f;
}
template<class T> inline void wi(T x){
  static char stk[128]; int top = 0;
  if(x == 0) putchar('0');
  if(x < 0) putchar('-'), x = -x;
  while(x) stk[++top] = x % 10, x /= 10;
  while(top) putchar(stk[top--] + '0');
}
template<class T, class ...Args>
inline void gi(T &x, Args&... args) {gi(x); gi(args...);}
template<class T, class ...Args>
inline void wi(T x, Args... args) {wi(x); SP; wi(args...);}
template<class T> void upmax(T &x, T y) {x = x > y ? x : y;}
template<class T> void upmin(T &x, T y) {x = x < y ? x : y;}

const int N = 400005;
vector<int> G[N];
int a[N], b[N];
int cnt = 0;
bool tree = false;
bool vis[N];

void dfs(int x, int fa) {
  vis[x] = true;
  ++cnt;
  for (int v : G[x]) {
    if (vis[v] && v != fa) tree = false;
    if (!vis[v]) dfs(v, x);
  }
}

int main() {
  File("b");
  int n, res = 0;
  gi(n);
  for (int i = 1; i <= n; ++i) gi(a[i], b[i]), G[a[i]].pb(b[i]), G[b[i]].pb(a[i]);
  for (int i = 1; i <= 400000; ++i) {
    if (!vis[i]) {
      cnt = 0;
      tree = true;
      dfs(i, -1);
      res += cnt - 1 + int(!tree);
    }
  }
  printf("%d\n", res);
  return 0;
}

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// clang-format off
#define PI 3.14159265358979323846264338327950L
#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 prep_ios(do_flush) if(!do_flush){ ios::sync_with_stdio(false);cin.tie(nullptr);constexpr char endl='\n'; } cout<<fixed<<setprecision(20)
#define put(expr) cout<<expr<<'\n'
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "[ "; for (auto v: vec) os << v << " "; os << "]"; return os; }template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "< "; for (auto v: vec) os << v << " "; os << ">"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; }template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; 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; }template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << ", " << pa.second << ")"; return os; }
typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef float f32; typedef double f64; typedef long double f80;
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef map<int, int> mii;
typedef __int128_t i128; i128 parse(string &s) { i128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream &operator>>(istream &is, i128 &v) { string s; is >> s; v = parse(s); return is; } ostream &operator<<(ostream &os, const i128 &v) { if (v == 0) return (os << "0"); i128 num = v; if (v < 0) { os << '-'; num = -num; } string s; for (; num > 0; num /= 10) s.push_back('0' + (char)(num % 10)); reverse(s.begin(), s.end()); return (os << s); }
template <typename T> bool chmax(T &now, const T &cand) { if (now < cand) { now = cand; return true; } return false; } template <typename T> bool chmin(T &now, const T &cand) { if (now > cand) { now = cand; return true; } return false; }
template <typename T> T gcd(const T &a, const T &b) { if (a % b == 0) return b; return gcd(b, a % b); } template <typename T> T lcm(const T &a, const T &b) { return a * b / gcd(a, b); }
template <typename T> T nCr(const T &n, T k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; int result = n; for (int i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; }
const int INF = 1<<30; /* INF > 10^9 */ const i64 INFLL = 1LL<<60; /* INFLL > 10^18 */ const string YES = "Yes", NO = "No";

// clang-format on?

int main() {
  prep_ios(true);

  i64 N, X;
  cin >> N >> X;

  // vector<i64> Fs(202020, 0);
  vector<pair<i64, i64>> Fs;

  rep(i, N) {
    i64 a =0; i64 b = 0;
    cin >> a >> b;
    Fs.push_back({a, b});
  };

  sort(Fs.begin(), Fs.end());

  rep(i, N) {
    auto pf = Fs[i];
    if (pf.first > X){
      break;
    } else {
      X += pf.second;
    }
  }
  put(X);
}


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

using namespace std;

#define debug(x) cerr << #x << " = " << x << endl
#define boost ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) { cout << endl; }

template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << " ";
    err(++it, args...);
}

template<typename T>
inline void read(T& x) {
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    x *= f;
}

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

constexpr int MAXN = 1e5 + 5, MOD = 1e9 + 7;

int n, m, k;
array<int, 11> a;
array<double, MAXN * 2> sufa, sufb;

int main() {
    boost;
    cin >> n >> m >> k;
    cout << setprecision(4) << fixed;
    set<int> s;
    for (int i = 1; i <= k; i++) cin >> a[i], s.insert(a[i]);
    for (int i = n - 1; i >= 0; i--) {
        if (s.count(i)) {
            sufa[i] = sufa[i + 1] + 1;
            sufb[i] = sufb[i + 1];
            continue;
        }
        double tmpa = (sufa[i + 1] - sufa[i + m + 1]) / m;
        double tmpb = (sufb[i + 1] - sufb[i + m + 1]) / m + 1;
        sufa[i] = sufa[i + 1] + tmpa;
        sufb[i] = sufb[i + 1] + tmpb;
        if (i == 0) {
            if (abs(1.0 - tmpa) > 1e-15)cout << tmpb / (1.0 - tmpa) << endl;
            else cout << -1 << endl;
        }
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <random>
#include <fstream>

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

#define l_ength size

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

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

int x[222],y[222],r[222],a[222],b[222],c[222],d[222];
bool mp[10000][10000];

bool fil_row(int p, int l, int r){
	int i;
	bool flag = false;
	if(!(0 <= p && p < 10000)){
		return false;
	}
	for(i=l; i<r; ++i){
		flag |= mp[p][i];
	}
	if(flag){
		return false;
	}
	for(i=l; i<r; ++i){
		mp[p][i] = true;
	}
	return true;
}

bool fil_col(int p, int l, int r){
	int i;
	bool flag = false;
	if(!(0 <= p && p < 10000)){
		return false;
	}
	for(i=l; i<r; ++i){
		flag |= mp[i][p];
	}
	if(flag){
		return false;
	}
	for(i=l; i<r; ++i){
		mp[i][p] = true;
	}
	return true;
}

bool del_row(int p, int l, int r, int s){
	int i;
	if(a[s] >= c[s]){
		return true;
	}
	if(b[s] >= d[s]){
		return true;
	}
	if(!(a[s] <= x[s] && x[s] < c[s])){
		return true;
	}
	if(!(b[s] <= y[s] && y[s] < d[s])){
		return true;
	}
	for(i=l; i<r; ++i){
		mp[p][i] = false;
	}
	return false;
}

bool del_col(int p, int l, int r, int s){
	int i;
	if(a[s] >= c[s]){
		return true;
	}
	if(b[s] >= d[s]){
		return true;
	}
	if(!(a[s] <= x[s] && x[s] < c[s])){
		return true;
	}
	if(!(b[s] <= y[s] && y[s] < d[s])){
		return true;
	}
	for(i=l; i<r; ++i){
		mp[i][p] = false;
	}
	return false;
}

bool score(int i, int j){
	int before,after;
	before = (c[i]-a[i])*(d[i]-b[i]);
	if(j<2){
		after = before + (d[i]-b[i]);
	}else if(j<4){
		after = before + (c[i]-a[i]);
	}else if(j<6){
		after = before - (d[i]-b[i]);
	}else{
		after = before - (c[i]-a[i]);
	}
	return 1ll*std::min(after,r[i])*std::max(before,r[i]) > 1ll*std::min(before,r[i])*std::max(after,r[i]);
}

int main(void){
	int n,i,tm,j,k,calc=33*32;
	tm = clock();
	std::random_device seed_gen;
	std::default_random_engine engine(seed_gen());
	scanf("%d",&n);
	std::uniform_int_distribution<> rnd1(0,n-1);
	std::uniform_int_distribution<> rnd2(0,8);
	for(i=0; i<n; ++i){
		scanf("%d%d%d",&x[i],&y[i],&r[i]);
		mp[x[i]][y[i]] = true;
		a[i] = x[i]; b[i] = y[i]; c[i] = x[i]+1; d[i] = y[i]+1;
	}
	while(clock() - tm < ((TT * CLOCKS_PER_SEC) >> SS)){
		i = rnd1(engine);
		j = rnd2(engine);

		if(!score(i,j) && ((seed_gen())%(calc>>4))){
			continue;
		}
		switch(j){
			case 0:
			if(fil_row(a[i]-1,b[i],d[i])){
				--a[i];
			}
			break;
			case 1:
			if(fil_row(c[i],b[i],d[i])){
				++c[i];
			}
			break;
			case 2:
			if(fil_col(b[i]-1,a[i],c[i])){
				--b[i];
			}
			break;
			case 3:
			if(fil_col(d[i],a[i],c[i])){
				++d[i];
			}
			break;
			case 4:
			++a[i];
			if(del_row(a[i]-1,b[i],d[i],i)){
				--a[i];
			}
			break;
			case 5:
			--c[i];
			if(del_row(c[i],b[i],d[i],i)){
				++c[i];
			}
			break;
			case 6:
			++b[i];
			if(del_col(b[i]-1,a[i],c[i],i)){
				--b[i];
			}
			break;
			default:
			--d[i];
			if(del_col(d[i],a[i],c[i],i)){
				++d[i];
			}
			break;
		}
		++calc;
    }
    for(i=0; i<n; ++i){
    	printf("%d %d %d %d\n",a[i],b[i],c[i],d[i]);
    }
	return 0;
}

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

int main(){
    int N; cin >> N;
    int x[N],y[N],r[N],a[N],b[N],c[N],d[N];
    vector<tuple<int,int,int,int>> order;
    //set<P> used;
    //vector<vector<P>> rowfront; vector<vector<P>> rowback;
    //vector<vector<P>> columnfront; vector<vector<P>> columnback;
    vector<vector<P>> column(10000); vector<vector<P>> row(10000);
    rep(i,N){
        cin >> x[i] >> y[i] >> r[i];
        order.push_back(make_tuple(r[i],x[i],y[i],i));
        //rowfront[x[i]].push_back(make_pair(y[i],y[i])); rowback[x[i]].push_back(make_pair(y[i],y[i]));
        //coulmnfront[y[i]].push_back(make_pair(x[i],x[i])); columnback[y[i]].push_back(make_pair(x[i],x[i]));
        column[y[i]].push_back(make_pair(x[i],x[i])); row[x[i]].push_back(make_pair(y[i],y[i]));
    }
    sort(order.begin(),order.end());
    rep(i,N){
        //s:r p:x q:y k:i
        int s = get<0>(order[i]); int p = get<1>(order[i]);
        int q = get<2>(order[i]); int k = get<3>(order[i]);
        int fx = 0; int bx = 0; int fy = 0; int by = 0;
        bool fxok = true; bool bxok = true; bool fyok = true; bool byok = true;
        rep(j,10000){
            if((fx+bx+3)*(fy+by+3)>s) break;
            if(p-bx-1<0) bxok = false;
            if(p+fx+1>9999) fxok = false;
            if(q-by-1<0) byok = false;
            if(q+fy+1>9999) fyok = false;
            if(byok){
                for(auto range:column[q-j-1]){
                    int ini = range.first; int fin = range.second;
                    if(ini>p+fx||fin<p-bx) continue;
                    byok = false; break;
                }
                if(byok) by = j+1;
            }
            if(fyok){
                for(auto range:column[q+j+1]){
                    int ini = range.first; int fin = range.second;
                    if(ini>p+fx||fin<p-bx) continue;
                    fyok = false; break;
                }
                if(fyok) fy = j+1;
            }
            if(bxok){
                for(auto range:row[p-j-1]){
                    int ini = range.first; int fin = range.second;
                    if(ini>q+fy||fin<q-by) continue;
                    bxok = false; break;
                }
                if(bxok) bx = j+1;
            }
            if(fxok){
                for(auto range:row[p+j+1]){
                    int ini = range.first; int fin = range.second;
                    if(ini>q+fy||fin<q-by) continue;
                    fxok = false; break;
                }
                if(fxok) fx = j+1;
            }
            if(!fxok&&!bxok&&!fyok&&!byok) break;
        }
        column[q+fy].push_back(make_pair(p-bx,p+fx));
        column[q-by].push_back(make_pair(p-bx,p+fx));
        row[p+fx].push_back(make_pair(q-by,q+fy));
        row[p-bx].push_back(make_pair(q-by,q+fy));
        //------------------------
        a[k] = p-bx; c[k] = p+fx+1;
        b[k] = q-by; d[k] = q+fy+1;
    }
    cout << endl << endl << endl;
    rep(i,N){
        cout << a[i] << " " << b[i] << " "
        <<  c[i] << " " << d[i] << endl;
    }
}


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

struct dijkstra{
    vector<long long> min_dst;
    vector<vector<pair<long long, long long>>> G;

    dijkstra(int n){
        min_dst.resize(n);
        G.resize(n);
    }

    void append(int u, int v, long long cost){G[u].push_back(make_pair(v, cost));}

    void run(int s){
        priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> que;
        fill(min_dst.begin(), min_dst.end(), -1);
        min_dst[s] = 0;
        que.push(make_pair(0, s));

        while(!que.empty()){
            auto p = que.top(); que.pop();
            int at = p.second;
            if(min_dst[at] < p.first){continue;}
            for(int i = 0; i < G[at].size(); i++){
                int to = G[at][i].first;
                long long cost = G[at][i].second;
                if(min_dst[to] == -1 || min_dst[at] + cost < min_dst[to]){
                    min_dst[to] = min_dst[at] + cost;
                    que.push(make_pair(min_dst[to], to));
                }
            }
        }
    }

    long long get(int at){return min_dst[at];}
};

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

    ll n, m; cin >> n >> m;

    dijkstra d(2 * n);
    zep(i, 0, m){
        ll a, b, c; cin >> a >> b >> c; a--; b--;
        d.append(a, b + n, c);
        d.append(a + n, b + n, c);
    }

    zep(i, 0, n){
        d.run(i);
        print(d.get(i + n))
    }

    return 0;
}

#include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<int,int> P;
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<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<30;
int dx[]={0,1,0,-1,0,1,-1,1,-1};
int dy[]={0,0,1,0,-1,1,-1,-1,1};

struct edge {int to;ll cost;};

const int M_N=10000;

vector<edge> G[M_N+1];
struct Dijkstra{

    ll d[M_N+1];
    void init(int n){
        fill(d,d+n,LINF);
    }

    ll dijkstra(int s){
        priority_queue<P,vector<P>,greater<P>> que;
        que.push(P(0,s));
        d[s] = 0;
        ll ret = LINF;
        while(!que.empty()){
            P p=que.top();
            que.pop();
            int v=p.second;
            if(d[v]<p.first) continue;
            for(int i=0;i<G[v].size();i++){
                edge e=G[v][i];
                if(e.to == s){
                    chmin(ret, d[v] + e.cost);
                }
                if(d[e.to]>d[v]+e.cost){
                    d[e.to]=d[v]+e.cost;
                    que.push(P(d[e.to],e.to));
                }
            }
        }
        return ret;
    }

};

int main(){
    int n,m;cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a,b,c;cin >> a >> b >> c;
        a--,b--;
        G[a].pb({b, c});
    }
    Dijkstra ds;
    for (int i = 0; i < n; i++) {
        ds.init(n);
        auto p = ds.dijkstra(i);
        if(p == LINF) cout << -1 << endl;
        else cout << p << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector< pii >
#define vpll vector< pll >
#define mpii map<int,int>
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
	if (b==0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll pow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}

int main()
{
	fast
	int n,k;
	cin>>n>>k;
	vec v(n);
	read0(v,n);
	mpll m;
	trav(it,v)m[it]++;
	vec temp[k];
	ll prev=m[0]-1;
	FOR(i,0,n-1)
	{
	    prev=min(prev,m[i]-1);
	    FOR(j,0,min((ll)k-1,prev))
	    {
	        temp[j].pb(1);
	    }
	}
	ll ans=0;
	FOR(i,0,k-1)
	{
	    ans+=s(temp[i]);
	}
	cout<<ans<<nl;
}

/*-------------必要なときに使うもの------------------*/
//#define _GLIBCXX_DEBUG
/*#include<atcoder/modint>
#include<atcoder/math>
using namespace atcoder;
using mint=modint1000000007;
using MINT=modint998244353;*/
/*#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
/*-------------------end-----------------------------*/
#include<bits/stdc++.h>
using namespace std;
/*---------------------マクロ-------------------------*/
#define int long long
#define rep(i, n) for (int i = 0; i < (long long)(n); i++)
#define stoi stoll
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define debug(x) cout<<#x<<" : "<<x<<endl
#define endl '\n'
/*------------------------end---------------------------*/
/*-----------------------型エイリアス--------------------*/
using ll = long long;
using lint=long long;
using P=pair<long long,long long>;
using vi=vector<long long>;
using vvi=vector<vi>;
using vb=vector<bool>;
using vc=vector<char>;
using vvc=vector<vc>;
using vs=vector<string>;
using vd=vector<double>;
/*------------------------end---------------------------*/
/* ----------------よく使う数字や配列-------------------- */
constexpr long long INF = LLONG_MAX/3;
constexpr long long dx[]={1,0,-1,0},dy[]={0,1,0,-1};
constexpr long long mod = 1e9 + 7;
constexpr long long MOD=998244353;
constexpr long double pi = 3.141592653589793238462643383279;
constexpr double EPS=1e-14;
/* ----------------------end-----------------------------*/
/*---------------------template------------------------*/
template<class T>inline bool chmin(T& a,T b){
  if(a>b){a=b;return true;}
  return false;
}
template<class T>inline bool chmax(T& a,T b){
  if(a<b){a=b;return true;}
  return false;
}
template<class T>inline void mainend(T x){cout<<x<<endl;exit(0);}
/*----------------------end-------------------------*/
void Main(){
  int N,K;cin>>N>>K;
  map<int,int>mp;
  int MAX=-INF;
  rep(i,N){int a;cin>>a;chmax(MAX,a);mp[a]++;}
  for(int i=0;i<=MAX;i++){
    if(!mp.count(i))mp[i]=0;
  }
  map<int,pair<set<int>,int>>ma;
  for(auto&[x,y]:mp){
    ma[y].first.insert(x);
  }
  for(auto &[x,y]:ma){
    y.second=*y.first.begin();
  }
  if(!ma.count(0)){
    ma[0].first.insert(MAX+1);
    ma[0].second=MAX+1;
  }
  int t_min=ma[0].second;
  int sum=0;
  /*for(auto[x,y]:ma){
    cout<<'%'<<x<<'%'<<endl;
    for(auto z:y.first){
      cout<<z<<' ';
    }
    cout<<endl;
  }*/

  for(int i=0;i<K;i++){
    int a;
    if(!ma.count(i))a=t_min;
    else a=ma[i].second;
    //debug(a);
    chmin(t_min,a);
    sum+=t_min;
  }
  cout<<sum<<endl;

}
signed main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<fixed<<setprecision(16);
  Main();
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it) {
	*this << ((it != d.b) ? ", " : "") << *it;
  }
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = 1e9 + 7;

int modu(ll x) {
	if (x >= mod) {
		x %= mod;
	}
	return x;
}

int main() {
	ios_base::sync_with_stdio(false);
	int h, w;
	cin >> h >> w;
	vector<string> t(h);
	rep(i, 0, h) {
		cin >> t[i];
		// t[i] = string(w, '.');
	}
	vector<vi> up(h, vi(w)), left(h, vi(w)), diag(h, vi(w));
	vector<vi> f(h, vi(w));
	f[0][0] = up[0][0] = left[0][0] = diag[0][0] = 1;
	rep(i,0,h) rep(j,0,w) {
		if (i +j == 0) {
			continue;
		}
		if (t[i][j] == '#') {
			continue;
		}
		f[i][j] = modu((ll)(i > 0 ? up[i-1][j] : 0) + (j > 0 ? left[i][j-1] : 0) + (i>0&&j>0 ? diag[i-1][j-1] : 0));
		up[i][j] = modu((i > 0 ? up[i-1][j] : 0) + f[i][j]);
		left[i][j] = modu((j > 0 ? left[i][j-1] : 0) + f[i][j]);
		diag[i][j] = modu((i>0&&j>0 ? diag[i - 1][j - 1] : 0) + f[i][j]);
	}
	cout << f[h-1][w-1] << endl;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>

using namespace std;

int main() {

   int l;
   scanf("%d", &l);

   // Answer is C(l - 1, 11)
   long long ans = 1;
   for (int i = 1; i <= 11; i++) {
      ans *= l - i;
      ans /= i;
   }
   printf("%lld\n", ans);

   return 0;
}

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;
using ll = long long;

int l;

ll dp[201][201];

ll memo(int idx, int leng);

int main()
{
    memset(dp, -1, sizeof dp);
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> l;
    cout << memo(0, 0);
}

ll memo(int idx, int leng)
{
    if (idx == 11)
    {
        if (leng < l)
            return 1;
        else
            return 0;
    }

    ll &ret = dp[idx][leng];

    if (ret != -1)
        return ret;
    ret = 0;

    for (int i = leng + 1; i < l; ++i)
        ret += memo(idx + 1, i);

    return ret;
}


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

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

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

  int max = -1;
  for(int l = 0; l < n; l++) {
    int min = a[l];
    for(int r = l; r < n; r++) {
      if(a[r] < min) {
        min = a[r];
      }
      if(min * (r - l + 1) > max) {
        max = min * (r - l + 1);
      }
    }
  }
  cout << max;
}

// code by lynmisakura. wish to be accepted!

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

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

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

#define lmd(func) auto func = [&]

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
    cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
    return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
    cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
    return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
    cout << "{" << p.first << "," << p.second << "}" << '\n';
    return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
    for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
    cin >> p.first >> p.second; return in;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

/////////////////////////////////////////////////////////////////////////////

int main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);
  //cout << fixed << setprecision(20);

  int ans = INT_MAX;

  int n;cin >> n;
  vi a(n);
  rep(i,n)cin >> a[i];

  rep(mask,(1<<(n-1))){
    int cand = 0;
    int tmp = 0;
    rep(i,n){
      tmp |= a[i];
      if(i < n-1 && (mask >> i & 1)){
        cand ^= tmp;
        tmp = 0;
      }
    }
    cand ^= tmp;
    ans = min(ans , cand);
  }

  cout << ans << '\n';

}


#include <bits/stdc++.h>
#define N 5015
#define mod 998244353

using namespace std;

typedef long long ll;

int n, m;
ll sum[N], power[N][N];

ll ksm(int a, int b) {
	return power[a][b];
}

void Init() {
	for (int i = 0; i <= m+2; i++) {
		power[i][0] = 1;
		for (int j = 1; j <= n+2; j++) {
			power[i][j] = (power[i][j - 1] * i) % mod;
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	Init();
	for (int j = 0; j <= n + 5; j++) {
		for (int i = 0; i < m; i++) {
			sum[j] = (sum[j] + ksm(i, j)) % mod;
		}
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ll res = 0;
		res = (res + (m * (ans + ksm(m - 1, i - 1)) % mod) % mod) % mod;
		for (int j = i - 1; j >= 1; j--) {
			res = (res % mod + ((ksm(m - 1, i - j - 1) * ksm(m, j - 1)) % mod * m)) % mod;
			res = ((res - (ksm(m, j - 1) * sum[i - j - 1]) % mod) % mod + mod) % mod;
		}
		ans = res;
	}
	cout << ans % mod << '\n';
	return 0;
}

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

#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>

#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
// constexpr ll mod = 1000000007;
constexpr ll mod = 998244353;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
void pt(T val) {
    cout << val << "\n";
}
template <typename T>
void pt_vll(vector<T> &v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];

        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}
ll mypow(ll a, ll n) {
    ll ret = 1;
    if(n==0) return 1;
    if(a==0) return 0;
    rep(i, n) {
        if (ret > (ll)(1e18 + 10) / a) return -1;
        ret *= a;
    }
    return ret;
}
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll dig(ll n) {//桁数
    ll res = 0;
    while(n > 0) {
        res ++;
        n /= 10;
    }
    return res;
}
vector<bool> seen;
void dfs(vvll &G, ll v){
    seen[v]=true;
    for(auto next:G[v]){
        if(seen[next]) continue;
        dfs(G,next);
    }
}

int main() {
    ll n,m,x,ans=1;
    cin>>n>>m;
    vll A(n);
    vll X(m+1,1);
    rep(i,n){
        if(i==0){
            A[0]=m;
            continue;
        }
        ll sum=0;
        rep(j,m){
            A[i]+=mod+A[i-1]+modpow(m,i,mod)-X[j];
            A[i]%=mod;
            X[j]=(m-j-1)*X[j]+modpow(m,i,mod);
            X[j]%=mod;
        }
    }
    cout<<A[n-1]%mod<<endl;
}


#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

#define edge pair<int,int>
#define x first
#define y second

const int N=1e5+5;
int n,m,ans[N];
vector<edge> g[N];

void dfs(int x){
	for(edge e:g[x]) if(!ans[e.x]){
		ans[e.x]=ans[x]==e.y?1+(ans[x]==1):e.y;
		dfs(e.x);
	}
}

signed main(){
	read(n,m);
	for(int i=1,x,y,z;i<=m;i++){
		read(x,y,z);
		g[x].emplace_back(y,z);
		g[y].emplace_back(x,z);
	}
	ans[1]=1;
	dfs(1);
	for(int i=1;i<=n;i++) write(ans[i]),puts("");
}

#include <bits/stdc++.h>

#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
// template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
// template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}

int main() {
  // std::cout << std::fixed << std::setprecision(10);

  ll N;
  scanf("%lld", &N);

  vector<pair<ll, string>> list;
  rep(i, N) {
    string name;
    cin >> name;
    ll h;
    scanf("%lld", &h);
    list.emplace_back(make_pair(h, name));
  }

  sort(list.begin(), list.end(), greater<pair<ll, string>>());

  cout << (list[1].second) << endl;

  // cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define endl '\n'

ll dp[105][2];

int main(){
    ios_base::sync_with_stdio(false);
    ll a, b, x, y;
    cin >> a >> b >> x >> y;

    if(a == b) cout << x, exit(0);
    else {
        if(a > b) swap(a, b), b--;
        int k = abs(a-b);
        cout << x + min(y, 2*x) * k << endl;
    }

    return 0;
}

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

int main()
{
	int N, x_0, y_0, x_N, y_N;
	cin >> N >> x_0 >> y_0 >> x_N >> y_N;

	double R_x, R_y;
	R_x = ((double)x_0 + (double)x_N) / 2;
	R_y = ((double)y_0 + (double)y_N) / 2;
	double x, y;
	x = x_0 - R_x;
	y = y_0 - R_y;

	double theta,A_sin,A_cos;
	const double PI = 3.14159265358979323846;
	theta = 2 * PI/N;
	A_cos = cos(theta);
	A_sin = sin(theta);
	double A_x, A_y;
	A_x = A_cos * x - y * A_sin;
	A_y = A_sin * x + A_cos * y;

	double Ans_x, Ans_y;
	Ans_x = R_x + A_x;
	Ans_y = R_y + A_y;
	cout << Ans_x <<" "<< Ans_y;

}


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

typedef long long LL;
LL b[300];
int p = 200;

int main()
{
    int n, a;
    cin >> n;
    memset(b, 0, sizeof b);
    for(int i = 0; i < n; i++){
        cin >> a; a = a % p;
        b[a]++;
    }
    LL ans = 0;
    for(int i = 0; i < 205; i++){
        if(b[i] != 0 && b[i] != 1){
            LL k = b[i] * (b[i] - 1) / 2;
            ans += k;
        }
    }
    cout << ans << "\n";
    return 0;
}

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

#define mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 500005

int n;
char s[MAXN], t[MAXN];
vii v;
int ans;

int main() {
	scanf("%d", &n);
	scanf(" %s", s);
	scanf(" %s", t);
	int cnt = 0;
	int lo = 0;
	REP (i, 0, n) {
		if (s[i] == t[i]) continue;
		if (s[i] == '1' && t[i] == '0') {
			cnt++;
		} else if (s[i] == '0' & t[i] == '1') {
			cnt--;
		}
		if (cnt == 0) {
			v.pb(lo, i);
			lo = i + 1;
		}
	}
	if (cnt != 0) {
		printf("-1\n");
		return 0;
	}
	for (auto [lo, hi] : v) {
		while (s[lo] == t[lo]) lo++;
		while (s[hi] == t[hi]) hi--;
		if (s[lo] == '0') {
			assert(t[lo] == '1');
			reverse(s + lo, s + hi + 1);
			reverse(t + lo, t + hi + 1);
		}
		//printf("%d %d\n", lo, hi);
		//REP (i, lo, hi + 1) {
			//printf("%c", s[i]);
		//}
		//printf("\n");
		//REP (i, lo, hi + 1) {
			//printf("%c", t[i]);
		//}
		//printf("\n\n");
		REP (i, lo, hi + 1) {
			if (s[i] == '0') ans++;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <chrono>
#include <queue>
#include <tuple>
#include <set>
using namespace std;

long long seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
unsigned int rx = seed, ry = seed >> 12, rz = seed >> 24;
inline unsigned int r()
{
	unsigned int t;
	rx ^= rx << 16; rx ^= rx >> 5; rx ^= rx << 1;
	t = rx; rx = ry; ry = rz; rz = t ^ rx ^ ry;
	return t;
}

int range(int a, int b)
{
	return a + r() % (b - a + 1);
}

double z()
{
	return (r() + 0.5) / ((double)4294967296);
}

int N, X[202], Y[202], R[202];

struct rect{
	int x1, y1, x2, y2;
}best[202], now[202], nxt[202];
int bestScore;

bool xIntersect(rect &a, rect &b)
{
	return max(a.x1, b.x1) < min(a.x2, b.x2);
}

bool yIntersect(rect &a, rect &b)
{
	return max(a.y1, b.y1) < min(a.y2, b.y2);
}

int score(rect *r)
{
	double pt = 0;
	for (int i = 0; i < N; i++){
		if (r[i].x1 == r[i].x2 || r[i].y1 == r[i].y2 || r[i].x1 < 0 || r[i].y1 < 0 || r[i].x2 > 10000 || r[i].y2 > 10000){
			return -1;
		}
		if (r[i].x1 <= X[i] && X[i] < r[i].x2 && r[i].y1 <= Y[i] && Y[i] < r[i].y2){
			int area = (r[i].x2 - r[i].x1) * (r[i].y2 - r[i].y1);
			double rat = 1 - 1. * min(R[i], area) / max(R[i], area);
			pt += (1 - rat * rat);
		}
		else return -1;
	}
	pt /= N;
	return (pt * 1e9 + 0.5);
}

void proc()
{
	auto start = std::chrono::system_clock::now();

	scanf ("%d", &N);
	for (int i = 0; i < N; i++){
		scanf ("%d %d %d", &X[i], &Y[i], &R[i]);
		tie(now[i].x1, now[i].y1) = tie(now[i].x2, now[i].y2) = tie(X[i], Y[i]);
		now[i].x2++; now[i].y2++;
	}

	const double TL = 4.85;
	while (1){
		double rat = std::chrono::duration<double>(std::chrono::system_clock::now() - start).count() / TL;
		if (rat >= 1) break;

		int step = 1e7 / N;
		double reduce = pow(1e-7, 1. / step);
		double temp = 1e5;
		int mv = N / 4;
		for (int i = 0; i < N; i++) {
			int x1 = X[i], x2 = X[i] + 1, y1 = Y[i], y2 = Y[i] + 1;
			x1 = range(now[i].x1, x1);
			x2 = range(x2, now[i].x2);
			y1 = range(now[i].y1, y1);
			y2 = range(y2, now[i].y2);
			now[i] = { x1, y1, x2, y2 };
		}

		int nowScore = score(now);
		for (int att = 0; att < step; att++, temp *= reduce){
			int i = range(1, N) - 1;

			for (int j = 0; j < N; j++) nxt[j] = now[j];
			int t = range(0, 3), m1 = range(1, range(1, mv)), m2 = range(1, m1);
			if (t == 0){
				nxt[i].x1 -= m1;
				if (range(0, 1)) nxt[i].x2 -= m2;
			}
			if (t == 1){
				nxt[i].x2 += m1;
				if (range(0, 1)) nxt[i].x1 += m2;
			}
			if (t == 2){
				nxt[i].y1 -= m1;
				if (range(0, 1)) nxt[i].y2 -= m2;
			}
			if (t == 3){
				nxt[i].y2 += m1;
				if (range(0, 1)) nxt[i].y1 += m2;
			}

			for (int j = 0; j < N; j++) if (j != i){
				if (xIntersect(nxt[i], nxt[j]) && yIntersect(nxt[i], nxt[j])){
					if (t == 0) nxt[j].x2 = nxt[i].x1;
					if (t == 1) nxt[j].x1 = nxt[i].x2;
					if (t == 2) nxt[j].y2 = nxt[i].y1;
					if (t == 3) nxt[j].y1 = nxt[i].y2;
				}
			}

			int nxtScore = score(nxt);
			if (nxtScore <= 0) continue;
			if (nowScore <= nxtScore || exp((nxtScore - nowScore) / temp) >= z()){
				nowScore = nxtScore;
				for (int j = 0; j < N; j++) now[j] = nxt[j];
			}
		}

		if (bestScore < nowScore){
			bestScore = nowScore;
			for (int j = 0; j < N; j++) best[j] = now[j];
		}
		for (int j = 0; j < N; j++) now[j] = best[j];
	}

	for (int i = 0; i < N; i++) printf ("%d %d %d %d\n", best[i].x1, best[i].y1, best[i].x2, best[i].y2);
}

int main()
{
	proc();
}

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int main() {
    int K; cin >> K;
    ll N, M; cin >> N >> M;
    vector<ll> A(K);
    for(auto &e : A) cin >> e;

    ll ub = 1LL << 60, lb = -1;
    while(ub - lb > 1) {
        ll mid = (ub + lb) / 2;
        // abs(BN - AM) <= mid
        // -mid <= BN - AM <= mid
        // [Lsum, Rsum]
        ll Lsum = 0, Rsum = 0;
        for(int i=0; i<K; i++) {
            ll L = max(0LL, (A[i] * M - mid + N - 1) / N);
            ll R = (mid + A[i] * M) / N;
            Lsum += L, Rsum += R;
        }
        if(Lsum <= M and M <= Rsum) ub = mid;
        else lb = mid;
    }

    {
        vector<ll> L(K), R(K);
        for(int i=0; i<K; i++) {
            ll l = max(0LL, (A[i] * M - ub + N - 1) / N);
            ll r = (ub + A[i] * M) / N;
            L[i] = l, R[i] = r;
        }
        vector<ll> ans = L;
        ll sum = accumulate(ans.begin(), ans.end(), 0LL);
        for(int i=0; i<K; i++) {
            ll rem = min(R[i] - ans[i], M - sum);
            ans[i] += rem;
            sum += rem;
        }
        for(int i=0; i<K; i++) {
            printf("%lld%c", ans[i], " \n"[i + 1 == K]);
        }
    }

    return 0;
}


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;

struct Tree {
	int _size;
	vector<vector<int>> _edges;
	vector<int> _done;
	vector<int> _vis;
	vector<int> _color;
	vector<int> _shifts;
	vector<int> _nodes;

	Tree(int size) : _size(size), _edges(size), _done(size), _vis(size) { }

	void build_edges(const vector<int>& a, const vector<int>& b) {
		for (int i = 0; i < a.size(); ++i) {
			_edges[a[i]].emplace_back(b[i]);
			_edges[b[i]].emplace_back(a[i]);
		}
	}

	void dfs1(int n) {
		_done[n] = 1;
		for (auto next : _edges[n]) {
			if (_done[next] <= 0) {
				dfs1(next);
			}
		}
		_nodes.emplace_back(n);
	}

	bool dfs2(int n) {
		_vis[n] = 1;
		for (auto next : _edges[n]) {
			if (_vis[next] <= 0) {
				_color[next] = (_color[n] + _shifts[next]) % 3;
				if (!dfs2(next)) {
					return false;
				}
			}
			if (_color[n] == _color[next]) {
				return false;
			}
		}
		return true;
	}

	LL solve(int M, std::vector<int>& A, std::vector<int>& B) {
		LL ans = 1;
		for (int node = 0; node < _size; ++node) {
			if (_done[node]) {
				continue;
			}
			_nodes.clear();
			dfs1(node);
			_nodes.pop_back();
			_shifts = vector<int>(_size);
			_color = vector<int>(_size, -1);
			int sum = 0, bm = 1 << _nodes.size();
			for (int b = 0; b < bm; ++b) {
				for (int i = 0; i < _nodes.size(); ++i) {
					_shifts[_nodes[i]] = ((1 << i) & b) ? 1 : 2;
				}
				_color[node] = 0;
				_vis = vector<int>(_size);
				if (dfs2(node)) {
					++sum;
				}
			}
			ans *= sum * 3;
		}
		return ans;
	}
};

LL solve(int N, int M, std::vector<int> &A, std::vector<int> &B) {
	Tree tree(N);
	tree.build_edges(A, B);
	return tree.solve(M, A, B);
}

int main() {
	int N, M;
	std::cin >> N >> M;
	std::vector<int> A(M), B(M);
	for (int i = 0; i < M; i++) {
		std::cin >> A[i] >> B[i];
		--A[i], --B[i];
	}
	cout << solve(N, M, A, B) << endl;
	return 0;
}

/**
*    author:
*    created: 16.11.2020 06:42:51
**/

#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;
using ll = long long;
const int INF = 1001001001;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1};
typedef vector<int> vi;
typedef vector<vi> vvi;

#define rep(i,n) for(int i = 0; i < (n); i++)
#define repn(i,n) for(int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

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

const double PI = acos(-1);
const int mod = 1000000007;
class mint {
  long long x;
  public:
  mint(long long x=0) : x((x%mod+mod)%mod) {}
  mint operator-() const {
    return mint(-x);
  }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const  mint& a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint& a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint& a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint& a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    return a;
  }
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint& a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint& a) const {
    mint res(*this);
    return res/=a;
  }

  friend ostream& operator<<(ostream& os, const mint& m){
    os << m.x;
    return os;
  }
};
#pragma endregion

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i,h) cin >> s[i];
  vector<vector<mint>> dp(h,vector<mint>(w,0));
  vector<vector<mint>> x(h,vector<mint>(w,0));
  vector<vector<mint>> y(h,vector<mint>(w,0));
  vector<vector<mint>> z(h,vector<mint>(w,0));
  dp[0][0] = 1;
  rep(i,h)rep(j,w){
    if(i==0 && j==0) continue;
    if(s[i][j]=='#')continue;
    if(j>0)x[i][j] = (x[i][j-1]+dp[i][j-1]);
    if(i>0)y[i][j] = (y[i-1][j]+dp[i-1][j]);
    if(i>0&&j>0)z[i][j] = (z[i-1][j-1]+dp[i-1][j-1]);
    dp[i][j] = (x[i][j]+y[i][j]+z[i][j]);
  }
  cout << dp[h-1][w-1] << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=2e5+10;
const int inf=0x7fffffff;
vector<int> adj[N];
vector<int> mst;
LL n,m,k;
int fa[N],arr[N];
int findfa(int x)
{
    if(fa[x]==x)    return fa[x];
    else return fa[x]=findfa(fa[x]);
}
void merge(int x,int y)
{
    int fx=findfa(x),fy=findfa(y);
    if(fx!=fy)
    {
        if(rand()%2)
            fa[fy]=fx;
        else fa[fx]=fy;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    for(int i=1;i<=N;i++)   fa[i]=i;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    bool f=0;
    for(int i=1;i<=n/2+1;i++)
    {
        if(arr[i]!=arr[n-i+1])  f=1;
    }
    if(f==0)    cout<<0<<endl;
    else
    {
        int cnt=0,aa=0;
        set<pair<int,int>> st;
        for(int i=1;i<=n/2;i++)
        {
            if(arr[i]!=arr[n-i+1]&&st.count(make_pair(arr[i],arr[n-i+1]))==0)
            {
                st.insert(make_pair(arr[i],arr[n-i+1]));
                aa++;
                if(findfa(arr[i])==findfa(arr[n-i+1]))  cnt++;
                merge(arr[i],arr[n-i+1]);
            }
        }
        cout<<aa-cnt<<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 F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(c) c.begin(), c.end()
#define TR(c, it) for (auto it = c.begin(); it != c.end(); it++)
#define SZ(c) int(c.size())
#define present(c, e) (c.find(e) != c.end())
#define cpresent(c, e) (find(ALL(c),e) != c.end())
#define ffasstt() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define pii(T) pair<T, T>
#define v1(T) vector<T>
#define v2(T) vector< vector<T> >
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class T> void inline Scan(v1(T) &arr){ for(auto &i:arr) cin >> i; }
template<class T> void inline Print(v1(T) &arr){ for(auto &i:arr) cout << fixed << setprecision(7) << i << " "; }

const int N = 200010;

vector<int>adj[N];
v1(bool) vis(N, 0);
ll cnt = 0;
ll ans = 0;
void dfs(int s){
	vis[s] = 1;
	cnt++;
	for(int t: adj[s]){
		if(!vis[t])
			dfs(t);
	}
}
void solve(){
	ll n;
	cin >> n;
	v1(int) arr(n);
	Scan(arr);
	REP(i, 0, n - 1){
		if(arr[i] != arr[n - 1 - i]){
			adj[arr[i]].PB(arr[n - 1 - i]);
			adj[arr[n - 1 - i]].PB(arr[i]);
		}
	}
	REP(i, 0, n - 1){
		if(!vis[arr[i]]){
			cnt = 0;
			dfs(arr[i]);
			ans += (cnt - 1);
		}
	}
	cout << ans;
}
int main()
{
	ffasstt();
	int t = 1;
	// cin >> t;
	while(t--){
		solve();
	}
	cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC <<" secs"<<endl;
	return 0;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pi;

const int maxn = 2e5 + 5;

int n;
ll a[2 * maxn];

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1;i <= 2 * n;i++){
        cin >> a[i];
    }
    multiset<ll> s;
    ll ans = 0;
    for(int i = n;i >= 1;i--){
        s.insert(a[i]);
        ans += a[i];
        if(a[2 * n + 1 - i] > *s.begin()){
            ans += (a[2 * n + 1 - i] - (*s.begin()));
            s.erase(s.begin());
            s.insert(a[2 * n + 1 - i]);
        }
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

constexpr int MOD = 1e9 + 7;

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<long long> A(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  long long ans = 1;

  for (int i = 0; i < N; i++) {
    ans *= A[i + 1] - A[i] + 1;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}