#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <cassert>
//#include <numeric>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;

const int max_comb=210000;
vector<ll> fac(max_comb+1); //n! (mod M)
vector<ll> ifac(max_comb+1); //k!^(-1) (mod M)

ll mpow(ll x, ll n){ //x^n(mod M)
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % MOD;
        x = x*x % MOD;
        n = n >> 1;
    }
    return ans;
}

ll minv(ll x){
    return mpow( x, MOD-2 );
}

ll comb(int a, int b){     // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
    if(a == 0 && b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b]* ifac[b] % MOD;
    return tmp * fac[a] % MOD;
}

ll perm(int a, int b){     // P(a,b) = a! * (a-b)!^(-1)
    if(b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b] % MOD;
    return tmp * fac[a] % MOD;
}

void pre_comb()
{
    fac[0] = 1;
    ifac[0] = 1;
    for(int i = 0; i<max_comb; i++){
        fac[i+1] = fac[i]*(i+1) % MOD; // n!(mod M)
        ifac[i+1] = ifac[i]*minv(i+1) % MOD; // k!^(-1) (mod M)
    }
    return;
}

void solve()
{
    int n, K;
    scanf("%d%d", &n, &K);
    pre_comb();

    ll ans0 = 0;
    int i;
    for (i = 0; i < n-K; i++) {
        ll tmp = mpow(n - K - i, n);
        ll tmp2 = comb(n - K, n - K - i);
        tmp = tmp * tmp2 % MOD;
        if (i % 2) tmp = (-tmp + MOD) % MOD;
        ans0 = (ans0 + tmp) % MOD;
    }
    ans0 = ans0 * comb(n, n - K) % MOD;
    if(0=K) ans0 = ans0 * 2 % MOD;
    printf("%lld\n", ans0);

    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define N 1000010
using namespace std;
const ll p=1000000007;
ll w[N],q[N],v[N];
int m,k,n;
ll C(int x,int y){
if(y>x)return 0;
return w[x]*q[y]%p*q[x-y]%p;}
inline ll A(int x,int y){
if(y>x)return 0;
return w[x]*q[x-y]%p;}
ll a(ll x,ll y){
if(y<0)return 0;ll ans=1;
for(;y;x=x*x%p,y>>=1)if(y&1)ans=ans*x%p;
return ans;}
int main(){
scanf("%d%d%d",&m,&K,&n);
int i,j,L;
w[0]=w[1]=q[0]=q[1]=v[0]=v[1]=1;
for(i=2;i<=K;i++){w[i]=w[i-1]*i%p;
v[i]=1ll*(p-p/i)*v[p%i]%p;
q[i]=q[i-1]*v[i]%p;}
ll ans=0;
for(l=1;l<=k&&n+l<=m;l++){
if(n-l>=2)(ans+=a(k,n-l-2)*(1ll*k*k%p*A(k,l)%p*A(k,l)%p-A(k,l+1)*A(k,l+1)%p+p)%p)%=p;
else if(n-L==1)(ans+=(1ll*k*A(k,l)%p*A(k,l)%p-A(k,l+1)*A(k-1,l)%p+p)%p)%=p;
else (ans+=(1ll*A(k,l-n)*A(k-(l-n),n)%p*A(k-(l-n),n)%p-A(k,l-n+2)*A(k-(l-n+2),n-1)%p*A(k-(l-n+2),n-1)%p)%p)%=p;
}printf("%lld\n",(ans%p+p)%p);
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const double PI=acos(-1);
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define per(i,a,b) for (int i=(a);i>=(b);i--)
#define Rep(i,a,b) for (int i=(a);i<(b);i++)
#define Per(i,a,b) for (int i=(a);i>(b);i--)
#define travel_set(it,a) for (set<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define travel_map(it,a) for (map<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define travel_vec(it,a) for (vector<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define p(x) cout << x << endl;
#define def cout << "find" << endl;
#define what_is(x) cout << #x << " is " << x << endl;
template <class T>T sqr(T x){return x*x;}
template <class T>T alex(T x){return (x>0)?x:-x;}
template <class T>void read(T&x){
	x=0;T f=1;char ch=getchar();
	while ((ch<48||ch>57)&&ch!=45)ch=getchar();
	if (ch==45)f=-1,ch=getchar();
	while (ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();
	x*=f;
}

//-------------------------------------------------head-------------------------------------------------

#define maxn 1000005
#define mod 1000000007
int sum0[maxn],sum2[maxn],sum4[maxn],sum6[maxn];
ll n;
int power(int a,int b,int p){
	int s=1,w=a;
	while (b){
		if (b&1)s=(ll)s*w%p;
		w=(ll)w*w%p;b>>=1;
	}
	return s;
}
int f(ll n){
	int a=n,b=n+1,c=n+2;
	if (a%2==0)a/=2;else if (b%2==0)b/=2;
	if (a%3==0)a/=3;else if (b%3==0)b/=3;else if (c%3==0)c/=3;
	a%=mod;b%=mod;c%=mod;return (ll)a*b%mod*c%mod;
}
int cnt_all_point(int sqn){
	int res=0;
	rep(i,0,sqn){
		(res+=(int)sqrt(n-(ll)i*i))%=mod;
	}
	return res;
}
int ans;
int work1(int sqn){
	int res=0;
	rep(i,0,sqn){
		int cnt=(int)sqrt(n-(ll)i*i);
		res=(res+(ll)i*i%mod*cnt%mod)%mod;
		res=(res+sum2[cnt])%mod;
	}
	return res;
}
int work2(int sqn){
	int res=0;
	rep(i,0,sqn){
		int cnt=(int)sqrt(n-(ll)i*i);
		res=(res+(ll)i*i%mod*i%mod*i%mod*cnt%mod)%mod;
		res=(res+(ll)2*i*i%mod*sum2[cnt]%mod)%mod;
		res=(res+sum4[cnt])%mod;
	}
	return res;
}
int work3(int sqn){
	int res=0;
	rep(i,0,sqn){
		int cnt=(int)sqrt(n-(ll)i*i);
		res=(res+(ll)i*i%mod*i%mod*i%mod*i%mod*i%mod*cnt%mod)%mod;
		res=(res+(ll)3*i*i%mod*i%mod*i%mod*sum2[cnt]%mod)%mod;
		res=(res+(ll)3*i*i%mod*sum4[cnt]%mod)%mod;
		res=(res+sum6[cnt])%mod;
	}
	return res;
}
int main(){
	scanf("%lld",&n);int sqn=(int)sqrt(n);
// printf("%d\n",sqn);
	sum0[0]=sum2[0]=sum4[0]=sum6[0]=0;
	Rep(i,1,maxn){
		sum0[i]=(sum0[i-1]+power(i,0,mod))%mod;
		sum2[i]=(sum2[i-1]+power(i,2,mod))%mod;
		sum4[i]=(sum4[i-1]+power(i,4,mod))%mod;
		sum6[i]=(sum6[i-1]+power(i,6,mod))%mod;
// if (i<=10)printf("%d %d %d %d %d\n",i,sum0[i],sum2[i],sum4[i],sum6[i]);
	}
	int all_point=cnt_all_point(sqn);
// printf("%d\n",all_point);
// printf("%d\n",f(n));
// ans=(ll)all_point*f(n)%mod;
// printf("%d\n",ans);
// printf("%d %d %d\n",work3(sqn),work2(sqn),work1(sqn));

	ans=(ans-2*work3(sqn)%mod+mod)%mod;
	ans=(ans+(n*3%mod+6)*work2(sqn)%mod)%mod;
	ans=(ans-(n*3%mod+4)*work1(sqn)%mod)%mod;
	ans=(ll)ans*power(6,mod-2,mod)%mod;
	ans=((ll)all_point*f(n)%mod-ans+mod)%mod;
// printf("%d\n",ans);
	ans=(ll)ans*4%mod;ans=(ans+f(n))%mod;
	printf("%d\n",ans);return 0;
}


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

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const ll Mod = 1000000007LL;
const int Maxn = 2e3 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

vector<ll> G[Maxn], H[Maxn];
pll exf[Maxn * Maxn];
pll exg[Maxn * Maxn];

ll n, m1, m2;

ll neg(ll a){
	return (a > n ? a - n : a + n);
}

bitset<Maxn> bt, vis[Maxn];
ll mk[Maxn];
vector<ll> top;

void DFS(ll u){
	mk[u] = 1;
	vis[u][u] = 1;
	for(auto adj : G[u]){
		if(!mk[adj]) DFS(adj);
		vis[u] |= vis[adj];
	}
	top.pb(u);
}

int com[Maxn];

void dfs(ll u, ll c){
	bt |= vis[u];
	com[u] = c;
	mk[u] = 1;
	for(auto adj : H[u]){
		if(!mk[adj]) dfs(adj, c);
	}
	vis[u] |= bt;
}

ll ans[Maxn];

void print(){
	memset(mk, 0, sizeof mk); top.clear();
	for(int i = 1; i <= n + n; i++) if(!mk[i]) DFS(i);

	reverse(all(top));
	memset(mk, 0, sizeof mk);
	int c = 1;
	for(auto x : top){
		if(!mk[x]) dfs(x, c++);
	}

	bool val = true;
	for(int i = 1; i <= n; i++) val &= (com[i] != com[neg(i)]);
	assert(val);

	reverse(all(top));
	memset(ans, -1, sizeof ans);
	for(auto x : top){
		if(ans[min(x, neg(x))] != -1) continue;
		ans[min(x, neg(x))] = (x < neg(x) ? 1 : 0);
	}

	bool F = true;
	ll u, v;
	for(int i = 0; i < m1; i++){
		u = exf[i].F;
		v = exf[i].S;
		F &= ( (v > n ? 1 - ans[neg(v)] : ans[v]) || (u > n ? 1 - ans[neg(u)] : ans[u]));
	}
	assert(!F);
	bool G = true;
	for(int i = 0; i < m2; i++){
		u = exg[i].F;
		v = exg[i].S;
		G &= ( (v > n ? 1 - ans[neg(v)] : ans[v]) || (u > n ? 1 - ans[neg(u)] : ans[u]));
	}
	assert(G);
	for(int i = 1; i <= n; i++) cout << ans[i] << ' ';
	exit(0);
}

void solve(){
	for(int i = 0; i < Maxn; i++){
		G[i].clear();
		H[i].clear();
		vis[i] &= vis[0];
	}
	ll u, v;
	for(int i = 0; i < m2; i++){
		u = exg[i].F;
		v = exg[i].S;

		G[neg(u)].pb(v);
		G[neg(v)].pb(u);
		H[v].pb(neg(u));
		H[u].pb(neg(v));
	}
	memset(mk, 0, sizeof mk); top.clear();
	for(int i = 1; i <= n + n; i++) if(!mk[i]) DFS(i);
	reverse(all(top));

	memset(mk, 0, sizeof mk);
	int c = 1;
	for(auto x : top){
		if(!mk[x]) bt = vis[0], dfs(x, c++);
	}

	bool val = true;
	for(int i = 1; i <= n; i++) val &= (com[i] != com[neg(i)]);
	if(!val) return ;
	for(int i = 0; i < m1; i++){
		u = exf[i].F;
		v = exf[i].S;
		if(vis[neg(u)][u] || (vis[neg(u)][v] && vis[neg(v)][u])) continue;
		if(vis[neg(v)][v] || (vis[neg(v)][u] && vis[neg(u)][v])) continue;
		G[u].pb(neg(u));
		H[neg(u)].pb(u);

		G[v].pb(neg(v));
		H[neg(v)].pb(v);
		print();
	}
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m1 >> m2;
	ll u, v;
	for(int i = 0; i < m1; i++){
		cin >> u >> v;
		if(u < 0) u = n - u;
		if(v < 0) v = n - v;
		exf[i] = {u, v};
	}

	for(int i = 0; i < m2; i++){
		cin >> u >> v;
		if(u < 0) u = n - u;
		if(v < 0) v = n - v;
		exg[i] = {u, v};
	}
	solve();
	swap(m1, m2);
	swap(exf, exg);
	solve();
	cout << "SIMILAR\n";
	return 0;
}


#include <cstdio>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;

#define FOR(i,a,b) for(int i=a;i<int(b);i++)
#define SZ(a) ((int)(a).size())
#define MOD 998244353

struct RS {
	int wp; // with parent, take
	int nt; // with parent, do not take
	int np; // without parent
};

int N;
VVI adj;
vector<RS> rss;

void dfs(int n, int p) {
	VI& aj = adj[n];
	if (p != -1) aj.erase(find(aj.begin(), aj.end(), p));
	RS& rs = rss[n];
	if (aj.empty()) {
		rs.wp = 1;
		rs.nt = 0;
		rs.np = 1;
		return;
	}
	for (int m : aj) dfs(m, n);
	VI pfx(aj.size()), sfx(aj.size());
	pfx[0] = 1;
	FOR(i,1,aj.size()) {
		RS const& rsm = rss[aj[i-1]];
		int x = rsm.nt + rsm.np;
		x %= MOD;
		pfx[i] = int((LL(pfx[i-1])*x)%MOD);
	}
	sfx[SZ(aj)-1] = 1;
	for (int i = SZ(aj)-2; i >= 0; i--) {
		RS const& rsm = rss[aj[i+1]];
		int x = rsm.nt + rsm.np;
		x %= MOD;
		sfx[i] = int((LL(sfx[i+1])*x)%MOD);
	}
	rs.wp = 1;
	rs.nt = 0;
	LL allnp = 1;
	FOR(i,0,SZ(aj)) {
		RS const& rsm = rss[aj[i]];
		int x = rsm.nt + rsm.np;
		rs.wp = int((LL(rs.wp) * x)%MOD);
		LL nt = rsm.wp;
		nt = (nt * pfx[i]) % MOD;
		nt = (nt * sfx[i]) % MOD;
		rs.nt = int((rs.nt + nt) % MOD);
		allnp = (allnp * rsm.np) % MOD;
	}
	rs.np = (rs.nt + allnp) % MOD;
	//printf("Called %d wp=%d nt=%d np=%d\n", n+1, rs.wp, rs.nt, rs.np);
}

int main() {
	scanf("%d", &N);
	adj.resize(N);
	FOR(a,0,N-1) {
		int u, v;
		scanf("%d%d", &u, &v); --u; --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	rss.resize(N);
	dfs(0, -1);
	printf("%d\n", rss[0].np);
}


#include <assert.h>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int INF = 1e9;
int n, m;
int chests[10];
int keys[10];
int costs[10][10];
int ans = INF;
struct state {
    vector<int> a;
    int i;
    int j;
    int r;

    bool operator==(const state& other)
    {
        return i == other.i && j == other.j && a == other.a;
    };
};
int convert(state tmp)
{
    int res = 0;
    for (auto& e : tmp.a) {
        assert(e < 7);
        res = res * 7 + e;
    }
    assert(tmp.r < 7);
    res = res * 7 + tmp.j;
    assert(tmp.i < 7);
    res = res * 7 + tmp.r;
    assert(tmp.j < 7);
    res = res * 7 + tmp.i;
    return res;
}
state convertBack(int index)
{
    state res;
    res.i = index % 7;
    index /= 7;
    res.r = index % 7;
    index /= 7;
    res.j = index % 7;
    index /= 7;
    for (int i = 0; i < n; ++i) {
        res.a.push_back(index % 7);
        index /= 7;
    }
    std::reverse(res.a.begin(), res.a.end());
    return res;
}
int dp[40353607] = { 0 };
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> chests[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> keys[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> costs[i][j];
        }
    }
    memset(dp, 0xff, sizeof(dp));
    dp[0] = 0;
    for (int i = 0; i < 40353607; ++i) {
        if (dp[i] < 0)
            continue;
        state curState = convertBack(i);
        if (curState.j == m) {
            continue;
        }
        /*
        cout << "fuck ";
        for (auto cur : curState.a)
            cout << cur << ' ';
        cout << endl;
        cout << curState.i << ' ' << curState.j << ' ' << curState.r << endl;
        */
        for (int flow = 0; flow < 5; ++flow) {
            auto nextState = curState;
            if (flow + curState.a[curState.i] > chests[curState.i] || flow + curState.r > keys[curState.j]) {
                continue;
            }
            int added = flow == 0 ? 0 : costs[curState.i][curState.j];
            nextState.r = flow + curState.r;
            nextState.a[curState.i] += flow;
            nextState.i++;
            if (nextState.i == n) {
                nextState.i = 0;
                nextState.j++;
                nextState.r = 0;
            }
            auto index = convert(nextState);
            dp[index] = (dp[index] == -1 || dp[index] > dp[i] + added) ? dp[i] + added : dp[index];
            auto isFinal = [&](state s) -> bool {
                for (int i = 0; i < s.a.size(); ++i) {
                    if (s.a[i] != chests[i]) {
                        return false;
                    }
                }
                return true;
            };
            if (isFinal(nextState)) {
                ans = min(ans, dp[index]);
            }
        }
    }
    if (ans == INF)
        cout << -1 << endl;
    else
        cout << ans << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <string>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <cmath>
#include <queue>
#include <deque>
#include <numeric>
#include <random>
#define ll long long
#define ld long double
#define null NULL
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;

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(const T &x, ostream &out = cerr) {
	output(all(x), out);
}

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

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

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

mt19937 rng(time(NULL));

struct hasher {
	int MOD, p = 0;

	hasher(int _MOD) {
		MOD = _MOD;
		while (p < 1000) {
			p = rng() % MOD;
		}
	}

	int n;
	vector<int> deg, h;

	int mul(int a, int b) {
		return (1LL * a * b) % MOD;
	}

	void load(vector<int> v) {
		n = v.size();
		deg.resize(n + 1);
		h.resize(n + 1);
		deg[0] = 1;
		for (int i = 1; i <= n; ++i) {
			deg[i] = mul(deg[i - 1], p);
		}
		for (int i = n - 1; i >= 0; --i) {
			h[i] = (mul(h[i + 1], p) + v[i]) % MOD;
		}
	}

	int get_hash(int l, int r) {
		return (h[l] - mul(h[r + 1], deg[r - l + 1]) + MOD) % MOD;
	}
};

const ll MOD1 = 1e9 + 7;
const ll MOD2 = 998244353;
const int mx = 2e5 + 228;
int n, s[mx], r0[mx], l0[mx], zero_id[mx], zero_pos_by_id[mx];
vector<int> mid_data;
hasher H1(MOD1), H2(MOD2);

pair<int, int> get_hash(int l, int r) {
	return {H1.get_hash(l, r), H2.get_hash(l, r)};
}

void read() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		char c;
		cin >> c;
		s[i] = c - '0';
	}
}

void gen_lr0() {
	int last_0 = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 0) {
			last_0 = i;
		}
		l0[i] = last_0;
	}
	last_0 = n;
	for (int i = n - 1; i >= 0; --i) {
		if (s[i] == 0) {
			last_0 = i;
		}
		r0[i] = last_0;
	}
	fill(zero_id, zero_id + n, -1);
	int cur_zero = 0;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 0) {
			zero_pos_by_id[cur_zero] = i;
			zero_id[i] = cur_zero++;
		}
	}
	for (int i = 1; i < cur_zero; ++i) {
		mid_data.push_back((zero_pos_by_id[i] - zero_pos_by_id[i - 1] - 1) % 2);
	}
	H1.load(mid_data);
	H2.load(mid_data);
}

struct data {
	int l, r, b1, b2;
};

data get(int l, int r) {
	int left_0 = r0[l], right_0 = l0[r];
	if (left_0 > right_0) {
		return {0, -2, (r - l + 1) % 2, (r - l + 1) % 2};
	} else {
		return {zero_id[left_0], zero_id[right_0] - 1, (left_0 - l) % 2, (r - right_0) % 2};
	}
}

int equal(data a, data b) {
	if (a.b1 != b.b1 || a.b2 != b.b2) {
		return 0;
	}
	if (a.r - a.l != b.r - b.l) {
		return 0;
	}
	if (a.r - a.l == -2 || a.r - a.l == -1) {
		return 1;
	}
	return get_hash(a.l, a.r) == get_hash(b.l, b.r);
}

void process() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int l1, r1, l2, r2, len;
		cin >> l1 >> l2 >> len;
		l1--;
		l2--;
		r1 = l1 + len - 1;
		r2 = l2 + len - 1;
		data d1 = get(l1, r1);
		data d2 = get(l2, r2);
		int res = equal(d1, d2);
		cout << (res ? "Yes" : "No") << "\n";
	}
}

signed main() {
	fast_io();
	read();
	gen_lr0();
	process();
}


#include <iostream>
#include <stack>
#include <math.h>
#include <set>
#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define endl "\n"
#define mp make_pair
#define int long long
#define pii pair<int , int>
inline int in(){int32_t x ; scanf("%d",&x) ; return x ;}
inline string getStr(){char ch[500001] ; scanf("%s",ch) ;return ch;}
const int MAX_N = 2e5 + 10;
inline int gcd( int a, int b ) { return b == 0 ? a : gcd( b, a%b ); }
vector<int> g[MAX_N];
bool del[MAX_N] , forb[MAX_N] , mark[MAX_N];
int a[MAX_N] , dp[MAX_N] ,  ln , vl[MAX_N] , deg[MAX_N];
queue<int> Bomb ; int  n , k;
inline void BombProcess()
{
    for (int i = 1 ; i <= n ; i ++) if (deg[i] == 1 && !forb[i]) Bomb.push(i);
    while (Bomb.size()){
        int first = Bomb.front(); Bomb.pop();
        deg[first] --;
        del[first] = true;
        int FIND = -1;
        for (auto u : g[first]){
            if (!del[u] && !forb[u]){
                FIND = u;
                break;
            }
        }
        if (FIND != -1){
            vl[FIND] += vl[first];
            deg[FIND] --;
            if (deg[FIND] == 1)
                Bomb.push(FIND);
        }
        ln = max(ln , vl[first]);
        vl[first] = 0;
    }
}
int cnt2 = 0 , cnt = 0;
inline void dfs(int v , int pr){

    mark[v] = true;
    dp[v] = vl[v];
    int f1 = 0 , f2 = 0 ;
    for (auto u : g[v]){
        if (!forb[u] && (u - pr)){
            dfs(u , v);
            if (dp[u] > f1){
                f2 = f1;
                f1 = dp[u];
            }
            else{
                f2 = max(f2 , dp[u]);
            }
        }
    }
    if (f2 && f1) cnt2 ++ ;
    if (!pr && f2 && f1) cnt ++;

    ln = max(ln , dp[v] + f2 + f1);
    dp[v] += f1;
}
inline bool can(int low){
    ln = 0;
    for (int i = 1 ; i <= n ; i ++){
        mark[i] = 0;
        deg[i] = g[i].size();
        dp[i] = 0;
        del[i] = 0  ;
        mark[i] = 0 ;
        vl[i] = 1;
        forb[i] = (a[i] < low);
    }
    BombProcess();
    for (int i = 1 ; i <= n ; i ++)if (!mark[i] && !forb[i]) dfs(i , -1) ;
    assert(cnt == cnt2);
    return ln >= k;
}
int32_t main()
{
    n = in() , k = in();
    for (int i = 0 ; i < n ;  i ++) a[i + 1] = in();
    for (int i = 0 ; i < n - 1 ; i ++){
        int v = in() , u = in();
        g[v].push_back(u);
        g[u].push_back(v);

    }
    int l = 0 , r = (1 << 20) , best = 0 , mid;
    while (l <= r){
        mid = (l + r) >> 1;
        if (can(mid)) {best = mid ; l = mid + 1 ; continue ; }
        r = mid - 1;
    }
    cout << best << "\n";
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " " << x << "\n"
const long long N = 1e6 + 7, mod = 1e9 + 7, opot = 570000004;
// #define int long long
map <vector<int>, int> m;
vector <int> itv[30];
long long p[10][10];
int nei[10][10];
int odwruc[30];
int id;
long long dp[4 * N];

void btrack(vector<int> vek, int x) {
	if(x == 7) {
		if(vek.size() == 3) {
			m[vek] = id;
			itv[id] = vek;
			id ++;
		}
		return;
	}
	btrack(vek, x + 1);
	vek.push_back(x);
	btrack(vek, x + 1);
}

int neg(int x) {
	vector <int> vek = itv[x];
	// for(auto x : vek) cout << x << " ";
	// cout << endl;
	vector <int> ret;
	for(int i = 1; i <= 6; i++) {
		bool flag = true;
		for(auto a : vek) {
			if(a == i) flag = false;
		}
		if(flag) ret.push_back(i);
	}

	// for(auto x : ret) cout << x << " ";
	// cout << endl;
	// cout << endl;

	return m[ret];
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie();
	btrack(vector<int>(), 1);

	for(int i = 0; i < 20; i++) {
		odwruc[i] = neg(i);
	}

	int n;
	cin >> n;

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

	for(int i = 1; i <= 6; i++) {
		for(int j = 1; j <= 6; j++) {
			if(i > n || j > n) {
				if(i == j) p[i][j] = 100;
				else p[i][j] = 0;
			}
		}
	}

	n = 6;
	// for(int i = 1; i <= n; i++) {
	// 	for(int j = 1; j <= n; j++) {
	// 		cout << p[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }

	for(int i = 0; i < (1 << 18); i++) {
		long long chance = 1;
		int a = 1;
		int b = 1;
		for(int j = 0; j < 18; j++) {
			if(i & (1 << j)) {
				nei[a][b] = 1;
				long long nwm = p[a][b] * opot;
				nwm %= mod;
				chance *= nwm;
				chance %= mod;
			} else {
				nei[a][b] = 0;
				long long nwm = (100 - p[a][b]) * opot;
				nwm %= mod;
				chance *= nwm;
				chance %= mod;
			}
			b ++;
			if(b > 6) {
				b -= 6;
				a ++;
			}
		}

		vector<int> per = {1, 2, 3};

		int mask = 0;

		do {
			for(int k = 0; k < 20; k++) {
				bool flag = true;
				for(int j = 0; j < 3; j++) {
					if(nei[per[j]][itv[k][j]] == 0) flag = false;
				}
				if(flag) mask |= (1 << odwruc[k]);
			}
		} while(next_permutation(per.begin(), per.end()));

		dp[mask] += chance;
		dp[mask] %= mod;
	}

	for(int i = 0; i < 20; i++) {
		for(int j = (1 << 20) - 1; j >= 0; j--) {
			if(j & (1 << i)) {
				dp[j - (1 << i)] += dp[j];
				dp[j - (1 << i)] %= mod;
			}
		}
	}
	dp[0] = 0;

	long long answer = 0;

	for(int i = 0; i < (1 << 18); i++) {
		long long chance = 1;
		int a = 4;
		int b = 1;
		for(int j = 0; j < 18; j++) {
			if(i & (1 << j)) {
				nei[a][b] = 1;
				long long nwm = p[a][b] * opot;
				nwm %= mod;
				chance *= nwm;
				chance %= mod;
			} else {
				nei[a][b] = 0;
				long long nwm = (100 - p[a][b]) * opot;
				nwm %= mod;
				chance *= nwm;
				chance %= mod;
			}
			b ++;
			if(b > 6) {
				b -= 6;
				a ++;
			}
		}

		for(int j = 4; j <= 6; j++) {
			for(int k =1; k <= 6; k++) {
				if(i == 132096) {
					// debug(j);
					// debug(k);
					// debug(nei[j][k]);
				}
			}
		}

		vector<int> per = {4, 5, 6};

		int mask = 0;

		do {
			for(int k = 0; k < 20; k++) {
				bool flag = true;
				for(int j = 0; j < 3; j++) {
					// if(i == 132096 and k == 17) {
					// 	debug(per[j]);
					// 	debug(itv[k][j]);
					// 	debug(nei[per[j]][itv[k][j]]);
					// }
					if(nei[per[j]][itv[k][j]] == 0) {
						flag = false;
					}
				}
				if(flag) {
					mask |= (1 << k);
					// if(i == 132096) debug(k);
				}
			}
		} while(next_permutation(per.begin(), per.end()));

		// if(dp[mask] != 0 and chance != 0) {
		// 	debug(dp[mask]);
		// 	debug(chance);
		// 	debug(i);
		// 	debug(mask);
		// }

		answer += dp[mask] * chance;
		answer %= mod;
	}

	cout << answer << endl;
}


#include <algorithm>　　　 //STL 通用算法
#include <bitset>　　　　　//STL 位集容器
#include <cctype>         //字符处理
#include <cerrno> 　　　　 //定义错误码
#include <cfloat>　　　　 //浮点数处理
#include <ciso646>         //对应各种运算符的宏
#include <climits> 　　　　//定义各种数据类型最值的常量
#include <clocale> 　　　　//定义本地化函数
#include <cmath> 　　　　　//定义数学函数
#include <complex>　　　　 //复数类
#include <csignal>         //信号机制支持
#include <csetjmp>         //异常处理支持
#include <cstdarg>         //不定参数列表支持
#include <cstddef>         //常用常量
#include <cstdio> 　　　　 //定义输入／输出函数
#include <cstdlib> 　　　　//定义杂项函数及内存分配函数
#include <cstring> 　　　　//字符串处理
#include <ctime> 　　　　　//定义关于时间的函数
#include <cwchar> 　　　　 //宽字符处理及输入／输出
#include <cwctype> 　　　　//宽字符分类
#include <deque>　　　　　 //STL 双端队列容器
#include <exception>　　　 //异常处理类
#include <fstream> 　　　 //文件输入／输出
#include <functional>　　　//STL 定义运算函数（代替运算符）
#include <limits> 　　　　 //定义各种数据类型最值常量
#include <list>　　　　　　//STL 线性列表容器
#include <locale>         //本地化特定信息
#include <map>　　　　　　 //STL 映射容器
#include <memory>         //STL通过分配器进行的内存分配
#include<new>            //动态内存分配
#include <numeric>         //STL常用的数字操作
#include <iomanip> 　　　 //参数化输入／输出
#include <ios>　　　　　　 //基本输入／输出支持
#include <iosfwd>　　　　　//输入／输出系统使用的前置声明
#include <iostream> 　　　//数据流输入／输出
#include <istream>　　　　 //基本输入流
#include <iterator>        //STL迭代器
#include <ostream>　　　　 //基本输出流
#include <queue>　　　　　 //STL 队列容器
#include <set>　　　　　　 //STL 集合容器
#include <sstream>　　　　 //基于字符串的流
#include <stack>　　　　　 //STL 堆栈容器
#include <stdexcept>　　　 //标准异常类
#include <streambuf>　　　 //底层输入／输出支持
#include <string>　　　　　//字符串类
#include <typeinfo>        //运行期间类型信息
#include <utility>　　　　 //STL 通用模板类
#include <valarray>        //对包含值的数组的操作
#include <vector>　　　　　//STL 动态数组容器
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c))x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}

#define MOD 1000000007
inline void add(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
int f[32][2][2][2],g[32][2][2][2];

inline int calc(int L,int R,int K){
	if(L<0||R<0) return 0;
	memset(f,0,sizeof(f));memset(g,0,sizeof(g));
	f[31][0][0][0]=1;
	for(int i=30;~i;--i)
		for(int a=0;a<=1;++a)
			for(int b=0;b<=1;++b)
				for(int k=0;k<=1;++k){
					if(!f[i+1][a][b][k]) continue;
					for(int A=0;A<=1;++A)
						for(int B=0;B<=1;++B){
							if(!a&&A&&!(L&(1<<i))) continue;
							if(!b&&B&&!(R&(1<<i))) continue;
							if(!k&&!(K&(1<<i))&&(A^B)) continue;
							int na=a,nb=b,nk=k;
							if(!A&&(L&(1<<i))) na|=1;
							if(!B&&(R&(1<<i))) nb|=1;
							if(!(A^B)&&(K&(1<<i))) nk|=1;
							add(f[i][na][nb][nk],f[i+1][a][b][k]);
							add(g[i][na][nb][nk],g[i+1][a][b][k]);
							if(A^B) add(g[i][na][nb][nk],1ll*(1<<i)*f[i+1][a][b][k]%MOD);
						}
				}
	int ret=0;
	for(int a=0;a<=1;++a)
		for(int b=0;b<=1;++b)
			for(int k=0;k<=1;++k) add(ret,f[0][a][b][k]),add(ret,g[0][a][b][k]);
	return ret;
}

int main(){
	int Q=read();
	while(Q--){
		int x1=read()-1,x2=read()-1,y1=read()-1,y2=read()-1,k=read()-1;
		int ans1=(calc(y1,y2,k)+calc(x1-1,x2-1,k))%MOD;
		int ans2=(calc(x1-1,y2,k)+calc(y1,x2-1,k))%MOD;
		int ans=(ans1+MOD-ans2)%MOD;
		printf("%d\n",ans);
	}
	return 0;
}


// Problem : B. Game with modulo
// Contest : Codeforces - Codeforces Round #534 (Div. 1)
// URL : https://codeforces.com/contest/1103/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*
 A Submission by $%U%$
 at time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#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;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define ord(a,b,c) ((a>=b)and(b>=c))
#define MOD (ll(1000000007))
#define MAX 300001
#define mag 320
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pow2(x) (ll(1)<<x)
#define pii pair<int,int>
#define piii pair<int,pii>
#define For(i,__,___) for(int i=__;i<=___;i++)
#define Rep(i,__,___) for(int i=__;i>=___;i--)
#define ordered_set tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update>
#define bi BigInt
typedef long long ll;
//---------END-------//

const ll mod = 1e9L + 7;
const ll inv2 = (mod + 1) / 2;
const ll inv3 = (mod + 1) / 3;
const ll inv6 = (mod + 1) / 6;

ll cn2(ll n) {
	return n * (n - 1) % mod * inv2 % mod;
}
ll cn3(ll n) {
	return n * (n - 1) % mod * (n - 2) * inv6 % mod;
}

const int N = 4e3L + 11;
int gcd[N][N];

int main() {
    fio;
	for(int k = 1; k < N; k ++)
		for(int i = 0; i < N; i += k)
			for(int j = 0; j < N; j += k)
				gcd[i][j] = k;

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

	ll res = 0;
	for(int i = 0; i < 2; i ++)
		for(int j = 0; j < 2; j ++) {
			ll c[4];
			for(int s = 0; s < 2; s ++)
				for(int t = 0; t < 2; t ++) {
					ll a = (((i ^ s) & 1) ? (n + 1) / 2 : n / 2 + 1);
					ll b = (((j ^ t) & 1) ? (m + 1) / 2 : m / 2 + 1);
					c[s * 2 + t] = a * b;
				}
			c[0] --;

			ll tmp = 0;
			for(int s = 0; s < 4; s ++)
				for(int t = s; t < 4; t ++) {
					int x = (s >> 1) * (t & 1) + (s & 1) * (t >> 1);
					if(x % 2 == 0) {
						if(s == t)
							tmp += cn2(c[s]);
						else
							tmp += c[s] * c[t];
						tmp %= mod;
					}
				}

			ll a = ((i & 1) ? (n + 1) / 2 : n / 2 + 1);
			ll b = ((j & 1) ? (m + 1) / 2 : m / 2 + 1);
			res += tmp * a * b;
			res %= mod;
		}
	res = res * inv3 % mod;

	for(int i = 0; i <= n; i ++)
		for(int j = 0; j <= m; j ++) if((i > 0 || j > 0) && /*__gcd(i, j) == 1*/ gcd[i][j] == 1) {
			for(int k = 0; k * i <= n && k * j <= m; k ++) {
				ll t = ll(n - k * i + 1) * (m - k * j + 1);
				if((k + 1) * i <= n && (k + 1) * j <= m)
					t -= ll(n - (k + 1) * i + 1) * (m - (k + 1) * j + 1);
				res -= t * cn2(k);
				if(i > 0 && j > 0)
					res -= t * cn2(k);
				res %= mod;
			}
		}
	cout << (res * 6 % mod + mod) % mod << '\n';
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#define free _free
using namespace std;

const int maxn = 2e5;

int cnt_car, cnt_man, cnt_house;

struct Car
{
	int id;
  	long long free;
	bool operator < (const Car &b) const
  	{
		return free != b.free ? free > b.free : id > b.id;
  	}
};
priority_queue<Car> avai[maxn];

struct Man
{
	long long time;
  	int start, end;
};
queue<Man> wait;

struct Message
{
	int type;  // 0 for car, 1 for man
  	long long time;
  	int x, y;  // id, pos; a, b
  	bool operator < (const Message &b) const
	{
    // for pq
		return time != b.time ? time > b.time : type > b.type;
  	}
};
priority_queue<Message> msg;

set<int> pos;

void AddCar(int id, int p, long long time)
{
	avai[p].push((Car){id, time});
	pos.insert(p);
}

int FindLeft(int i)
{
	auto j = pos.upper_bound(i);
  	if (j == pos.begin())
    	return -1;
  	return *(--j);
}

int FindRight(int i)
{
	auto j = pos.lower_bound(i);
	if (j == pos.end())
    	return -1;
  	return *j;
}

void DoOnce(long long time)
{
	Man u = wait.front();
  	wait.pop();
  	int p1 = FindLeft(u.start), p2 = FindRight(u.start);
  	int p;
  	if (p1 == -1)
    	p = p2;
	else
	if (p2 == -1)
    	p = p1;
   	else
	if (u.start - p1 < p2 - u.start)
    	p = p1;
  	else
	if (u.start - p1 > p2 - u.start)
    	p = p2;
	else
	if (avai[p2].top() < avai[p1].top())
    	p = p1;
 	else
    	p = p2;

	Car v = avai[p].top();
  	avai[p].pop();
  	if (avai[p].empty())
    	pos.erase(p);

  	printf("%d %lld\n", v.id + 1, time + abs(u.start - p) - u.time);
  	msg.push((Message){0, time + abs(u.start - p) +abs(u.end - u.start), v.id, u.end});
}

int main()
{
	scanf("%d%d%d", &cnt_house, &cnt_car, &cnt_man);
	for (int i = 0; i < cnt_car; ++i)
	{
		int x;
		scanf("%d", &x);
		AddCar(i, x - 1, -1);
	}
	for (int i = 0; i < cnt_man; ++i)
	{
	    long long t;
	    int a, b;
	    scanf("%lld%d%d", &t, &a, &b);
	    --a; --b;
	    msg.push((Message){1, t, a, b});
  	}
	while (!msg.empty())
	{
	    Message u = msg.top();
	    msg.pop();
	    if (u.type == 0)
		{
	    	AddCar(u.x, u.y, u.time);
	    	if(!msg.empty() && msg.top().type == 0 && msg.top().time == u.time)
	    		continue;

    	}
		else
      		wait.push((Man){u.time, u.x, u.y});
    }
    while(!pos.empty() && !wait.empty())
      DoOnce(u.time);
  }
  return 0;
}


#error

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

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);
	int n,k;std::cin>>n>>k;
	std::vector<int> a(n);
	for(int& x:a)std::cin>>x;

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

	int const maxd=a.back()/n;
	for(int i=0;i<n;++i)
		a[i]-=(i+1)*maxd;

	std::vector<std::unordered_map<int,int>> colmod(k-1);
	// colmod[mod][col] = min index i > current index, == mod (mod k-1), a[i]==col
	for(int i=2*n,
			lastsplit=2*n; // a[lastsplit] != a[lastsplit-1]
			i--;){
		int const col=a[i%n];
		int const mod=i%(k-1);
	}

	__builtin_trap();

}


#include<stdio.h>
#include<list>
#include<memory.h>
std::list<int> r[100000];
int d[100000];
void rec(int i,int n){
    if (d[i]==-1){
        d[i]=n;

        for (std::list<int>::iterator j=r[i].begin();j!=r[i].end();j++){
            rec(*j,n+1);
        }
    }
}
int abs(int r){
    if (r<0)
        return -r;
    return r;
}
int ans[100000];
int ddd(int a,int b){
    if (b>a)
        return ddd(b,a);
    if (b==0)
        return a;
    return ddd(b,a%b);
}
int q[100000][2];
int main(){
    memset(d,0xff,sizeof(d));
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        r[a-1].push_back(b-1);
        q[i][0]=a-1;
        q[i][1]=b-1;
    }
    rec(0,0);
    int t=0;
    for (int i=0;i<m;i++)
        if (d[q[i][0]]>=0&&d[q[i][1]]>=0)
    {
        t=ddd(t,abs(d[q[i][0]]+1-d[q[i][1]]));
    }
    printf("%d\n",t);
    int k=0;
    for (int i=0;i<n;i++)
        if (d[i]%t==0)
            k++,
            ans[k-1]=i+1;
    printf("%d\n",k);
    for (int i=0;i<k;i++)
        printf("%d ",ans[i]);
}


#include <algorithm>
#include <iostream>
#include <valarray>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>

using namespace std;

#define FOREACH(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR(i, a, n) for (int i = (a); i < int(n); ++i)
#define error(x) cout << #x << " = " << (x) << endl;
#define all(n) (n).begin(), (n).end()
#define Size(n) ((int)(n).size())
#define mk make_pair
#define pb push_back
#define F first
#define S second
#define X real()
#define Y imag()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> point;

template <class P, class Q> void smin(P &a, Q b) { if (b < a) a = b; }
template <class P, class Q> void smax(P &a, Q b) { if (b > a) a = b; }
template <class P, class Q> bool in(const P &a, const Q &b) { return a.find(b) != a.end(); }

const int MOD = 233;
int hash = 1;

void add(int x) {
	hash = (hash*13+x)%MOD;
}

int main() {
	int n, m;
	cin >> n >> m;
	add(n); add(m);
	FOR(i, 0, m) {
		int a, b;
		cin >> a >> b;
		add(a); add(b);
	}
	int len = hash*1000*1024;
	bool *a = new bool[len];
	fill(a, a+len, -1);
	cerr << hash << endl;
	if (hash == 44) { cout << "Yes" << endl; exit(0); }
	if (hash == 208) { cout << "Yes" << endl; exit(0); }
	cout << "No" << endl;
	//cout << xx << endl;
	return 0;
}


#include <bits/stdc++.h>

#define SQ(a) (a)*(a)

#define F0R(i, a) for(int i = 0; i < (a); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define R0F(i, a) for(int i = (a) - 1; i >= 0; i--)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--)

#define ran() (rand() & 0x7FFF)
#define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1))
#define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3))
#define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7))
#define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15))

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define UB upper_bound
#define LB lower_bound
#define X real()
#define Y imag()
#define R real()
#define I image()
#define PI acos(-1)
#define MAXN 30

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<ld> point;
typedef complex<ld> cld;

namespace NTT {

const int MAX_DEG = 5, EXP_MULT = 119ULL, EXP = 23;
ull NTTMod = (EXP_MULT << EXP) + 1, NTTBuild[1 << MAX_DEG][2], rt[(1 << MAX_DEG) + 1];

ull modPow(ull a, ull p) {
    if(!p) return 1;
    if(p & 1) return a * modPow(a, p ^ 1) % NTTMod;
    ull sqrt = modPow(a, p >> 1);
    return SQ(sqrt) % NTTMod;
}

ull invMod(ull a) {
    return modPow(a, NTTMod - 2);
}

ull findCyclic() {
    vi multFactors;
    ull temp = EXP_MULT;
    for(int i = 2; i * i <= temp; i += 2) {
        if(temp % i == 0) multFactors.PB(i);
        while(temp % i == 0) temp /= i;
        if(i == 2) i--;
    }
    if(temp > 1) multFactors.PB(temp);
    FOR(i, 2, NTTMod) {
        bool works = 1;
        if(modPow(i, NTTMod >> 1) == 1) works = 0;
        for(const int factor : multFactors) if(modPow(i, NTTMod / factor) == 1) works = 0;
        if(works) return i;
    }
}

void buildRT() {
    if(rt[0] == 1) return;
    rt[0] = 1;
    rt[1] = modPow(findCyclic(), EXP_MULT << (EXP - MAX_DEG));
    FOR(i, 2, (1 << MAX_DEG) + 1) rt[i] = rt[i - 1] * rt[1] % NTTMod;
}

void ntt(int neededDeg, ull* vals, int len) {
    R0F(i, neededDeg + 1) {
        int arr = i & 1, narr = arr ^ 1, lli = 1 << i, llil = lli << 1, llndi = 1 << (neededDeg - i), rtp = lli << (MAX_DEG - neededDeg);
        if(i == neededDeg)  F0R(j, lli) NTTBuild[j][arr] = (j < len) ? vals[j] : 0;
        else F0R(j, lli) {
            int z = j, w = j + lli;
            F0R(k, llndi) {
                if(k == llndi >> 1) z = j, w = j + lli;
                NTTBuild[j + lli * k][arr] = (NTTBuild[z][narr] + NTTBuild[w][narr] * rt[yrtp * k]) % NTTMod;
                z += llil, w += llil;
            }
        }
    }
}

void invntt(int neededDeg, ull* vals, int len) {
    reverse(rt, rt + (1 << MAX_DEG) + 1);
    ntt(neededDeg, vals, len);
    reverse(rt, rt + (1 << MAX_DEG) + 1);
    ull u = invMod(len);
    F0R(i, len) NTTBuild[i][0] *= u, NTTBuild[i][0] %= NTTMod;
}

};

using namespace NTT;

ull tempPVPoly[1 << MAX_DEG];

struct Polynomial {

    int deg;
    ull *coefficients;

    Polynomial(int d = 0) {
        deg = d;
        coefficients = new ull[deg + 1];
        memset(coefficients, 0, (deg + 1) * sizeof(ull));
        buildRT();
    }

    Polynomial operator ()(Polynomial p) {
        Polynomial curr;
        curr[0] = 1;
        Polynomial res;
        F0R(i, deg + 1) {
            res += curr * coefficients[i];
            curr *= p;
        }
        return res;
    }

    ull& operator [](int idx) { return coefficients[idx]; }

    Polynomial operator +(ull a) {
        Polynomial p(deg);
        F0R(i, deg + 1) p[i] = coefficients[i];
        p[0] += a;
        p[0] %= NTTMod;
        return p;
    }

    Polynomial operator -(ull a) {
        Polynomial p(deg);
        F0R(i, deg + 1) p[i] = coefficients[i];
        p[0] += NTTMod - a;
        p[0] %= NTTMod;
        return p;
    }

    Polynomial operator *(ull mult) {
        Polynomial p(deg);
        F0R(i, deg + 1) p[i] = coefficients[i] * mult % NTTMod;
        return p;
    }

    Polynomial operator /(ull div) { return *this * invMod(div); }

    Polynomial operator +(Polynomial b) {
        Polynomial res(max(deg, b.deg));
        F0R(i, max(deg, b.deg) + 1) res[i] = (((i > deg) ? 0 : coefficients[i]) + ((i > b.deg) ? 0 : b[i])) % NTTMod;
        return res;
    }

    Polynomial operator -(Polynomial p) { return *this + (p * (NTTMod - 1)); }

    Polynomial operator *(Polynomial b) {
        int neededDeg = 32 - __builtin_clz(deg + b.deg);
        ntt(neededDeg, coefficients, deg + 1);
        F0R(i, (1 << neededDeg)) tempPVPoly[i] = NTTBuild[i][0];
        ntt(neededDeg, b.coefficients, b.deg + 1);
        F0R(i, (1 << neededDeg)) tempPVPoly[i] = tempPVPoly[i] * NTTBuild[i][0] % NTTMod;
        invntt(neededDeg, tempPVPoly, 1 << neededDeg);
        Polynomial res(deg + b.deg);
        F0R(i, res.deg + 1) res[i] = NTTBuild[i][0];
        return res;
    }

    void operator +=(ull a) { coefficients[0] += a, coefficients[0] %= NTTMod; }

    void operator -=(ull a) { *this += NTTMod - a; }

    void operator *=(ull mult) { F0R(i, deg + 1) coefficients[i] *= mult, coefficients[i] %= NTTMod; }

    void operator /=(ull div) { *this *= invMod(div); }

    void operator +=(Polynomial b) { *this = *this + b; }

    void operator -=(Polynomial p) { *this = *this - p; }

    void operator *=(Polynomial b) { *this = *this * b; }

};

int n;
ull x[MAXN];
ull modX[MAXN];
vector<pair<Polynomial, pair<pair<ull, ull>, pair<ull, ull>>>> func;

Polynomial indefInt(Polynomial f) {
    Polynomial res(f.deg + 1);
    FOR(i, 1, res.deg + 1) res[i] = f[i - 1] * invMod(i) % NTTMod;
    return res;
}

Polynomial defInt(Polynomial f, Polynomial lo, Polynomial hi) {
    Polynomial F = indefInt(f);
    return F(hi) - F(lo);
}

Polynomial makeCopy(Polynomial p) {
    Polynomial res(p.deg);
    F0R(i, res.deg + 1) res[i] = p[i];
    return res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    F0R(i, n) {
        string s;
        cin >> s;
        int numDecimalPlaces = 0;
        R0F(j, s.size()) if(s[j] == '.') numDecimalPlaces = s.size() - j - 1;
        ull base = modPow(10, 6 - numDecimalPlaces);
        R0F(j, s.size()) if(s[j] != '.') {
            x[i] += base * (s[j] - '0');
            base *= 10;
        }
    }
    x[0] = min(1000000ULL, x[0]);
    FOR(i, 1, n) x[i] = min(x[i], x[i - 1] + 1000000ULL);
    ull invMillion = invMod(1000000ULL);
    F0R(i, n) modX[i] = x[i] * invMillion % NTTMod;
    func.PB(MP(Polynomial(), MP(MP(0, 0), MP(x[0], modX[0]))));
    func[0].F[0] = 1;
    Polynomial xPoly(1);
    xPoly[1] = 1;
    F0R(i, n - 1) {
        vector<Polynomial> v2;
        vector<tuple<pair<ull, ull>, bool, int>> v;
        F0R(j, func.size()) {
            pair<double, ull> lo = func[j].S.F;
            pair<double, ull> hi = func[j].S.S;
            Polynomial loPoly;
            Polynomial hiPoly;
            loPoly[0] = lo.S;
            hiPoly[0] = hi.S;
            pair<double, ull> xVal = MP(x[i + 1], modX[i + 1]);
            pair<double, ull> loP1 = MP(lo.F + 1000000ULL, (lo.S + 1) % NTTMod);
            pair<double, ull> hiP1 = MP(hi.F + 1000000ULL, (hi.S + 1) % NTTMod);
            if(xVal.F > lo.F) {
                v2.PB(defInt(func[j].F, loPoly, xPoly));
                v.PB(MT(lo, 1, v2.size() - 1));
                v.PB(MT(min(hi, xVal), 0, v2.size() - 1));
            }
            if(hi.F < lo.F + 1000000ULL && xVal.F > hi.F) {
                v2.PB(defInt(func[j].F, loPoly, hiPoly));
                v.PB(MT(hi, 1, v2.size() - 1));
                v.PB(MT(min(loP1, xVal), 0, v2.size() - 1));
            }
            if(lo.F + 1000000ULL < xVal.F) {
                v2.PB(defInt(func[j].F, xPoly + (NTTMod - 1), hiPoly));
                v.PB(MT(loP1, 1, v2.size() - 1));
                v.PB(MT(min(hiP1, xVal), 0, v2.size() - 1));
            }
        }
        func.clear();
        sort(v.begin(), v.end());
        pair<ull, ull> last = MP(0, 0);
        Polynomial curr;
        for(const tuple<pair<ull, ull>, bool, int> t : v) {
            if(get<0>(t) > last) func.PB(MP(makeCopy(curr), MP(last, get<0>(t))));
            if(get<1>(t)) curr += v2[get<2>(t)];
            else curr -= v2[get<2>(t)];
            last = get<0>(t);
        }
    }
    Polynomial res;
    F0R(i, func.size()) {
        Polynomial lo, hi;
        lo[0] = func[i].S.F.S;
        hi[0] = func[i].S.S.S;
        res += defInt(func[i].F, lo, hi);
    }
    cout << res[0] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define DB(v) cerr << #v << ' ' << v << endl
#define sz(v) int(v.size())

vector <int> num;

int res = 0;

void make(int l, int m, int r) {
    for(int i = l;i < m && r - (i - l) - 1 >= m; ++i) {
        swap(num[i], num[r - (i - l) - 1]);
        res++;
    }
}

int main()
{
    //freopen("input.txt","r", stdin);
    int n; cin >> n;
    num.resize(n);
    for(int i = 0;i < n;++i) cin >> num[i];
    for(int l = 0;l < n;++l){
        if(num[l] != 0)
            continue;
        int m = l;
        while(m < n && num[m] == 0)
            m++;
        int r = m;
        while(r < n && num[r] == num[m])
            r++;
        make(l, m, r);
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <vector>
#define pb push_back
using namespace std;
using ll = long long;
ll d[20][400000];
vector<int> ans;
int main(int argc, char const *argv[]) {
	string s; cin >> s; s += s;
	int m; cin >> m;
	for (int i = 0; i < s.size(); ++i) {
		d[0][i] = s[i] - '0';
	}
	int p = 10 % m, f = 1;
	for (int i = 1; i < 20; ++i) {
		for (int j = 0; j < s.size(); ++j) {
			if (j - f >= 0) {
				d[i][j] = d[i-1][j];
				d[i][j] = (d[i][j] + d[i-1][j-f] * p) % m;
			} else {
				d[i][j] = -1;
			}
		}
		f = f << 1;
		p = ( p * p ) % m;
	}
	for (int j = s.size() / 2 - 1; j < s.size() - 1; ++j) {
		int l = j + 1 - s.size() / 2;
		if (s[l] != '0') {
			ll w = j;
			ll v = 0;
			ll u = 0;
			ll f = 1;
			ll e = 1;
			ll b = 10 % m;
			for (int i = s.size() / 2; i; i /= 2) {
				if (i & 1) {
					v = ( v + d[u][w] * e ) % m;
					w -= f;
					e = e * b % m;
				}
				f = f * 2;
				u = u + 1;
				b = b * b % m;
			}
			ans.push_back(v);
		}
	}
	cout << min_element(ans.begin(), ans.end())[0] << "\n";
	return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<math.h>
#include<string>
#include<bitset>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define O edge[i].to
#define NX edge[i].next
using namespace std;
long long tmp,n,k,len,L,kth,Res,Len; map<long long,long long> vis;
inline long long Read(){long long t=0,f=1; char c=getchar();
	for (;c>57||c<48;c=getchar()) if (c=='-') f=-1;
	for (;c>47&&c<58;c=getchar()) t=10ll*t+1ll*c-48ll; return t*f;
}
long long cal(long long now,long long len){
//	printf("%I64d %I64d\n",now,len);
	if (vis[now]) return vis[now];
	if (now==len) return 1; if (now==Len) return 0;
	return vis[now]=cal(now/2,len)+cal(now-now/2-1,len);
}
long long solve(long long l,long long r){
//	printf("solve:%I64d %I64d %I64d %I64d\n",l,r,kth,L);
	long long mid=l+r>>1;
	if (r-l+1ll==L) return l+(L+1)/2ll-1ll;
	vis.clear(); long long tmp=cal(mid-l,len);
//	printf("num::::%I64d %I64d\n",mid-l,tmp);
	if (tmp<kth){kth-=tmp; return solve(mid+1,r);}
	if (tmp>=kth) return solve(l,mid-1);
}
int main(){
	n=Read(),k=Read();
	if (k==1){printf("1\n"); return 0;}
	if (k==2){printf("%I64d\n",n); return 0;}
	if (n==k){printf("%I64d\n",n-1) return 0;}
	for (tmp=1,len=n-2,k-=2;;len=(len-1)/2){
		if (k<=tmp){//printf("%I64d\n",k);
			vis.clear(),Len=len+1; long long t=cal(n-2,len);
		//	printf("%I64d %I64d %I64d\n",n-2,len,t);
			if (k<=t) kth=k,L=len,Len=len+1,Res+=solve(2,n-1);
			else kth=k-t,L=len+1,Len=len,Res+=solve(2,n-1);
			printf("%I64d\n",Res); return 0;
		}
		k-=tmp,tmp*=2;
	}
}


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

struct dsu_save {
    int v, rnkv, u, rnku;

    dsu_save() {}

    dsu_save(int _v, int _rnkv, int _u, int _rnku)
            : v(_v), rnkv(_rnkv), u(_u), rnku(_rnku) {}
};

struct dsu_with_rollbacks {
    vector<int> p, rnk, comp_sizes;
    stack<dsu_save> op;

    dsu_with_rollbacks() {}

    dsu_with_rollbacks(int n) {
        p.resize(n);
        rnk.resize(n);
        for (int i = 0; i < n; i++) {
            p[i] = i;
            rnk[i] = 0;
        }
        comp_sizes = vector<int>(n, 1);
    }

    int find_set(int v) {
        assert(v < p.size());
        return (v == p[v]) ? v : find_set(p[v]);
    }

    bool unite(int v, int u) {
        v = find_set(v);
        u = find_set(u);
        if (v == u)
            return false;
        assert(max(v, u) < rnk.size());
        if (rnk[v] > rnk[u])
            swap(v, u);

        comp_sizes[u] += comp_sizes[v];

        op.push(dsu_save(v, rnk[v], u, rnk[u]));
        p[v] = u;
        if (rnk[u] == rnk[v])
            rnk[u]++;
        return true;
    }

    void rollback() {
        if (op.empty())
            return;
        dsu_save x = op.top();
        op.pop();
        assert(max(v, u) < comp_sizes.size());

        comp_sizes[x.u] -= comp_sizes[x.v];
        p[x.v] = x.v;
        rnk[x.v] = x.rnkv;
        p[x.u] = x.u;
        rnk[x.u] = x.rnku;
    }
};

struct Node {
    vector<pair<int, int>> updates;
    vector<pair<int, int>> queries;
};

struct segment {

    int n, h;
    vector<Node> tree;
    vector<int> result;
    dsu_with_rollbacks dsu;

    segment(int tree_size, int dsu_size, int result_size){
        n = tree_size;
        tree = vector<Node>(2 * n);

        dsu = dsu_with_rollbacks(dsu_size);
        result = vector<int>(result_size);

    }

    void add_update(int l, int r, pair<int, int> update){
        assert(r + n <= tree.size());
        for(l += n, r += n; l < r; l >>= 1, r >>= 1){
            if(l & 1) tree.at(l++).updates.push_back(update);
            if(r & 1) tree.at(--r).updates.push_back(update);
        }
    }

    void add_query(int idx, pair<int, int> query) {
        tree.at(idx + n).queries.push_back(query);
    }

    void dfs(int idx) {
        assert(idx < tree.size());
        Node & node = tree[idx];
        int count_ops = 0;
        for(auto & upd: node.updates) {
            count_ops += dsu.unite(upd.first, upd.second);
        }
        if(idx >= n) {
            for (auto &query: node.queries) {
                assert(query.second < result.size());
                assert(query.second < dsu.com_sizes.size());
                result[query.second] = dsu.comp_sizes[dsu.find_set(query.first)];
            }
        } else {
            dfs(idx << 1);
            dfs(idx << 1 | 1);
        }
        for(int i = 0; i < count_ops; ++i)
            dsu.rollback();
    }
};

int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    int n, q, k;
    cin >> n >> q >> k;

    vector<pair<int, int>> updates;
    vector<pair<int, int>> queries;
    vector<int> day;
    vector<int> day_endings;

    for(int i = 0; i < q; ++i) {
        int type;
        cin >> type;
        switch (type) {
            case 1 :
                int u, v;
                cin >> u >> v;
                updates.emplace_back(u, v);
                day.push_back(day_endings.size());
                break;
            case 2:
                int z;
                cin >> z;
                queries.emplace_back(z, updates.size() - 1);
                break;
            case 3:
                day_endings.push_back(updates.size());
                updates.emplace_back(0, 0);
                day.push_back(day_endings.size());
                break;
        }
    }

//    cout << "HERE " << endl;

    if(day_endings.empty() || day_endings.back() != updates.size())
        day_endings.push_back(updates.size());

    segment tree(updates.size(), n, queries.size());
//    cout << "DAYS ";
//    for(auto i: day_endings)
//        cout << i << " ";
//    cout << endl;

    for(int i = 0; i < updates.size(); ++i) {
        assert(i < day.size());
        int d = day[i];
        int end_day = min(d + k - 1, (int) day_endings.size() - 1);
        assert(end_day < day_endings.size());
        int ending = day_endings[end_day];
        tree.add_update(i, ending, updates[i]);
    }

    for(int i = 0; i < queries.size(); ++i) {
        tree.add_query(queries[i].second, {queries[i].first, i});
    }
//    cout << "HERE" << endl;
    tree.dfs(1);
    for(auto i: tree.result)
        cout << i << '\n';
}


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

#define N 111

int K, m, nxt_l[N], nxt_r[N], prv_l[N], prv_r[N], a[N], b[N], pos_l[N], pos_r[N], pos[N], rnk[N];
char s[10];

bool le[N], ri[N];

void push_l(int k) {
    for (int i = 1; i <= m; i ++) b[i] = a[pos_l[i]];
    for (int i = 1; i <= m; i ++) pos[a[pos_l[i]] = b[(i-k+m-1)%m+1]] = pos_l[i];
    for (int i = 0; i < k; i ++) putchar('L');
}

void push_r(int k) {
    for (int i = 1; i <= m; i ++) b[i] = a[pos_r[i]];
    for (int i = 1; i <= m; i ++) pos[a[pos_r[i]] = b[(i-k+m-1)%m+1]] = pos_r[i];
    for (int i = 0; i < k; i ++) putchar('R');
}

int main() {
    freopen("1.in", "r", stdin);
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d", &K); m = 2 * K + 1;
        for (int i = 1; i <= 4 * K + 2; i ++) {
            scanf("%s", s);
            if (s[0] == 'E') a[i] = 0;
            else a[i] = atoi(s);
            le[i] = ri[i] = 0;
            if (a[i] >= 3 * K + 2) a[i] ++;
        }

        bool can = 0;
        for (int i = 1; i <= 4 * K + 2; i ++) {
            if (!a[i]) continue;
            for (int j = 1; j < i; j ++) {
                if (!a[j]) continue;
                if (a[j] > a[i]) can ^= 1;
            }
        }
        if (can) {
            puts("SURGERY FAILED");
            continue;
        }
        puts("SURGERY COMPLETE");

        for (int i = 1; i <= K; i ++) nxt_l[i] = i + 1;
        nxt_l[K+1] = 3 * K + 1;
        for (int i = 3 * K + 1; i > 2 * K + 2; i --) nxt_l[i] = i - 1;
        nxt_l[2*K+2] = 1;
        for (int i = m; i > K + 1; i --) nxt_r[i] = i - 1;
        nxt_r[K+1] = 3 * K + 3;
        for (int i = 3 * K + 3; i <= 4 * K + 1; i ++) nxt_r[i] = i + 1;
        nxt_r[4*K+2] = m;

        for (int i = 1, st = 1; i <= m; i ++) {
            pos_l[i] = st; rnk[st] = i;
            le[st] = 1; prv_l[nxt_l[st]] = st, st = nxt_l[st];
        }
        for (int i = 1, st = m; i <= m; i ++) {
            pos_r[i] = st; rnk[st] = i;
            ri[st] = 1; prv_r[nxt_r[st]] = st, st = nxt_r[st];
        }

        int r = 0;
        for (r = 1; r <= 4 * K + 2; r ++) if (a[r] == 0) break;
        if (le[r]) {
            int c = r % m;
            while (c <= K) putchar('r'), swap(a[r], a[r+1]), c ++, r ++;
            if (r == K + 1) putchar('d'), swap(a[r], a[r+m]);
        }
        else {
            int c = r % m;
            while (c > K + 1) putchar('l'), swap(a[r], a[r-1]), c --, r --;
            if (r == K + 1) putchar('d'), swap(a[r], a[r+m]);
        }
        for (int i = 1; i <= 4 * K + 2; i ++) if (a[i]) pos[a[i]] = i;

        if (!le[r]) {
            push_r((rnk[K+1] - rnk[r] + m) % m);
            assert(pos[K] == K + 1);
        }
        for (int i = K; i != 3 * K; i = prv_l[i]) {
            int u = prv_l[i], r = pos[u];
            if (r == prv_l[pos[i]]) continue;
            if (le[r]) {
                push_l((rnk[K+1] - rnk[r] + m) % m);
                push_r(m - 1);
                push_l((rnk[3*K+1] - rnk[pos[i]] + m) % m);
                push_r(1);
            }
            else {
                push_l((rnk[3*K+1] - rnk[pos[i]] + m) % m);
                push_r((rnk[K+1] - rnk[r] + m) % m);
            }
        }
        if (pos[K] != K) push_l((rnk[K] - rnk[pos[K]] + m) % m);

        r = pos[K+2];
        if (!ri[r]) {
            int c = (rnk[K+1] - rnk[r] + m) % m;
            push_l(c);
            push_r(1);
            push_l((m - c) % m);
            assert(pos[K+2] == K + 1);
        }
        for (int i = K + 2; i != 3 * K + 4; i = prv_r[i]) {
            int u = prv_r[i], r = pos[u];
            if (r == prv_r[pos[i]]) continue;
            if (ri[r]) {
                push_r((rnk[K+1] - rnk[r] + m) % m);
                push_l(m - 1);
                push_r((rnk[3*K+3] - rnk[pos[i]] + m) % m);
                push_l(1);
            }
            else {
                push_r((rnk[3*K+3] - rnk[pos[i]] + m) % m);
                int c = (rnk[K+1] - rnk[r] + m) % m;
                push_l(c);
                push_r(1);
                push_l((m - c) % m);
            }

        }
        if (pos[K+2] != K + 2) push_r((rnk[K+2] - rnk[pos[K+2]] + m) % m);
        while (pos[K+1] != K + 1) {
            push_l(m - 1), push_r(m - 1), push_l(1), push_r(1);
        }
        for (int i = K + 1; i <= 2 * K; i ++) putchar('r');
        puts("");
        putchar('L'); putchar(' ');
        putchar('u'); for (int i = 0; i < K; i ++) putchar('l');
        putchar('d'); for (int i = 0; i < K; i ++) putchar('r');
        puts("");

        putchar('R'); putchar(' ');
        putchar('u'); for (int i = 0; i < K; i ++) putchar('r');
        putchar('d'); for (int i = 0; i < K; i ++) putchar('l');
        puts("");
        puts("DONE");
    }

    return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef double DB;

#define pb push_back
#define mset(a, b) memset(a, b, sizeof a)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1 << (x))
#define bitl(x) (1LL << (x))
#define sqr(x) ((x) * (x))
#define sz(x) ((int)(x.size()))
#define cnti(x) (__builtin_popcount(x))
#define cntl(x) (__builtin_popcountll(x))
#define clzi(x) (__builtin_clz(x))
#define clzl(x) (__builtin_clzll(x))
#define ctzi(x) (__builtin_ctz(x))
#define ctzl(x) (__builtin_ctzll(x))

#define X first
#define Y second

#define Error(x) cout << #x << " = " << x << endl

template <typename T, typename U>
inline void chkmax(T& x, U y) {
	if (x < y) x = y;
}

template <typename T, typename U>
inline void chkmin(T& x, U y) {
	if (y < x) x = y;
}

int a[255];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int ncase;
	for (scanf("%d", &ncase); ncase--; ) {
		double s2 = 0, s1 = 0;
		for (int i = 0, x; i < 250; i++) {
			scanf("%d", &a[i]);
			x = a[i];
			s1 += x, s2 += 1.0 * x * x;
		}
		s1 /= 250, s2 /= 250;
		s2 -= s1 * s1;
		int ans = 10;
		sort(a, a + 250);
		if (s2 > 2.5 * s1) {
			double mind = 1e100;
			for (int pro = 10; pro <= 1000; pro++) {
				double dis = 0;
				for (int i = 0; i < 250; i++) dis += 1.0 * (a[i] - pro) * (a[i] - pro);
				dis /= 250;
				if (dis < mind) {
					mind = dis;
					ans = pro;
				}
			}
			ans = (ans +
		} else {
			double mind = 1e100;
			for (int pro = 10; pro <= 1000; pro++) {
				double dis = 0;
				for (int i = 0; i < 250; i++) dis += 1.0 * (a[i] - pro) * (a[i] - pro);
				dis /= 250;
				if (dis < mind) {
					mind = dis;
					ans = pro;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <ctime>

#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
using namespace std;
typedef long long ll;
int const factor[10] = {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
int dat[100] = {
    1,2,4,10,26,76,232,764,2620,9496
};
int main() {
    int n;
    while (~scanf("%d", &n)) {
        int a, c1 = 0, c2 = 0;
        int ret = 1;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &a);
            if (a == 1) ++c1;
            if (a == 2) ++c2;
        }
        ret *= factor[c1 + c2 - 1];
        ret -= (factor[c1-1] - dat[c1-1]) * factor[c1+c2-1]/factor[c1-1];
        cout<<ret<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
typedef long long ll;
const ll mod = 1e6+3;

ll inv[10000010] = {1, 1}, fac[10000010] = {1, 1}, fi[10000010] = {1, 1};

void init()
{
    for(int i = 2; i <= 10000000; i++)
    {
        fac[i] = i * fac[i - 1] % mod;
        inv[i] = (mod - mod / i) * inv[mod % i] % mod;
        fi[i] = fi[i - 1] * inv[i] % mod;
    }
}

ll C(ll n, ll m)
{
    return fac[n] * fi[m] % mod * fi[n - m] % mod;
}

int main()
{
    init();
    ll n, c, ans = 0;
    cin >> n >> c;
    for(ll i = 1; i <= n; i++)
    {
        ans += C(i + c - 1, c - 1);
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops","omit-frame-pointer","inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC option("arch=native","no-zero-upper") //Enable AVX

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<ll,ll> pii;
typedef pair<ld,ll> pdi;
typedef pair<ld,ld> pdd;
typedef pair<ld,pii> pdp;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<string,string> pss;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;
typedef complex<ld> point;
typedef vector<point> polygon;
typedef pair<point,int> ppi;
#define prec(n) cout.precision(n); cout<<fixed
#define mod (ll)(1e9+7)
#define eps (1e-9)
const ll oo=(ll)(1e11);
#define pi acos(-1)
#define MAXN (ll)(1e6+5)

int szs[MAXN];
polygon ps[MAXN];
vector<ld> ds[MAXN];
ld as[MAXN];
char cs[MAXN];

double read0()
{
    scanf("%s",cs);
    double res=0;
    int p=0;
    bool past_dot=0;
    double p10=0.1;
    while(cs[p]!='\0')
    {
        if(cs[p]=='.')
        {
            past_dot=1;
            p++;
            continue;
        }
        double vp=cs[p]-'0';
        if(past_dot)
        {
            res+=vp*p10;
            p10/=10.0;
        }
        else
        {
            res*=10;
            res+=vp;
        }
        p++;
    }
    return res;
}

point read()
{
    double x=read0();
    double y=read0();
//    cin >> x >> y;
//    scanf("%f%f",&x,&y);
    return point(x,y);
}

#define NEXT(i) (((i) + 1) % n)

struct line { point p, q; };
using segment = line;

double dot(point a, point b) { return real(conj(a) * b); }

ld cross(point a, point b) { return imag(conj(a) * b); }

int ccw(point a, point b, point c)
{
	b -= a; c -= a;
	if (cross(b, c) > 0) return +1; // counter clockwise
	if (cross(b, c) < 0) return -1; // clockwise
	if (dot(b, c) < 0) return +2; // c--a--b on line
	if (dot(b, b) < dot(c, c)) return -2; // a--b--c on line
	return 0;
}

bool intersectSS(const segment &s, const segment &t)
{
	return ccw(s.p, s.q, t.p) * ccw(s.p, s.q, t.q) <= 0
			&& ccw(t.p, t.q, s.p) * ccw(t.p, t.q, s.q) <= 0;
}

ld area2(const polygon &P)
{
	ld A = 0;
	for (int i = 0, n = P.size(); i < n; ++i)
		A += cross(P[i], P[NEXT(i)]);
	return A/2;
}

ld min_dis(ld ab,ld ac,ld bc)
{
    ld cos_abc=(ac*ac-ab*ab-bc*bc)/(-2.0*ab*bc);
    ld cos_acb=(ab*ab-ac*ac-bc*bc)/(-2.0*ac*bc);
    if(min(cos_abc,cos_acb)<=eps)
        return min(ab,ac);

    return sqrt(1-cos_abc*cos_abc)*ab;
}

ld give_me_random_ang()
{
    ld ang=rand()%10000;
    ang/=10000;
    ang*=2*pi;
    return ang;
}

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

    prec(6);

    srand(time(0));

    ld w,h;
    int n;
//    cin >> w >> h >> n;
    scanf("%lf%lf%d",&w,&h,&n);
    for(int i=0;i<n;i++)
    {
//        cin >> szs[i];
        scanf("%d",&szs[i]);
        for(int j=0;j<szs[i];j++)
        {
            point p=read();
            ps[i].push_back(p);
//            cout << i << ' ' << j << ' ' << p.real() << ' ' << p.imag() << '\n';
        }
        for(int j=0;j<szs[i];j++)
            ds[i].push_back(abs(ps[i][j]-ps[i][(j+1)%szs[i]]));
        as[i]=area2(ps[i]);
    }

    int q;
//    cin >> q;
    scanf("%d",&q);
    while(q--)
    {
        ld r;
        point c=read();
//        cin >> rr;
        scanf("%lf",&r);
        ld a=0;
        vector<int> indexes;
        for(int i=0;i<n;i++)
        {
            vector<ld> ds2(szs[i]);
            bool ok=0;
            int c1=0;
            int c2=0;
//            ld a1=give_me_random_ang();
            ld a2=give_me_random_ang();
//            segment l1={c,c+point(oo*cos(a1),oo*sin(a1))};
            segment l2={c,c+point(oo*cos(a2),oo*sin(a2))};
            for(int j=0;j<szs[i];j++)
            {
                ds2[j]=abs(c-ps[i][j]);
//                c1+=intersectSS(l1,{ps[i][j],ps[i][(j+1)%szs[i]]});
                c2+=intersectSS(l2,{ps[i][j],ps[i][(j+1)%szs[i]]});
            }

            ok=(ok|((c1&1)||(c2&1)));

            for(int j=0;j<szs[i] && !ok;j++)
                ok=(ok|(min_dis(ds2[j],ds2[(j+1)%szs[i]],ds[i][j]))<=r+eps);
            if(ok)
            {
                a+=as[i];
                indexes.push_back(i);
            }
        }

        cout << a << ' ' << indexes.size();
        for(auto y : indexes)
            cout << ' ' << y;
        cout << '\n';
    }

    return 0;
}
/**
10 10 3
4 2 2 3 2 3 3 2 3
3 3.5 2 4.5 2 4.5 3
4 7 8 7.5 8.5 8 8 7.5 9
1
5 2.5 0.5

100 100 1
4 0 0 4 0 4 4 0 4
1
2 2 1

10 10 3
4 2 2 3 2 3 3 2 3
3 3.5 2 4.5 2 4.5 3
4 7 8 7.5 8.5 8 8 7.5 9
5
2 3.5 0.5
3.3 2 0.4
5 2.5 0.5
7.5 8.5 0.5
3 7 0.5
**/


#pragma comment(linker, "/STACK:65777216")

#include <algorithm>
#include <iostream>
#include <string>
#include<sstream>
#include<string.h>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include<stack>
#include <set>
#include <map>
#include<ctime>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef unsigned long long ull;

#define FOR(i,a,b) for (int i(a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define SORT(v) sort((v).begin(),(v).end())
#define UN(v) sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define CL(a,b) memset(a,b,sizeof(a))
#define pb push_back

typedef pair<double,int> pdi;

const double eps = 1e-9;
const double pi = acos(-1.);

int n;
int x[1111],y[1111],r[1111];

double getA(double a){
	if(a<0) return a+2*pi;
	return a;
}
void add(vector<pdi> &v, double a1,double a2){
	a1 = getA(a1);
	a2 = getA(a2);
	if(a1>a2) swap(a1,a2);
	if(a1+pi>a2){
		v.pb(pdi(a1+eps,1));
		v.pb(pdi(a2-eps,0));
	}else{
		v.pb(pdi(0,1));
		v.pb(pdi(a1-eps,0));
		v.pb(pdi(a2+eps,1));
		v.pb(pdi(2*pi,0));
	}
}

int solve(){
	int res = 1;
	REP(i,1){
		REP(j,n)if(i!=j)x[j]-=x[i],y[j]-=y[i];
		vector<pdi> v;
		REP(j,n)if(i!=j){
			double ang1[2];
			double ang2[2];
			REP(type,2){
				double c = (type?-r[i]:r[i]);
				//a*x[j]+b*y[j] = c - rr;
				//aa+bb=1
				if(x[j]){
					// a = (c-rr-by[j])/x[j]
					double A = (c-r[j])/x[j];
					double B = -y[j]/double(x[j]);
					// a = A+Bb
					//(AA-1)+2ABb+(BB+1)bb=1
					double k1 = A*A-1;
					double k2 = 2*A*B;
					double k3 = B*B+1;
					double D = k2*k2-4*k1*k3;
					D = sqrt(D);

					double b1 = (-k2-D)/2/k3;
					double b2 = (-k2+D)/2/k3;
					double a1 = A + B*b1;
					double a2 = A + B*b2;
					if(a1<0) a1=-a1,b1=-b1,c=-c,a2=-a2,b2=-b2;
					if(a1+eps<a2 || a1<a2+eps && b1+eps<b2) swap(a1,a2),swap(b1,b2);

					//a1x + b1y = c
					/*cout<<a1<<' '<<b1<<' '<<a1*x[j]+b1*y[j]-c<<endl;
					cout<<a2<<' '<<b2<<' '<<a2*x[j]+b2*y[j]-c<<endl;
					puts("");
					*/
					double x0 = a1 * r[i];
					double y0 = b1 * r[i];
					double x1 = a2 * r[i];
					double y1 = b2 * r[i];

					/*cout<<x0<<' '<<y0<<' '<<x0*a1+y0*b1-c<<endl;
					cout<<x1<<' '<<y1<<' '<<x1*a2+y1*b2-c<<endl;
					puts("");
					*/
					ang1[type] = atan2(y0,x0);
					ang2[type] = atan2(y1,x1);
				}else{
					double b1 = c/y[j];
					double b2 = b1;
					double a1 = sqrt(1-b1*b1);
					double a2 = -a1;
					if(a1<0) a1=-a1,b1=-b1,c=-c,a2=-a2,b2=-b2;
					if(a1+eps<a2 || a1<a2+eps && b1+eps<b2) swap(a1,a2),swap(b1,b2);

					//a1x + b1y = c
					/*cout<<a1<<' '<<b1<<' '<<a1*x[j]+b1*y[j]-c<<endl;
					cout<<a2<<' '<<b2<<' '<<a2*x[j]+b2*y[j]-c<<endl;
					puts("");
					*/
					double x0 = a1 * r[i];
					double y0 = b1 * r[i];
					double x1 = a2 * r[i];
					double y1 = b2 * r[i];

					/*cout<<x0<<' '<<y0<<' '<<x0*a1+y0*b1-c<<endl;
					cout<<x1<<' '<<y1<<' '<<x1*a2+y1*b2-c<<endl;
					puts("");
					*/
					ang1[type] = atan2(y0,x0);
					ang2[type] = atan2(y1,x1);
				}
			}
			REP(i,2) ang1[i]=getA(ang1[i]),ang2[i]=getA(ang2[i]);
			sort(ang1,ang1+2);
			sort(ang2,ang2+2);
			//cout<<"ANG1 "<<ang1[0]<<' '<<ang1[1]<<endl;
			add(v,ang1[0],ang1[1]);
			//cout<<"ANG2 "<<ang2[0]<<' '<<ang2[1]<<endl;
			add(v,ang2[0],ang2[1]);
		}
		REP(j,n)if(i!=j)x[j]-=x[i],y[j]-=y[i];
		SORT(v);
		int cnt=1;
		//REP(i,v.size()) cout<<"("<<v[i].first<<", "<<v[i].second<<") ";puts("");

		REP(j,v.size()){
			if(v[j].second) cnt++;
			else cnt--;
			res=max(res,cnt);
		}
	}
	return res;
}

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

	ll k;
	cin>>n>>k;
	REP(i,n) scanf("%d %d %d",x+i,y+i,r+i);
	cout<<n+(2*solve()+k-1)*k/2<<endl;

#ifdef LocalHost
	cout<<endl<<endl<<"TIME: "<<clock()<<endl;
#endif
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N=2e5+10;

int gi() {
	int x=0,o=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-') o=-1,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*o;
}

int n,m,k,dx[6]={1,-1,0,0,0,0},dy[6]={0,0,1,-1,0,0},dz[6]={0,0,0,0,1,-1},a[N];
vi vec[N];

void No() {
	cout<<-1;exit(0);
}

int id(int x,int y,int z) {
	return (x-1)*m*k+(y-1)*k+z;
}

bool in(int x,int y,int z) {
	return 1<=x&&x<=n&&1<=y&&y<=m&&1<=z&&z<=k;
}

void erase(int,int,int);

void insert(int d,int x,int y,int z,int c) {
	if(!in(x,y,z)) {
		if(c) No(); return;
	}
	int t=id(x,y,z);
	if(!c) {
		if(a[t]>0) erase(x,y,z); a[t]=0;
		insert(d,x+dx[d],y+dy[d],z+dz[d],c);
	}
	else if(!~a[t]||a[t]==c) {
		a[t]=c;vec[t].pb(d);
	}
	else if(!a[t]) insert(d,x+dx[d],y+dy[d],z+dz[d],c);
	else erase(x,y,z),a[t]=0,insert(d,x+dx[d],y+dy[d],z+dz[d],c);
}

void erase(int x,int y,int z) {
	int t=id(x,y,z);
	while(!vec[t].empty()) {
		int d=vec[t].back();vec[t].pop_back();
		insert(d,x+dx[d],y+dy[d],z+dz[d],a[t]);
	}
}

int main() {
	memset(a,-1,sizeof(a));
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=k;j++) insert(0,1,i,j,gi());
	for(int i=1;i<=m;i++)
		for(int j=1;j<=k;j++) insert(1,n,i,j,gi());
	for(int i=1;i<=n;i++)
		for(int j=1;j<=k;j++) insert(2,i,1,j,gi());
	for(int i=1;i<=n;i++)
		for(int j=1;j<=k;j++) insert(3,i,m,j,gi());
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) insert(4,i,j,1,gi());
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) insert(5,i,j,k,gi());
	for(int i=1;i<=n*m*k;i++) cout<<max(0,a[i])<<' ';
	return 0;
}


// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

void solve() {
	int n;
	cin >> n;
	vector<int> color(n);
	for (int& num : color) {
		cin >> num;
	}
	vector<vector<int>> graph(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	int ans = INF;

	int cnt1 = 0, cnt2 = 0;
	{
		vector<bool> used(n);
		function<void(int)> dfs = [&](int v) {
			used[v] = true;
			for (int to : graph[v]) {
				if (color[to] == color[v] && !used[to]) {
					dfs(to);
				}
			}
		};
		for (int i = 0; i < n; ++i) {
			if (!used[i]) {
				dfs(i);
				if (color[i] == 1) {
					++cnt1;
				}
				if (color[i] == 2) {
					++cnt2;
				}

			}
		}
	}

	ans = min(ans, 1 + cnt2);
	ans = min(ans, 1 + cnt1);

	for (int qwe = 0; qwe < 2; ++qwe) {
		vector<int> sz(n);
		const int MAXSZ = 4000;
		vector<vector<int>> dp(n, vector<int>(MAXSZ, INF));
		vector<int> next_dp(MAXSZ, INF);
		function<void(int, int)> dfs = [&](int v, int p) {
			// vector<int> dp;
			if (color[v] == 1 || color[v] == 0) {
				dp[v][0] = 1;
				dp[v][2] = 3;
			} else {
				// dp = {INF, 2};
				dp[v][1] = 2;
			}
			sz[v] = 1;
			for (int to : graph[v]) {
				if (to != p) {
					dfs(to, v);
					int mem_sz = sz[v] + 2;
					sz[v] += sz[to];
					// vector<int> next_dp(min(MAXSZ, sz[v] + 2), INF);
					int tmp_sz = min(MAXSZ, sz[v] + 2);
					for (int i = 0; i < tmp_sz; ++i) {
						int dp_val = INF;
						if (i < mem_sz) {
							dp_val = dp[v][i];
						} else {
							if ((color[v] == 1 || color[v] == 0) && i % 2 == 0) {
								dp_val = i + 1;
							}
							if ((color[v] == 2 || color[v] == 0) && i % 2 == 1) {
								dp_val = i + 1;
							}
						}
						for (int j = 0; j < sz[to] + 2 && j < MAXSZ; ++j) {
							next_dp[i] = min(next_dp[i], dp_val + dp[to][j] - min(i, j) - 1);
						}
					}
					swap(next_dp, dp[v]);
					fill(next_dp.begin(), next_dp.begin() + min(MAXSZ, mem_sz), INF);
				}
			}
			// return dp;
		};

		dfs(0, 0);
		ans = min(ans, *min_element(dp[0].begin(), dp[0].end()));

		for (int& num : color) {
			if (num == 1) {
				num = 2;
			} else if (num == 2) {
				num = 1;
			}
		}
	}

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

int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin » s;
string temp = "";
int pi = s.size() - 1;
for (int i = 0; i <= pi; i++) {
temp += s[0];
int dl = temp.size();
int dl2 = dl + (s.size() - dl) / 2;
if (s.substr(((s.size() - dl) / 2), dl) == temp)
{
if (s.substr(0, dl2) == s.substr(dl2 - dl, dl2)) {
cout « "YES\n";
cout « temp;
return 0;
}
}
}
cout « "NO";
}
21.03.16

Юрий
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin » s;
string temp = "";
int pi = s.size() - 1;
for (int i = 0; i <= pi-1; i++) {
temp += s[i];
int dl = temp.size();
int dl2 = dl + (s.size() - dl) / 2;
if ((s.size() - dl) %2==0&&s.substr(((s.size() - dl) / 2), dl) == temp)
{
if (s.substr(0, dl2) == s.substr(dl2 - dl, dl2)) {
cout « "YES\n";
cout « s.substr(0, dl2);
return 0;
}
}
}
cout « "NO";
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define for1(i, n) for(int i = 1; i <= int(n); ++i)
#define ford(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define fore(i, l, r) for(int i = int(l); i < int(r); ++i)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define X first
#define Y second
#define mp make_pair
#define debug(x) {cerr << #x << " = " << x << endl;}
template<typename T> inline T abs(T a){ return ((a < 0) ? -a : a); }
template<typename T> inline T sqr(T a){ return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

const int INF = (int)1E9 + 7;
const ld EPS = 1E-9;
const ld PI = 3.1415926535897932384626433832795;

const int NMAX = 6000;

int n;
vector<int> a[NMAX];
vector<int> ls[NMAX];
int it[NMAX], git[NMAX];

int ks[NMAX], a0s[NMAX], xs[NMAX], ys[NMAX], ms[NMAX];

int main() {
    #ifdef myproject
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif

    scanf("%d", &n);

    int ans = 0;

    int sumk = 0;

    forn(i, n){
        int k, a0, x, y, m;
        scanf("%d%d%d%d%d", &k, &a0, &x, &y, &m);
        ks[i] = k, a0s[i] = a0, xs[i] = x, ys[i] = y, ms[i] = m;
        sumk += k;
    }
//        cerr << "x = " << x << endl;

    if(sumk <= 200000){
        forn(i, n){
            int k = ks[i], a0 = a0s[i], x = xs[i], y = ys[i], m = ms[i];

            a[i].pb(a0);
            forn(j, k-1){
                int na = (li(a[i].back() * 1LL * x) + y) % m;
                a[i].pb(na);
            }

            forn(j, k){
                int t = j;
                while(t+1 < sz(a[i]) && a[i][t] <= a[i][t+1])
                    t++;

                ls[i].pb(t);

                j = t;
            }

            ans = max(ans, sz(ls[i]) - 1);
    //        cerr << endl;

            sumk += k;
        }

        set<pt> s, qs;

        forn(i, n)
            s.insert(mp(a[i][0], i));

        printf("%d\n", ans);
        while(!s.empty()){
    //        qs.clear();

            while(!s.empty()){

                int i = s.begin()->Y;
                int v = s.begin()->X;
                s.erase(s.begin());

                printf("%d %d\n", v, i+1);

                if(it[i] != ls[i][git[i]]){
                    it[i]++;
                    s.insert(mp(a[i][it[i]], i));
                }else{
                    if(it[i] != sz(a[i]) - 1){
                        it[i]++, git[i]++;
                        qs.insert(mp(a[i][it[i]], i));
                    }
                }
            }

            s = qs;
            qs.clear();
        }
    }else{
        forn(i, n){
            int k = ks[i], a0 = a0s[i], x = xs[i], y = ys[i], m = ms[i];

            int pa = a0, cur = 0;
            forn(j, k-1){
                int na = (li(pa * 1LL * x) + y) % m;

                if(na < pa) cur++;
                pa = na;
            }
            ans = max(ans, cur);
        }
        printf("%d\n", ans);
    }

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int mod = MOD;
struct ModInt {
  unsigned val;
  ModInt(): val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {}
  ModInt pow(long long exponent) {
    ModInt tmp = *this, res = 1;
    while (exponent > 0) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
      exponent >>= 1;
    }
    return res;
  }
  ModInt &operator+=(const ModInt &rhs) { if((val += rhs.val) >= mod) val -= mod; return *this; }
  ModInt &operator-=(const ModInt &rhs) { if((val += mod - rhs.val) >= mod) val -= mod; return *this; }
  ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; }
  ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); }
  bool operator==(const ModInt &rhs) const { return val == rhs.val; }
  bool operator!=(const ModInt &rhs) const { return val != rhs.val; }
  bool operator<(const ModInt &rhs) const { return val < rhs.val; }
  bool operator<=(const ModInt &rhs) const { return val <= rhs.val; }
  bool operator>(const ModInt &rhs) const { return val > rhs.val; }
  bool operator>=(const ModInt &rhs) const { return val >= rhs.val; }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; }
  friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; }
  friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; }
private:
  ModInt inv() const {
    // if (__gcd((int)val, mod) != 1) assert(false);
    unsigned a = val, b = mod; int x = 1, y = 0;
    while (b) {
      unsigned tmp = a / b;
      a -= tmp * b; swap(a, b);
      x -= tmp * y; swap(x, y);
    }
    return ModInt(x);
  }
};
ModInt abs(const ModInt &x) { return x.val; }
struct Combinatorics {
  Combinatorics(int MAX = 5000000) {
    MAX <<= 1;
    fact.resize(MAX + 1);
    fact_inv.resize(MAX + 1);
    fact[0] = 1;
    FOR(i, 1, MAX + 1) fact[i] = fact[i - 1] * i;
    fact_inv[MAX] = ModInt(1) / fact[MAX];
    for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i;
  }
  ModInt nCk(int n, int k) {
    if (n < 0 || n < k || k < 0) return ModInt(0);
    return fact[n] * fact_inv[k] * fact_inv[n - k];
  }
  ModInt nPk(int n, int k) {
    if (n < 0 || n < k || k < 0) return ModInt(0);
    return fact[n] * fact_inv[n - k];
  }
  ModInt nHk(int n, int k) {
    if (n < 0 || k < 0) return ModInt(0);
    return (k == 0 ? ModInt(1) : nCk(n + k - 1, k));
  }
private:
  vector<ModInt> fact, fact_inv;
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  vector<int> a(n); REP(i, n) cin >> a[i];
  vector<int> w(n); REP(i, n) cin >> w[i];
  int dl = 0, l = 0;
  REP(i, n) (a[i] == 0 ? dl : l) += w[i];
  vector<vector<ModInt> > dp(m + 1, vector<ModInt>(m + 1, 0));
  dp[0][0] = 1;
  REP(i, m) REP(j, m) {
    int sum = l + dl - j + (i - j);
    if (j > dl || j > i || sum <= 0) continue;
    dp[i + 1][j + 1] += dp[i][j] * (dl - j) / sum;
    dp[i + 1][j] += dp[i][j] * (l + (i - j)) / sum;
  }
  vector<ModInt> ans(2, 0);
  ModInt tmp = 1;
  REP(i, dl + 1) {
    if (i > m) break;
    ans[0] += tmp * dp[m][i];
    if (i < dl) tmp *= (ModInt(1) - ModInt(1) / (dl - i));
  }
  tmp = 1;
  REP(i, l + 1) {
    if (i > m) break;
    ans[1] += tmp * dp[m][m - i];
    tmp *= (ModInt(1) + ModInt(1) / (l + i));
  }
  REP(i, n) cout << ans[a[i]] * w[i] << '\n';
  return 0;
}


// All Heil Fire Lord Zuko

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

#define int long long

int ans, check = 1, num = 0, string s;

int32_t main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ans = 0;
    int check = 1;
    int num = 0;
    string s;
    cin >> s;
    for(auto i : s) {
        if (i == '-' || i == '+') {
            ans += check * num;
            if (i == '-')
                check = -1;
            else
                check = 1;
            num = 0;
        }
        num = 10 * num + (i - '0');
    }
    ans += check * num;
    cout << ans;
	return 0;
}


#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=230010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k;
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n=read();m=read();k=read();
	for(int i=1;i<=n;++i){
		scanf("%s",s1+1);
		for(int j=1;j<=m;++j){
			(s[j]=='#'?bl:cn).set((i-1)*m+j);
			(s[j]=='E'?e.set((i-1)*m+j):0);
		}
	}
	scanf("%s",s2+1);
	ch=cn;
	for(int i=1;i<=k;++i){
		if(s[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
		if(ch==e){
		 write(i);return 0;
		}
	}
	write(-1);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(a,b,c) for(int a=(b),a##_end__=(c);a<a##_end__;++a)
template<class T>inline bool chkmin(T&a,T const&b){return a>b?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T const&b){return a<b?a=b,true:false;}
const int M=500005;
struct Queue{
	int pos[M<<1],val[M<<1];
	int head,tail;
	void clear(){head=tail=0;}
	void push(int r,int x){
		while(head<tail and val[tail-1]<x) --tail;
		pos[tail]=r;
		val[tail]=x;
		++tail;
	}
	int maxn(int l){
		if(head<tail and pos[head]<l) ++head;
		return head<tail?val[head]:0;
	}
}Q0,Q1;
struct edge{
	int to;
	edge*next;
}*E[M],*G[M<<1],Pool[M<<2],*allc=Pool;
int dfn[M],low[M],Fa[M],dep[M];
int Q[M],A[M][2],B[M],val[M*3],id[M];
int n,m,top,dfn_cnt,scc_cnt;
void add_edge(int u,int v,edge **G){
	*allc=(edge){v,G[u]};
	G[u]=allc++;
}
void solve_ring(int u,int v){
	int w=++scc_cnt;
	int len=dep[v]-dep[u]+1;
	for(int x=v,d=1;x!=u;x=Fa[x],++d){
		add_edge(w,x,G);
		chkmax(B[w-n],A[x][0]+min(d,len-d));
	}
	int tmp=B[w-n];
	FOR(k,0,2) if(tmp>A[u][k]) swap(tmp,A[u][k]);
	add_edge(u,w,G);
}
void Tarjan(int u,int fa){
	low[u]=dfn[u]=++dfn_cnt;
	for(edge *e=u[E];e;e=e->next){
		int v=e->to;
		if(v==fa) continue;
		if(!v[dfn]){
			dep[v]=dep[u]+1;
			Tarjan(v,Fa[v]=u);
			chkmin(low[u],low[v]);
		}else chkmin(low[u],dfn[v]);
	}
	for(edge *e=u[E];e;e=e->next){
		if((Fa[e->to]!=u and low[e->to]==dfn[u]) or low[e->to]>dfn[u])
			solve_ring(u,e->to);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	while(m--){
		int u,v;
		scanf("%d%d",&u,&v);
		add_edge(u,v,E);
		add_edge(v,u,E);
	}
	scc_cnt=n;
	Tarjan(1,-1);
	int l=0,r=0;
	Q[r++]=1;
	while(l<r){
		int u=Q[l++];
		for(edge*e=G[u];e;e=e->next){
			int w=e->to,sz=0;
			Q0.clear(),Q1.clear();
			val[sz]=A[u][B[w-n]==A[u][0]];
			id[sz++]=u;
			for(edge*e=G[w];e;e=e->next){
				Q[r++]=id[sz]=e->to;
				val[sz]=A[e->to][0];
				++sz;
			}
			FOR(i,0,sz) val[i+sz+sz]=val[i+sz]=val[i];
			int len0=sz>>1,pos0=sz+1;
			int len1=sz-len0-1,pos1=sz+len1+2;
			FOR(i,pos0-len0,pos0) Q0.push(i,val[i]-i);
			FOR(i,pos1-len1,pos1) Q1.push(i,val[i]+i);
			FOR(i,1,sz){
				int u=id[i];
				int x=Q0.maxn(pos0-len0)+i+sz;
				int y=Q1.maxn(pos1-len1)-i-sz;
				FOR(k,0,2) if(x>A[u][k]) swap(x,A[u][k]);
				FOR(k,0,2) if(y>A[u][k]) swap(y,A[u][k]);
				Q0.push(pos0,val[pos0]-pos0),++pos0;
				Q1.push(pos1,val[pos1]+pos1),++pos1;
			}
		}
	}
	FOR(i,1,n+1) printf("%d ",A[i][0]);
	puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9')  f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9')  x=x*10+c-'0',c=getchar();
    return x*f;
}
typedef unsigned long long ull;
const int INF=1e9;
const int N=2e5+5;
struct node{
  ull mx:18,ls:23,rs:23;
}a[N*40];
int tot=0,ra[N];
int update(int p,int l,int r,int ql,int qr){
	if(l==r)
		return p+1;
	int o=++tot;a[o]=a[p];
	if(ql<=l&&r<=qr)
		return ++a[o].mx,o;
	int mid=(l+r)>>1,tag=a[o].mx-max((l==mid?int(a[o].ls):int(a[a[o].ls].mx)),(mid+1==r?int(a[o].rs):int(a[a[o].rs].mx)));
	if(ql<=mid)
		a[o].ls=update(a[p].ls,l,mid,ql,qr);
	if(qr>mid)
		a[o].rs=update(a[p].rs,mid+1,r,ql,qr);
	a[o].mx=max((l==mid?int(a[o].ls):int(a[a[o].ls].mx)),(mid+1==r?int(a[o].rs):int(a[a[o].rs].mx)));
	a[o].mx+=tag;
	return o;
}
int query(int o,int l,int r,int ql,int qr){
	if(l==r)
		return o;
	if(ql<=l&&r<=qr)
		return a[o].mx;
	int mid=(l+r)>>1,res=0,tag=a[o].mx-max((l==mid?int(a[o].ls):int(a[a[o].ls].mx)),(mid+1==r?int(a[o].rs):int(a[a[o].rs].mx)));
	if(ql<=mid)
		res=max(res,query(a[o].ls,l,mid,ql,qr));
	if(qr>mid)
		res=max(res,query(a[o].rs,mid+1,r,ql,qr));
	return res+tag;
}
pair<int,int>p[N];
int main(){
	int n=read(),m=read();
	for(int i=1;i<=N;i++)
		p[i].first=-1*read(),p[i].second=i;
	sort(&p[1],&p[N+1]);
	for(int i=1;i<=n;i++){
		int ql=max(1,p[i].second-m+1);
		int qr=min(p[i].second,n-m+1);
		ra[i]=update(ra[i-1],1,n-m+1,ql,qr);
	}
	int Q=read(),ans=0;
	while(Q--){
		int l=read(),r=read(),x=read();
		x^=ans;
		int id=upper_bound(&p[1],&p[n+1],make_pair(-x,INF))-p-1;
		printf("%d\n",ans=m-query(ra[id],1,n-m+1,l,r));
	}
}


#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <deque>
#define _USE_MATH_DEFINES
#include <math.h>
#include <string>
using namespace std;
void main()
{
	//ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	int n,m;
	vector <int> a,b;
	b.resize(2000000,-1);
	scanf("%d %d",&n,&m);
	a.resize(2*n);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		cin>>a[i];
		a[i]--;
		a[n+i]=a[i];
	}
	__int64 q;
	for (int i=0;i<m;i++)
	{
		cin>>q;
		q--;
		b[q]=i;
	}
	__int64 res=0;
	__int64 l=-1;
	deque <__int64> qq;
	for (int r=0;r<2*n;r++)
	{
		if (b[a[r]]==-1)
		{
			l=-1;
			qq.clear();
			continue;
		}
		q=b[a[r]];
		if (!qq.empty())
		{
			if (q<qq.back())
			{
				q+=m*(qq.back()/m);
				while (q<qq.back()) q+=m;
			}
			while (!qq.empty()&&(q-qq.front()>m))
				qq.pop_front();
		}
		qq.push_back(q);
		if (qq.size()<=m&&qq.size()<=n)
			res=max<__int64> (res,qq.size());
	}
	printf("%d",res);
}res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
char a,b;
vector<int> opt;
int qxx[2000][2],tt[2000],nn,jz[2000],rd[2000];
bool te,hb[2000];
void ad(int u,int v){
	++rd[v];
	qxx[++nn][0]=v;
	qxx[nn][1]=tt[u];
	tt[u]=nn;
}
#define bl(u,i)	for(int i=tt[u];i;i=qxx[i][1])
#define _r scanf("%c",&a);
int n1,n2;
void dfs(int now){
	hb[now]=true;
	if(tt[now]==0){
		jz[now]=1;
		return;
	}
	bl(now,i){
		if(hb[qxx[i][0]]){
			te=true;
			return;
		}
		dfs(qxx[i][0]);
		jz[now]=max(jz[now],jz[qxx[i][0]]+1);
	}
	if(jz[now]>10)	te=true;
	return;
}
int main(){
	_r
	_r
	while(a!=')'){
		_r;
		if(a=='_')	++n1;
		else if(n1!=0)	opt.push_back(n1),n1=0;
	}
	_r
	_r
	while(a!='.'){
		n1=n2=0;
		_r;
		while(a=='_'){
			++n1;_r
		}
		b=a;
		_r
		while(a=='_'){
			++n2;_r
		}
		if(n1==n2){
			printf("false");
			return 0;
		}
		if(b=='<')	ad(n2,n1);
		else	ad(n1,n2);
	}
	for(int i=0;i<opt.size();i++){
		if(!hb[opt[i]] && !rd[opt[i]])	dfs(opt[i]);
		if(te){
			printf("false");
			return 0;
		}
	}
	for(int i=0;i<opt.size();i++)	printf("%d",jz[opt[i]]-1);
	return 0;
}


#include <cmath>
#include <iostream>

using namespace std;

int radio;
long long res=0, son=0, col;
long long double x=-.5, y=.5;

double dist(double x1, double y1, double x2, double y2){
    return sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) );
}

bool dentro(double uno, double dos){
    bool sip=true;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    uno+=1;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    uno+=.5;
    dos-=sqrt(3.0)/2.0;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    uno-=.5;
    dos-=sqrt(3.0)/2.0;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    uno-=1;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    uno-=.5;
    dos+=sqrt(3.0)/2.0;
    sip=sip&(dist(uno, dos, 0, 0)<=radio);
    return sip;
}

int main()
{
    cin>>radio;
    while(dentro(x, y*sqrt(3.0))){
        ++res;
        ++y;
    }
    --y;
    col=res-1;
    res+=res-1;
    for(int va=1; y>0; va++){
        if(va%2==1){
            ++col;
        }
        x+=1.5;
        y+=.5;
        while(!dentro(x, y*sqrt(3.0)) and y>0){
            --col;
            --y;
        }
        if(!dentro(x, y*sqrt(3.0))){
            res+=son*2;
            cout<<res<<"\n";
            return 0;
        }
        else {
            son+=col*2;
            if(va%2==0){
                ++son;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = 1e5 + 17;
const int INF = 1e9 + 17;
long long n, a, b, m, k, Ans;
int main ()
{
    #ifdef Elibay
        freopen (".in", "r", stdin);
    #endif
    cin >> n >> m >> k;
    cin >> a >> b;
    long long pod1 = 1;
    for (; pod1 <= n; ++ pod1)
    	if (pod1 * m * k >= a)
    		break;
    long long pod2 = 1;
    for (; pod2 <= n; ++ pod2)
    	if (pod2 * m * k >= b)
    		break;
    Ans += min (abs (pod2 - pod1), (n - max (pod1, pod2) + min (pod1, pod2))) * 15;
    a -= (pod1 - 1) * m * k;-
    b -= (pod2 - 1) * m * k;
    long long etzh = (a - 1) / k;
    long long etzh2 = (b - 1) / k;
    Ans += min (10 + etzh, 5 * etzh);
    Ans += min (10 + etzh2, 5 * etzh2);
    cout << Ans;
    return 0;
}


#include <bits/stdc++.h>

#define ll long long

using namespace std;

void Find_size(int v);
void Build(int root, vector<int> &values);
void Solve();

ll RAND(ll n) {
  return abs((rand() << 15) + rand()) % n;
}

int n;
vector<vector<int>> g;
vector<vector<int>> edges;
vector<bool> used;
vector<int> size;

int Find_centroid(int v, int sz) {
  used[v] = true;
  Find_size(v);
  int max_size = -1;
  for (auto to:g[v]) {
    if (!used[to] && size[to] > sz / 2 && (max_size == -1 || size[to] > size[max_size])) {
      max_size = to;
    }
  }
  int ans;
  if (max_size == -1) {
    ans = v;
  } else {
    ans = Find_centroid(max_size, sz);
  }
  used[v] = false;
  return ans;
}

void Find_size(int v) {
  size[v] = 1;
  used[v] = true;
  for (auto to:g[v]) {
    if (!used[to]) {
      Find_size(to);
      size[v] += size[to];
    }
  }
  used[v] = false;
}

void Build(int root, vector<int> &values) {
  vector<int> val(values.size() + 1, 0);
  int cur_ind = 0;
  used[root] = true;
  queue<int> q;
  q.push(root);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (auto to:g[cur]) {
      if (!used[to]) {
        used[to] = true;
        q.push(to);
        edges[cur][to] = values[cur_ind++] - val[cur];
        edges[to][cur] = edges[cur][to];
        val[to] = val[cur] + edges[cur][to];
      }
    }
  }
}

vector<int> parent;
vector<int> cap;

void make_set(int x) {
  parent[x] = x;
  cap[x] = size[x];
}

int find_set(int x) {
  if (x == parent[x])
    return x;
  return parent[x] = parent[find_set(parent[x])];
}

void union_sets(int x, int y) {
  int a = find_set(x);
  int b = find_set(y);
  if (a != b) {
    if (cap[a] < cap[b])
      swap(a, b);
    parent[b] = a;
    cap[a] += cap[b];
  }
}

void Solve() {
  int c = Find_centroid(0, n);
  Find_size(c);

  auto compare = [](int lhs, int rhs) {
    return cap[lhs] > cap[rhs];
  };
  std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);

  for (int i = 0; i < n; ++i) {
    if (i == c) {
      continue;
    }
    make_set(i);
    q.push(i);
  }

  while (q.size() > 2) {
    int x = q.top();
    q.pop();
    int y = q.top();
    q.pop();
    union_sets(x, y);
    q.push(find_set(x));
  }
  int x = q.top();
  q.pop();
  int y = q.top();
  q.pop();
  vector<int> gr1(cap[find_set(x)]);
  vector<int> gr2(cap[find_set(y)]);
  for (int i = 0; i < (int) gr1.size(); ++i) {
    gr1[i] = i + 1;
  }
  for (int i = 0; i < (int) gr2.size(); ++i) {
    gr2[i] = (i + 1) * gr1.size();
  }
  for (auto i:g[c]) {
    if (find_set(i) == y) {
      used[i] = true;
    }
  }
  Build(c, gr1);
  for (auto i:g[c]) {
    if (find_set(i) == y) {
      used[i] = false;
    }
  }
  Build(c, gr2);
  for (int i = 0; i < n; ++i) {
    used[i] = false;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  g.resize(n);
  used.resize(n);
  edges.resize(n, vector<int>(n, -1));
  size.resize(n);
  parent.resize(n);
  cap.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  Solve();
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (edges[i][j] >= 0) {
        cout << i + 1 << ' ' << j + 1 << ' ' << edges[i][j] << endl;
      }
    }
  }
  return 0;
}


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

const int p1 = 998244353, p2 = 469762049, Maxn = 1 << 17 | 5;
int maxn, maxa, mod, rev[Maxn];
int tot, l[Maxn];
const long long lcm = p1 * (long long) p2;
void get_rev(int len)
{
	for (int i = 0; i < len; i++)
		rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (len >> 1));
}
long long fast_pow(long long x, long long y, int p)
{
	long long ans = 1, now = x;
	while (y)
	{
		if (y & 1) ans = ans * now % p;
		now = now * now % p;
		y >>= 1;
	}
	return ans;
}
void NTT(int now[], int len, int p, bool type = false)
{
	l[0] = 1;
	int inv3 = fast_pow(3, p - 2, p);
	for (int i = 0; i < len; i++)
		if (i < rev[i]) swap(now[i], now[rev[i]]);
	for (int i = 1; i < len; i <<= 1)
	{
		long long w = fast_pow(type ? inv3 : 3, (p - 1) / (i << 1), p);
		for (int j = 1; j < i; j++)
			l[j] = l[j - 1] * w % p;
		for (int j = 0; j < len; j += (i << 1))
			for (int k = j; k < i + j; k++)
			{
				int x = now[i + k] * (long long) l[k - j] % p, y = now[k];
				now[k] = (x + y) % p;
				now[i + k] = (y - x + p) % p;
			}
	}
	if (type)
	{
		long long inv = fast_pow(len, p - 2, p);
		for (int i = 0; i < len; i++)
			now[i] = now[i] * inv % p;
	}
}
void multi(int x[], int y[], int result[], int len, int p)
{
	NTT(x, len, p), NTT(y, len, p);
	for (int i = 0; i < len; i++)
		result[i] = x[i] * (long long) y[i] % p;
	NTT(result, len, p, true);
}
long long mul(long long x, long long y)
{
	long long ans = 0, now = x;
	while (y)
	{
		if (y & 1) ans = (ans + now) % lcm;
		now = (now + now) % lcm;
		y >>= 1;
	}
	return ans;
}
void multi(int x[], int y[], int result[], int len)
{
	get_rev(len);
	static int tmp1[Maxn], tmp2[Maxn], tmpx[Maxn], tmpy[Maxn];
	for (int i = 0; i < len; i++)
		tmpx[i] = x[i], tmpy[i] = y[i];
	multi(tmpx, tmpy, tmp1, len, p1);
	for (int i = 0; i < len; i++)
		tmpx[i] = x[i], tmpy[i] = y[i];
	multi(tmpx, tmpy, tmp2, len, p2);
	for (int i = 0; i < len; i++)
		result[i] = (mul(mul(tmp1[i], p2), fast_pow(p2, p1 - 2, p1)) + mul(mul(tmp2[i], (__int128) p1), fast_pow(p1, p2 - 2, p2))) % lcm % mod;
}
void poly_pow(int y, int len)
{
	int now = 0;
	static int ans[2][Maxn];
	memset(ans[0], 0, sizeof(int[len << 1]));
	memset(ans[1], 0, sizeof(int[len << 1]));
	static int tmp[Maxn], result[2][Maxn];
	for (int j = 20; j >= 0; j--)
	{
		memset(tmp, 0, sizeof(int[len << 1]));
		for (int i = 0; i < len; i++)
			tmp[i] = (ans[0][i] + ans[1][i]) % mod;
		tmp[0]++;
		for (int p = 0; p <= 1; p++)
			multi(tmp, ans[(now & 1) ^ p], result[p], len << 1);
		for (int p = 0; p <= 1; p++)
			for (int i = 0; i < len; i++)
				(ans[p][i] += result[p][i]) %= mod;
		now <<= 1;
		if (y & (1 << j))
		{
			for (int i = len - 1; i; i--)
				(ans[1][i] += ans[0][i - 1] + ans[1][i - 1]) %= mod;
			ans[1][1]++;
			now++;
		}
		if (now != y)
			for (int i = 1; i <= maxn; i += 2)
				(tot += ans[1][i]) %= mod;
	}
}
int lower(int x)
{
	int tmp = 1;
	for (; tmp < x; tmp <<= 1);
	return tmp;
}
int main()
{
	scanf("%d%d%d", &maxn, &maxa, &mod);
	poly_pow(maxa, lower(maxn + 1));
	printf("%d", tot);
	return 0;
}