#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
const int N_MAX = 50;
int N;
char str[N_MAX];
int cnt[N_MAX];

inline bool can00() {
  return cnt[0] >= 2;
}
inline bool can25() {
  return cnt[2] && cnt[5];
}
inline bool can50() {
  return cnt[5] && cnt[0];
}
inline bool can75() {
  return cnt[7] && cnt[5];
}

inline int calc(int l, int r) {
  int cnt = 0;
  char tmp[N_MAX];
  memcpy(tmp, str, sizeof(str));
  for (int i = N; i >= 1; i--) {
    if (tmp[i] == r) {
      while(i < N) swap(tmp[i], tmp[i+1]), cnt++, i++;
      break;
    }
  }
  for (int i = N-1; i >= 1; i--) {
    if (tmp[i] == l) {
      while(i < N-1) swap(tmp[i], tmp[i+1]), cnt++, i++;
      break;
    }
  }
  for (int i = 1; i <= N - 2; i++) {
    if (tmp[i] != 0) {
      while(i > 1) swap(tmp[i], tmp[i-1]), cnt++, i--;
      break;
    }
  }
  return cnt;
}
int main() {
  scanf("%s", str + 1);
  N = (int) strlen(str + 1);
  for (int i = 1; i <= N; i++) {
    cnt[str[i]-='0']++;
  }
  int ans = 0x3f3f3f3f;
  if (can00()) ans = min(ans, calc(0, 0));
  if (can25()) ans = min(ans, calc(2, 5));
  if (can50()) ans = min(ans, calc(5, 0));
  if (can75()) ans = min(ans, calc(7, 5));
  if (ans == 0x3f3f3f3f) ans = -1;
  printf("%d\n", ans);

}


// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
#define int long long
using namespace std;

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

const int _=1e18;

signed main() {
    int a=read(),q=(_%a*9%a*9%a+1)%a;
    printf("%lld %lld\n",a-q+1,a-q+_);
    return 0;
}
    		      	 			 	 			 	 	  		//hellobig_tq


#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#define LL __int64

using namespace std;

int dp[108][100008], l[108], r[108], n, k;
const int maxn = 300;
int Qv[100008], Qid[100008], pos1, pos2;

void solve(int nown)
{
    //+0
    for(int i = 0; i <= n; i++)
        dp[nown][i] = dp[nown - 1][i];
    //+2
    pos1 = pos2 = 0;
    for(int i = 1; i <= r[nown]; i++) {
        if(i - 1 <= n) {
            while(pos2 > pos1 && dp[nown - 1][i - 1] <= Qv[pos2 - 1])
            --pos2;
            Qv[pos2] = dp[nown - 1][i - 1];
            Qid[pos2++] = i - 1;
        }
        while(pos2 > pos1 && Qid[pos1] < i - (r[nown] - l[nown]))
            ++pos1;
        if(pos2 > pos1)
            dp[nown][i] = min(dp[nown][i], Qv[pos1] + 2);
    }
    //+1
    pos1 = pos2 = 0;
    for(int i = r[nown]; i >= 0; i--) {
        if(r[nown] - i <= n) {
            while(pos2 > pos1 && dp[nown - 1][r[nown] - i] <= Qv[pos2 - 1])
                --pos2;
            Qv[pos2] = dp[nown - 1][r[nown] - i];
            Qid[pos2++] = r[nown] - i;
        }
        while(pos2 > pos1 && Qid[pos1] < l[nown] - i)
            ++pos1;
        if(pos2 > pos1)
            dp[nown][i] = min(dp[nown][i], Qv[pos1] + 1);
    }
}

int main()
{
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= k; i++)
        scanf("%d%d", &l[i], &r[i]);

    dp[0][0] = 0;
    for(int i = 1; i <= n; i++)
        dp[0][i] = maxn;
    for(int i = 1; i <= k; i++)
        solve(i);

    if(dp[k][n] ==  maxn)
        printf("Hungry");
    else
        printf("Full\n%d", dp[k][n]);
    return 0;
}


#include <iostream>
#include <math.h>

#define eps 0.0000001
#define ll long long

using namespace std;

struct vec
{
    double x;
    double y;
    vec operator*(double d)
    {
        vec v;
        v.x=x*d;
        v.y=y*d;
        return v;
    }
    void norm()
    {
        double d;
        d=sqrt(x*x+y*y);
        x=x/d;
        y=y/d;
    }

};

struct pt
{
    double x;
    double y;
    vec operator-(pt p)
    {
        vec v;
        v.x=x-p.x;
        v.y=y-p.y;
        return v;
    }
    pt operator+(vec v)
    {
        pt p;
        p.x=x+v.x;
        p.y=y+v.y;
        return p;
    }
};
int cross(pt p1, pt p2,pt p3)
{
    double x1=p2.x-p1.x;
    double y1=p2.y-p1.y;
    double x2=p3.x-p1.x;
    double y2=p3.y-p1.y;
    double t=x1*y2-y1*x2;
    if (fabs(t)<eps)
    return 0;
    if(t>0) return 1;
    else return -1;
}
bool intersect(pt p1,pt p2, pt p3, pt p4, int tp) // tp=1 when wall
{
    ll a,b,c,d;
    a=cross(p1,p2,p3);
    b=cross(p1,p2,p4);
    c=cross(p3,p4,p1);
    d=cross(p3,p4,p2);
    if(fabs(a)<eps&&fabs(b)<eps)
    {
        if(tp==1)
        {
            if(max(p1.x,p2.x)<min(p3.x,p4.x)||min(p1.x,p2.x)>max(p3.x,p4.x))
            return 0;
            else
            return 1;
        }
        else return 0;
    }
    if(fabs(a)<eps)
    {
            if(p3.x>min(p1.x,p2.x)&&p3.x<max(p1.x,p2.x))
            return 1;
            else return 0;
    }
    if(fabs(b)<eps)
    {
            if(p4.x>min(p1.x,p2.x)&&p4.x<max(p1.x,p2.x))
            return 1;
            else return 0;
    }
    if(a*b<0&&c*d<0) return 1;
    else return 0;
}
double dis_from_line(pt p1, pt p2, pt p3)
{
    double x1=p3.x-p2.x;
    double y1=p3.y-p2.y;
    double x2=p1.x-p2.x;
    double y2=p1.y-p2.y;
    double d=sqrt(x2*x2+y2*y2);
    return (x1*y2-y1*x2)/d;
}
double dis(pt p1, pt p2)
{
    double x=p1.x-p2.x;
    double y=p1.y-p2.y;
    return sqrt(x*x+y*y);
}
int gcd(int a,int b)
{
    if (b == 0)
      return a;
   return gcd(b, a % b);
}
struct line
{
    ll a,b,c;
  /*  void simply()
    {
        int g;
        if((g=gcd(a,b))==gcd(b,c))
        {
            a=a/g;
            b=b/g;
            c=c/g;
        }
    }*/
};

int main()
{
    pt pv,pp,pw1,pw2,pm1,pm2;
    cin>>pv.x>>pv.y>>pp.x>>pp.y>>pw1.x>>pw1.y>>pw2.x>>pw2.y>>pm1.x>>pm1.y>>pm2.x>>pm2.y;
    if(!intersect(pv,pp,pw1,pw2,1))
    {
        if(!intersect(pv,pp,pm1,pm2,0))
        {
            cout<<"YES";
            return 0;
        }
    }

    int a1,a2;
    a1=cross(pm1,pm2,pv);
    a2=cross(pm1,pm2,pp);
    if(fabs(a1)<eps||fabs(a2)<eps)
    {

        cout<<"NO";
        return 0;

    }
    if((a1>0&&a2>0)||(a1<0&&a2<0))
    {
        double dv=dis_from_line(pm1,pm2,pv);
        double dp=dis_from_line(pm1,pm2,pp);
        double d=dis(pp,pv);
        double av=(d*dv)/(dp+dv);
        vec nn;
        nn=pp-pv;
        nn.norm();
        nn=nn*av;
        pt temp;
        temp=pv+nn;
     //   cout<<temp.x<<"<"<<temp.y<<endl;
        line m;
        m.a=-(pm2.y-pm1.y);
        m.b=pm2.x-pm1.x;
        m.c=-m.b*pm1.y-m.a*pm1.x;

     //   m.simply();
        line m_n;
        m_n.a=m.b;
        m_n.b=-m.a;
        m_n.c=-m_n.a*temp.x-m_n.b*temp.y;
        // cout<<m_n.a<<"<"<<m_n.b<<"<"<<m_n.c<<endl;
      //  m_n.simply();
        pt sec;
        if(m.a==0)
        {
            sec.y=-m.c/m.b;
            sec.x=temp.x;
        }
        else
        {
            sec.y=double(m.a*m_n.c-m_n.a*m.c)/double(m_n.a*m.b-m.a*m_n.b);
            sec.x=double (-m.b*sec.y-m.c)/double(m.a);
        }

        int minx=min(pm1.x,pm2.x);
        int miny=min(pm1.y,pm2.y);
        int maxx=max(pm1.x,pm2.x);
        int maxy=max(pm1.y,pm2.y);
        if(sec.x<minx||sec.x>maxx||sec.y<miny||sec.y>maxy)
        {
            cout<<"NO";
            return 0;
        }
        if(intersect(sec,pv,pw1,pw2,1))
        {

            cout<<"NO";
            return 0;
        }
        if(intersect(sec,pp,pw1,pw2,1))
        {
         //   cout<<"PP"<<endl;
             cout<<"NO";
            return 0;
        }
        else
        {
            cout<<"YES";
            return 0;
        }
    }
    else
    {

        cout<<"NO";
        return 0;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(x) ((int)(x).size())
#define rep(i,l,r) for(int i=(l);i<(r);++i)
//------
const int N = 3e2 + 7;
int n, V, m, a[N], b[N], c[N][N], vis[N], deg[N];
vector<int> e[N];
void addEdge(int u, int v) {
	++deg[u], ++deg[v];
	e[u].push_back(v), e[v].push_back(u);
}
void dfs(int u) {
	vis[u] = true;
	rep(i, 1, n + 1)
		if (c[u][i] && !vis[i]) {
			addEdge(u, i);
			dfs(i);
		}
}
vector<pair<pair<int, int>, int> > ans;
void trans(int u, int v, int w) {
	if (w <= 0)
		return ;
	ans.push_back(make_pair(make_pair(u, v), w));
	a[u] -= w, a[v] += w;
}
void DFS(int u, int p, int vol) {
	rep(i, 0, sz(e[u])) {
		int v = e[u][i];
		if (v == p || !vis[v])
			continue;
		if (a[v] < vol)
			DFS(v, u, vol - a[v]);
		int flow = min(vol, a[v]);
		trans(v, u, flow), vol -= flow;
	}
}
void DFS2(int u, int p, int vol) {
	rep(i, 0, sz(e[u])) {
		int v = e[u][i];
		if (v == p || !vis[v])
			continue;
		if (V - a[v] < vol)
			DFS2(v, u, vol - V + a[v]);
		int flow = min(vol, V - a[v]);
		trans(u, v, flow), vol -= flow;
	}
}
void solve() {
	queue<int> que;
	rep(i, 1, n + 1)
		if (deg[i] == 1)
			que.push(i);
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		vis[u] = false;
		if (a[u] < b[u])
			DFS(u, -1, b[u] - a[u]);
		else if (a[u] > b[u])
			DFS2(u, -1, a[u] - b[u]);
		rep(i, 0, sz(e[u])) {
			int v = e[u][i];
			--deg[v];
			if (deg[v] == 1)
				que.push(v);
		}
	}
	rep(i, 1, n + 1)
		if (a[i] != b[i]) {
			puts("NO");
			return ;
		}
	printf("%d\n", sz(ans));
	rep(i, 0, sz(ans))
		printf("%d %d %d\n", ans[i].first.first, ans[i].first.second, ans[i].second);
}
int main() {
	scanf("%d%d%d", &n, &V, &m);
	rep(i, 1, n + 1)
		scanf("%d", &a[i]);
	rep(i, 1, n + 1)
		scanf("%d", &b[i]);
	rep(e, 0, m) {
		int x, y;
		scanf("%d%d", &x, &y);
		c[x][y] = c[y][x] = 1;
	}
	rep(i, 1, n + 1)
		if (!vis[i])
			dfs(i);
	solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
const int maxd=11;
long long n,d,mod,dp[maxn][maxd],pd[maxn][maxd],f,t[maxd];
inline long long ksm(long long x,long long n) {
    long long ret=1;
    while (n) {
        if (n&1)
            ret=ret*x%mod;
        n>>=1;
        x=x*x%mod;
    }
    return ret;
}
inline long long solve() {
    dp[1][0]=1;
    for (int i=1;i<=n/2;++i) {//if (i%(d-1)==0) {
        f=(i==1)?1:dp[i][d-1];
        for (int j=1;j<=n;++j)
            for (int k=0;k<=d;++k)
                pd[j][k]=0;
        t[0]=1;
        for (int j=1;j<=d;++j)
            t[j]=t[j-1]*(f+j-1)%mod*ksm(j,mod-2)%mod;
        for (int j=0;j<=d;++j)
            for (int k=0;k<=d;++k)  if (j+k<=d)
                for (int l=1;l<=n;++l) if (l+i*k<=n)
                    pd[l+i*k][j+k]=(pd[l+i*k][j+k]+dp[l][j]*t[k])%mod;
        for (int j=0;j<=n;++j)
            for (int k=0;k<=d;++k)
                //if (pd[j][k])
                    dp[j][k]=pd[j][k];
    }
    long long ret=dp[n][d];
    if (n%2==0)
        ret=((ret-dp[n/2][d-1]*(dp[n/2][d-1]-1)/2)%mod+mod)%mod;
    return ret;
}
int main()
{
    scanf("%I64d%I64d%I64d",&n,&d,&mod);
    if (n<=2)
        puts("1");
    else
        printf("%I64d\n",solve());
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <set>
#include <map>
#include <string>
using namespace std;
int a[25];
int tu[8];

int b[6][8] = { { 22, 21, 18, 17, 5, 6, 14, 13 },
{ 3, 4, 17, 19, 10, 9, 16, 14 },
{ 7, 8, 19, 20, 23, 24, 15, 16 },
{ 1, 3, 5, 7, 9, 11, 24, 22 },
{ 4, 2, 21, 23, 12,10, 8, 6 },
{ 2, 1, 13, 15, 11, 12, 20, 18 } };

bool res (int tmp[]){

	for (int i = 2; i <=24; ++i){
		if ((i - 1) % 4 == 0)
			continue;
		if (tmp[i] != tmp[i - 1])
			return false;
	}

	return true;
}

void rot(int tmp[],int f ,bool d){

	if (d){
		int j = 0;
		for (int i = 2; i < 8; ++i){
			tu[j] = tmp[i];
			++j;
		}
		tu[6] = tmp[0];
		tu[7] = tmp[1];
	}
	else{
		int j = 0;
		for (int i = 2; i < 8; ++i){
			tu[i] = tmp[j];
			++j;
		}
		tu[0] = tmp[7];
		tu[1] = tmp[6];

	}

}

bool can(int f){

	int tmp[8];
	for (int i= 0; i < 8; ++i){
		tmp[i] = b[f][i];
	}

	int tmp_2[25];

	for (int i = 1; i <= 24; ++i){
		tmp_2[i] = a[i];
	}

	rot(tmp, f, 0);

	for (int i = 0; i < 8; ++i){
		tmp_2[b[f][i]] = a[tu[i]];
	}

	if (res(tmp_2))
		return true;

	rot(tmp,f,1);

	for (int i = 0; i < 8; ++i){
		tmp_2[b[f][i]] = a[tu[i]];
	}

	if (res(tmp_2))
		return true;

	return 0;

}

int main() {

//	freopen("Text.txt", "r", stdin);

	for (int i = 1; i <= 24; ++i){
		cin >> a[i];
	}

	for (int i = 0; i < 6; ++i){
		if (can(i)){
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define PI (acos(-1))
#define ct cout<<
#define cn cin>>
#define spc <<" "<<
#define nl puts("")
#define _fl(x) puts("FLAG " #x);
#define  _(x)  cout<< #x <<" is "<< x <<endl;
#define fs first
#define sc second
#define pb push_back
#define all(v)  (v).begin(), (v).end()
#define _109 (1000000000)
#define _0(arr) memset(arr,0,sizeof ( arr ) )
#define _1(arr) memset(arr,-1,sizeof ( arr ) )
#define _ninp(n,arr) for(int i=0; i<n;i++)cin>>arr[i];
#define _nout(n,arr) for(int i=0; i<n;i++)cout<<arr[i]<<" \n"[i==n-1];
double _eps = 1e-6;
long long gcd(long long a, long long b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

bool arr[601][601];

int main()
{
  int n,b,g;
  cn b>>g>>n;
  int c=0;
  for (int i = 0; i <=n ; ++i)
  {
    if(i<=b && n-i<=g)c++;
  }
  ct c;nl;

    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 200005
#define ls o<<1
#define rs o<<1|1
struct sgt
{
	int l,r,mn,mm,tag;
	long long sum;
}c[maxn<<2];
int n;
long long ans;
int a[maxn];
long long h[maxn];
vector<int>x[maxn];
void pd(int o)
{
	if(c[o].tag)
	{
		c[ls].mm=c[ls].mn=c[o].tag;
		c[rs].mm=c[rs].mn=c[o].tag;
		c[ls].tag=c[rs].tag=c[o].tag;
		c[ls].sum=((long long)c[ls].r-c[ls].l+1)*c[o].tag;
		c[rs].sum=((long long)c[rs].r-c[rs].l+1)*c[o].tag;
		c[o].tag=0;
	}
}
void build(int l,int r,int o)
{
	c[o].l=l,c[o].r=r;
	if(l==r)
	{
		c[o].mn=c[o].sum=c[o].mm=l;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls);
	build(mid+1,r,rs);
	c[o].mm=max(c[ls].mm,c[rs].mm);
	c[o].mn=min(c[ls].mn,c[rs].mn);
	c[o].sum=c[ls].sum+c[rs].sum;
}
void update(int l,int r,int k,int o)
{
	if(l>r) return ;
	if(l<=c[o].l&&r>=c[o].r&&k<=c[o].mn) return ;
 	if(l<=c[o].l&&r>=c[o].r&&k>=c[o].mm)
	{
		c[o].tag=k;
		c[o].mm=c[o].mn=c[o].tag;
		c[o].sum=((long long)c[o].r-c[o].l+1)*k;
		return ;
	}
	pd(o);
	int mid=(c[o].l+c[o].r)>>1;
	if(l>mid) update(l,r,k,rs);
	else if(r<=mid) update(l,r,k,ls);
	else
	{
		update(l,mid,k,ls);
		update(mid+1,r,k,rs);
	}
	c[o].mm=max(c[ls].mm,c[rs].mm);
	c[o].mn=min(c[ls].mn,c[rs].mn);
	c[o].sum=c[ls].sum+c[rs].sum;
}
int main()
{
	scanf("%d",&n);
	h[0]=3;
	for(int i=1;i<=n;++i) scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)
	{
		int t=sqrt(a[i]);
		for(int j=1;j<=t;++j)
		{
			if(a[i]%j==0)
			{
				x[j].push_back(i);
				if(j*j!=a[i]) x[a[i]/j].push_back(i);
			}
		}
	}
	/*for(int i=1;i<=5;++i)
	{
		for(int j=0;j<x[i].size();++j) cout<<x[i][j]<<" ";
		cout<<endl;
	}*/
	build(1,n,1);
	for(int i=200000;i>=1;--i)
	{
		h[i]=(long long)n*n-c[1].sum+n;
		if(x[i].size()<=1) continue;
		else
		{
			update(1,x[i][0],x[i][x[i].size()-2],1);
			update(x[i][0]+1,x[i][1],x[i][x[i].size()-1],1);
			update(x[i][1]+1,n,n+1,1);
		}
	}
	//for(int i=0;i<=5;++i) cout<<i<<"    "<<h[i]<<endl;
	for(int i=200000;i>=1;--i)
		ans=ans+(long long)i*(h[i]-h[i-1]);
	printf("%lld",ans);
	return 0;
}
//hellobig_tq//hello


#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define mp(x, y) make_pair(x, y)
#define forn(i, a, b) for(size_t i=a; i<b; ++i)
#define ford(i, a, b) for(size_t i=b-1; i>=a; --i)
#define all(a) a.begin(), a.end()
#define sz(a) (size_t)(a).size()
#define X first
#define Y second
#define sqr(x) 1ll*(x)*(x)
#define pb push_back
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
using vint = vector<int>;
using vll = vector<ll>;
using v2int = vector<vint>;
using v2ll = vector<vll>;

int main()
{
    fio;
    int a1,a2,k1,k2,n;
    int MAX=0;
    cin >>a1>>a2>>k1>>k2>>n;

    if (k2<k1)
    {
        swap(k1,k2);
        swap(a1,a2);
    }

    int t1=a1,t2=a2,nn=n;

    while (a1>0 && n>=k1)
    {
        n-=k1;
        a1--;
    }

    while (a2>0 && n>=k2)
    {
        n-=k2;
        a2--;
    }

    MAX=(t1-a1)+(t2-a2);
    ///////////////////

    a1=t1;
    a2=t2;
    n=nn;

    int tmp=a1*(k1-1)+a2*(k2-1);

    cout<<(tmp>=n?0:n-tmp)<<" "<<MAX<<endl;

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int P = 1000000007;
int n, m;

int get(int n, int mode) {
	if (mode == 0) {
		return 1 + (n / 2);
	} else {
		return (n + 1) / 2;
	}
}

int gcd(int x, int y) {
	if (x == 0 || y == 0) {
		return x + y;
	}
	if (x > y) {
		return gcd(x % y, y);
	} else {
		return gcd(y % x, x);
	}
}

long long cnk2(long long n) {
	return ((n * (n - 1)) / 2LL) % P;
}

const int maxN = 4003;
int ways[maxN];
int p[maxN][maxN];

void add(int dx, int dy) {

}

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

	for (int i = 0; i < maxN; ++i) {
		for (int j = 0; j < maxN; ++j) {
			if (i == 0 || j == 0) {
				p[i][j] = i + j;
			} else {
				if (i > j) {
					p[i][j] = p[i - j][j];
				} else {
					p[i][j] = p[i][j - i];
				}
			}
		}
	}
	cin >> n >> m;
	long long res = 0;
	for (int x1 = 0; x1 < 2; ++x1) {
		for (int y1 = 0; y1 < 2; ++y1) {
			for (int x2 = 0; x2 < 2; ++x2) {
				for (int y2 = 0; y2 < 2; ++y2) {
					for (int x3 = 0; x3 < 2; ++x3) {
						for (int y3 = 0; y3 < 2; ++y3) {
							int s = gcd(x1 + x2, y1 + y2) + gcd(x2 + x3, y2 + y3) + gcd(x1 + x3, y1 + y3);
							if (s % 2 == 0) {
								long long A = get(n, x1) * get(m, y1);
								long long B = get(n, x2) * get(m, y2);
								long long C = get(n, x3) * get(m, y3);
								res += ((A * B) % P) * C;
								res %= P;
							}
						}
					}
				}
			}
		}
	}
	res -= (long long)(n + 1) * (long long)(m + 1);
	res = ((res % P) + P) % P;
	res -= 6LL * cnk2((n + 1) * (m + 1));
	res = ((res % P) + P) % P;
	for (int dx = 1; dx <= n; ++dx) {
		for (int dy = 1; dy <= m; ++dy) {
			if (dx == 0 && dy <= 0) {
				continue;
			}
			int k = p[dx][dy] - 1;
			ways[k] += (n - dx + 1) * (m - dy + 1);
			if (ways[k] >= P) {
				ways[k] -= P;
			}
		}
	}
	for (int i = 1; i < maxN; ++i) {
		res -= 12LL * (long long)(i) * (long long)(ways[i]);
		res %= P;
	}

	memset(ways, 0, sizeof(ways));
	for (int dx = 0; dx <= n; ++dx) {
		int l = 1, r = m;
		if (dx > 0) {
			l = 0;
			r = 0;
		}
		for (int dy = l; dy <= r; ++dy) {
			if (dx == 0 && dy <= 0) {
				continue;
			}
			int k = p[dx][dy] - 1;
			ways[k] += (n - dx + 1) * (m - dy + 1);
			if (ways[k] >= P) {
				ways[k] -= P;
			}
		}
	}

	for (int i = 1; i < maxN; ++i) {
		res -= 6LL * (long long)(i) * (long long)(ways[i]);
		res %= P;
	}

	res = ((res % P) + P) % P;
	cout << res << endl;

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

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

    long long answer = n - 2;
    for (int i = 1; i < n; i++)
        answer += max(n - i - 2, 0) + max(0, i - 2);

    cout << answer;
    return 0;
}


#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const int MAXN = 3005;
const ll INF = 1e16;
const int MOD = 998244353;
ll b[MAXN], w[MAXN], size[MAXN];
pll dp[MAXN][MAXN], tmp[MAXN];
int n, m, T;

vector<int> e[MAXN];

void dfs (int u, int fa)
{
	dp[u][0] = mp(0, b[u]);
	size[u] = 1;
	for (int i = 0; i < e[u].size(); ++ i)
	{
		int v = e[u][i];
		if (v == fa)
			continue;
		dfs (v, u);
		for (int j = 0; j <= min ((ll)m, size[u] + size[v]); ++ j)
			tmp[j] = mp(0, -INF);
		for (int j = 0; j <= min ((ll)m, size[u]); ++ j)
			for (int k = 0; k <= min ((ll)(m - j), size[v]); ++ k)
			{
				tmp[k + j] = max (tmp[k + j], mp(dp[v][k].first + dp[u][j].first, dp[v][k].second + dp[u][j].second));
				tmp[k + j + 1] = max (tmp[k + j + 1], mp(dp[v][k].first + dp[u][j].first + (dp[v][k].second > 0), dp[u][j].second));
			}
		size[u] += size[v];
		for (int j = 0; j <= min ((ll)m, size[u]); ++ j)
			dp[u][j] = tmp[j];
	}
}

int main()
{
	scanf ("%d", &T);
	while (T)
	{
		-- T;
		scanf ("%d%d", &n, &m);
		for (int i = 1; i <= n; ++ i)
			size[i] = 0, tmp[i] = mp(0, 0), e[i].clear();
		for (int i = 1; i <= n; ++ i)
			for (int j = 1; j <= m; ++ j)
				dp[i][j] = mp(0, 0);
		for (int i = 1; i <= n; ++ i)
			cin >> w[i];
		for (int i = 1; i <= n; ++ i)
		{
			cin >> b[i];
			b[i] -= w[i];
		}
		for (int i = 1; i < n; ++ i)
		{
			int u, v;
			scanf ("%d%d", &u, &v);
			e[u].pb(v), e[v].pb(u);
		}

		dfs (1, 0);
		cout << dp[1][m - 1].first + (dp[1][m - 1].second > 0) << endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <sstream>

using namespace std;

inline void file()
{
    freopen("D:\\go.txt", "r", stdin);
    freopen("D:\\leave.txt", "w", stdout);
}

int main()
{
    //file();
    int cas;
    scanf("%d", &cas);
    while (cas--)
    {
    string str;
    cin >> str;
    str[3] = ' ';
    stringstream tmp(str.substr(4));
    int n;
    tmp >> n;
    int lenth = str.size() - 4;
    int time_to = 0, t = 10;
    //cout << lenth << endl;
    for (int i = 1; i < lenth; ++i)
    {
        time_to += t;
        t *= 10;
    }
    while (n < 1989 + time_to)
    {
        n += t;
        //cout << n << endl;
    }
    printf("%d\n", n);
    }
    return 0;
}


/**
 *    author:  tourist
 *    created: 31.07.2022 18:10:18
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
    value = normalize(x);
  }

  template <typename U>
  static Type normalize(const U& x) {
    Type v;
    if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    else v = static_cast<Type>(x % mod());
    if (v < 0) v += mod();
    return v;
  }

  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }

  Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
  Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
    uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
    uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (mod())
    );
    value = m;
#else
    value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
    long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }

  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }

  friend const Type& abs(const Modular& x) { return x.value; }

  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename V, typename U>
  friend V& operator>>(V& stream, Modular<U>& number);

 private:
  Type value;
};

template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }

template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }

template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }

template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }

template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }

template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1) res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}

template <typename T>
bool IsZero(const Modular<T>& number) {
  return number() == 0;
}

template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}

// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
  return stream << number();
}

// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, long long>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

using ModType = int;

struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;

vector<Mint> fact;
vector<Mint> inv_fact;

Mint C(int n, int k) {
  if (k < 0 || k > n) {
    return 0;
  }
  while ((int) fact.size() < n + 1) {
    fact.push_back(fact.back() * (int) fact.size());
    inv_fact.push_back(1 / fact.back());
  }
  return fact[n] * inv_fact[k] * inv_fact[n - k];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n >> md;
  fact = inv_fact = {1};
  C(2 * n + 1, 0);
  Mint ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; 2 * j <= i; j++) {
      Mint dp = fact[i] * C(i - j - 1, j - 1) * inv_fact[j];
      int ones = n - i;
      Mint add = dp * C(n, i) * power(Mint(n - 1), j) * power(Mint(n - j - ones), ones);
      ans += add;
    }
  }
  cout << ans << '\n';
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
// #define int long long
typedef long long ll;
#define fi first
#define se second
#define pii pair<int,int>
#define endl '\n'
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define sz(s) (int)s.size()
#define y1 fdfbvfds
typedef long double f80;
#define all(x) x.begin(), x.end()
typedef long double f80;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l,ll r){
    uniform_int_distribution<ll> uid(l, r);
    return uid(rng);
}

const int H = 60;
const int N = 5e5 + 5;

f80 dp[N][H];
int par[N];
int n = 0;

void add(int v) {
	n++;
	par[n] = v;
	fr(i, 0, H - 1) {
		dp[n][i] = 1;
	}
	vector<int> anc;
	v = n;
	for(int i = 0; i < H; i++) {
		if(!v) break;
		anc.pb(v);
		v = par[v];
	}
	for(int i = sz(anc) - 2; i > 0; i--) {
		int u = anc[i], p = anc[i + 1];
		dp[p][i] /= ((1 + dp[u][i - 1]) / 2.0);
	}
	if(sz(anc) > 1)
		dp[anc[1]][0] /= 2;
	for(int i = 1; i < sz(anc) - 1; i++) {
		int u = anc[i], p = anc[i + 1];
		dp[p][i] *= ((1 + dp[u][i - 1]) / 2.0);
	}
}

f80 query(int u) {
	f80 ans = 0;
	fr(i, 1, H - 1) {
		ans += i * (dp[u][i] - dp[u][i - 1]);
	}
	return ans;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int q;
    cin >> q;
    add(0);
    while(q--) {
    	int t, v;
    	cin >> t >> v;
    	if(t == 1) {
    		add(v);
    	}
    	else {
    		cout << setprecision(10) << fixed << query(v) << endl;
    	}
    }
	return 0;
}


#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
    #define lld "%I64d"
#else
    #define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1e9+7;
int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

ll n;
int k;

// mo < 2^30 = 1073741824
// first call mm_init(sz);
// indices from 0 to mm_n - 1
// call a.plus(),b.plus() before a*b

const int MM_N =33;

int mm_n;
struct mat{
	int a[MM_N][MM_N];
	void plus(){
		rep(i,0,mm_n)rep(j,0,mm_n)a[i][j]=(a[i][j]%mo+mo)%mo;
	}
}one;
mat operator*(const mat&a,const mat&b){
	static ll ans[MM_N][MM_N];
	cl(ans);
	ll infa=8e18;
	rep(k,0,mm_n)rep(i,0,mm_n)rep(j,0,mm_n){
		if((ans[i][j]+=1ll*a.a[i][k]*b.a[k][j])>=infa)ans[i][j]%=mo;
	}
	mat c;
	rep(i,0,mm_n)rep(j,0,mm_n)c.a[i][j]=ans[i][j]%mo;
	return c;
}
mat qp(mat a,ll b){a.plus();mat n=one;do{if(b&1)n=n*a;a=a*a;}while(b>>=1);return n;}
void mm_init(int sz){
	mm_n=sz;
	rep(i,0,mm_n)one.a[i][i]=1;
}

int nu[111];int tot=0;

mat a[100];

mat jia(const mat&x,int d){
	mat ret;
	rep(i,0,k+1)rep(j,0,k+1)ret.a[i!=k?(i+d)%k:k][j!=k?(j+d)%k:k]=x.a[i][j];
	return ret;
}
int main()
{
#ifdef JCVB
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	gn(n);gn(k);
	mm_init(k+1);

	while(n){
		nu[tot++]=n%k;
		n/=k;
	}
	for (int j=0;j<=k;j++)a[0].a[0][j]=1;
	for (int j=0;j<=k;j++)a[0].a[j][j]=1;
	for (int i=1;i<tot;i++){
		mat now=one;
		rep(j,0,k)now=now*jia(a[i-1],j);
		a[i]=now;
	}
	int ad=0;
	mat ans=one;
	for (int i=tot-1;i>=0;i--){
		for (int j=0;j<nu[i];j++)
			ans=ans*jia(a[i],(j+ad)%k);
		ad=(ad+nu[i])%k;
	}

	int ret=0;
	rep(i,0,k+1)upmo(ret,ans.a[i][k]);
	printf("%d\n",ret);
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN (int)(1e5 + 5)
#define MAXM (int)(1e2+5)
#define fr first
#define sd second
#define MP make_pair
#define pb push_back
#define SZ(A) (int)A.size()
#define ALL(v) v.begin(), v.end()
#define M0(a) memset(a, 0, sizeof(a))
#define M1(a) memset(a, -1, sizeof(a))
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REPN(i,n,m) for(int i = m; i <= int(n); i++)
#define RREP(i,n) for(int i = int(n-1); i >= 0; i--)
#define RREPN(i, n, m) for(int i = n; i >= (int)m; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const ll INF= ll(1e15);
const ll MOD = ll(1e9 + 7);
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, pii> ppii;
template<typename T, typename U> inline void AMIN(T &x, U y){ if(x > y) x = y;}
template<typename T, typename U> inline void AMAX(T &x, U y){ if(x < y) x = y;}

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

        int ans = INT_MAX;
        vector<int> V = {a, a, a, a, b, b};
        vector<int> IDX = {0, 1, 2, 3, 4, 5};

        do {

                int rem = 0, X = 0;
                REP(i, 6) {

                        if(rem < V[IDX[i]])
                                rem = n - V[IDX[i]], X++;
                        else
                                rem -= V[IDX[i]];
                }

                ans = min(ans, X);

        } while(next_permutation(ALL(IDX)));

        cout << ans << endl;

        return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <stdio.h>

using namespace std;

void init()
{
	freopen("input.txt", "r", stdin);
}

int main()
{
	string input, output;
	cin >> input >> output;
	int k;
	cin >> k;
	long long int mod = 1000 * 1000 * 1000 + 7;
	int length = input.length();
	vector <long long int> previous(length), present(length);
	present[0] = 1;
	for (int i = 1; i < length; ++i)
	{
		present[i] = 0;
	}
	for (int i = 0; i < k; ++i)
	{
		previous = present;
		long long int sum = 0;
		for (int j = 0; j < length; ++j)
		{
			sum += previous[j];
		}
		for (int j = 0; j < length; ++j)
		{
			present[j] = (sum - previous[j]) % mod;
		}
	}
	long long int ans = 0;
	for (int i = 0; i < length; ++i)
	{
		bool equals = true;
		for (int j = 0; j < length - i; ++j)
		{
			if (input[i + j] != output[j])
			{
				equals = false;
			}
		}
		for (int j = length - i; j < length; ++j)
		{
			if (input[j - length + i]!= output[j])
			{
				equals = false;
			}
		}
		if (equals)
		{
			ans += present[i];
		}
	}
	ans = ans % mod;
	cout << ans;
	return 0;
}


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

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

#define unordered_map __fast_unordered_map
template<class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const vector<VI> kShifts = {
  {0, 1, 4, 3}, {0, 3, 4, 1},
  {1, 2, 5, 4}, {1, 4, 5, 2},
  {3, 4, 7, 6}, {3, 6, 7, 4},
  {4, 5, 8, 7}, {4, 7, 8, 5},
};

VI Apply(VI perm, const VI &shift) {
  vector<int> elems(SZ(shift));
  for (int i = 0; i < SZ(shift); ++i)
    elems[i] = perm[shift[i]];

  for (int i = 0; i < SZ(shift); ++i)
    perm[shift[(i + 1) % SZ(shift)]] = elems[i];

  return perm;
}

int GetIndex(const vector<int> &perm) {
  vector<bool> has_it(9);
  int coef = 362880;
  int idx = 0;

  for (int i = 0; i < SZ(perm); ++i) {
    coef /= SZ(perm) - i;
    const int v = perm[i];
    int num_lower = 0;
    for (int j = 0; j < v; ++j)
      num_lower += !has_it[j];
    idx += coef * num_lower;
    has_it[v] = true;
  }

  // debug(perm, has_it, idx, coef);

  return idx;
}

const int kNumPerms = 5e5;

bool visited[kNumPerms];
VI op_done[kNumPerms];
int cur_dist[kNumPerms];
int parent[kNumPerms];

void Preproc() {
  queue<VI> Q;

  VI start(9);
  iota(ALL(start), 0);
  visited[GetIndex(start)] = true;
  Q.push(start);

  int num_visited = 0;
  int max_dist = 0;

  while (!Q.empty()) {
    auto V = Q.front(); Q.pop();
    ++num_visited;
    const int idx = GetIndex(V);
    const int dist = cur_dist[idx];
    maxi(max_dist, dist);
    // debug(V);

    for (auto &shift : kShifts) {
      const VI next_v = Apply(V, shift);
      const int next_idx = GetIndex(next_v);
      if (!visited[next_idx]) {
        visited[next_idx] = true;
        op_done[next_idx] = shift;
        cur_dist[next_idx] = dist + 1;
        parent[next_idx] = idx;
        Q.push(next_v);
      }
    }
  }
  debug(num_visited, max_dist);

}

const int MaxDim = 24;
int datax[MaxDim][MaxDim];
int data_copy[MaxDim][MaxDim];
int N, M;

vector<VI> cycles;

void SwapTwo(int r1, int c1, int r2, int c2) {
  debug("SwapTwo", r1, c1, r2, c2);
  assert(abs(r1 - r2) <= 2 && abs(c1 - c2) <= 2);

  const int r0 = min(N - 3, min(r1, r2));
  const int c0 = min(M - 3, min(c1, c2));

  /* vector<int> order(9);
  iota(ALL(order), 0);
  sort(ALL(order), [&](int lhs, int rhs) {
        int rl = lhs / 3, cr = lhs % 3;
        int rr = rhs / 3, cr = rhs % 3;
        return datax[r0+rl][c0+cr] < data[r0+rr][c0+cr];
      }); */

  const int swap1 = (r1-r0)*3+(c1-c0);
  const int swap2 = (r2-r0)*3+(c2-c0);

  vector<int> order(9);
  iota(ALL(order), 0);
  swap(order[swap1], order[swap2]);
  int cur_idx = GetIndex(order);

  vector<VI> ops;

  while (cur_idx != 0) {
    const VI cycle = op_done[cur_idx];
    ops.PB(cycle);
    cur_idx = parent[cur_idx];
  }

  reverse(ALL(ops));
  for (auto &cycle : ops) {
    VI description;
    for (int v : cycle)
      description.push_back(datax[r0 + v / 3][c0 + v % 3]);

    const int S = SZ(cycle);
    for (int i = 0; i < S; ++i) {
      const int j = (i + S - 1) % S;
      const int v = cycle[i];
      datax[r0+v/3][c0+v%3] = description[j];
    }

#ifdef LOCAL
    debug(description);
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j)
        cerr << datax[i][j] << "\t";
      cerr << "\n";
    }
#endif

    cycles.push_back(description);
  }

  // swap(datax[r1][c1], datax[r2][c2]);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  Preproc();

  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j) {
      cin >> datax[i][j];
      data_copy[i][j] = datax[i][j];
    }

  for (int idx = 1; idx <= N * M; ++idx) {
    int r = -1, c = -1;
    const int needed_r = (idx - 1) / M;
    const int needed_c = (idx - 1) % M;

    debug(idx, r, c, needed_r, needed_c);

    for (int i = 0; i < N; ++i)
      for (int j = 0; j < M; ++j)
        if (datax[i][j] == idx) {
          r = i;
          c = j;
        }

    assert(r != -1);
    assert(c != -1);

    auto GetCloser = [&](int from, int to) {
      if (from < to)
        return min(to, from + 2);
      else
        return max(to, from - 2);
    };

    while(tie(r, c) != tie(needed_r, needed_c)) {
      int next_r = GetCloser(r, needed_r);
      int next_c = GetCloser(c, needed_c);
      while (make_pair(next_r, next_c) < make_pair(needed_r, needed_c))
        ++next_r;
      SwapTwo(r, c, next_r, next_c);
      r = next_r;
      c = next_c;
    }
#ifdef LOCAL
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++j)
        cerr << datax[i][j] << "\t";
      cerr << "\n";
    }
#endif

  }

  vector<PII> cur_loc(N * M + 1);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      cur_loc[data_copy[i][j]] = MP(i, j);

  int total_len = 0;

  cout << SZ(cycles) << "\n";
  for (auto &V : cycles) {
    cout << SZ(V);
    for (int v : V) cout << " " << v;
    cout << "\n";

    vector<PII> C;
    for (int v : V)
      C.PB(cur_loc[v]);

    vector<tuple<int, int>> new_locs;

    for (int i = 0; i < SZ(C); ++i) {
      const int j = V[(i + 1) % SZ(C)];
      data_copy[cur_loc[j].st][cur_loc[j].nd] = V[i];
      new_locs.emplace_back(cur_loc[j].st, cur_loc[j].nd);
    }

    for (auto &[r, c] : new_locs)
      cur_loc[data_copy[r][c]] = MP(r, c);

#ifdef LOCAL
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++j)
        cerr << data_copy[i][j] << "\t";
      cerr << "\n";
    }
#endif

    total_len += SZ(V);
  }

  cout << flush;

  debug(total_len);
  assert(total_len < 1e5);

#ifdef LOCALX
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++j)
        cout << data_copy[i][j] << "\t";
      cout << "\n";
    }
    cout << flush;
#endif

  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      assert(data_copy[i][j] == i * M + j + 1);
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cctype>
#include <cassert>
#include <bitset>
#include <ctime>

using namespace std;

#define pau system("pause")
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define pli pair<ll, int>
#define pil pair<int, ll>
#define clr(a, x) memset(a, x, sizeof(a))

const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define TREE tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update>
TREE T;
*/

int n, q;
ll a[200015], w[200015];
ll c1[200015], c2[200015];
inline int lb(int x) {return x & -x;}
void add1(int p, ll x) {
	for (; p <= n; p += lb(p)) c1[p] += x;
}
ll query1(int p) {
	ll res = 0;
	for (; p; p -= lb(p)) res += c1[p];
	return res;
}
void add2(int p, ll x) {
	for (; p <= n; p += lb(p)) {
		c2[p] += x;
		if (c2[p] >= MOD) c2[p] -= MOD;
	}
}
ll query2(int p) {
	ll res = 0;
	for (; p; p -= lb(p)) res += c2[p];
	return res % MOD;
}
int get_p(int l, int r) {
	ll r1 = query1(l - 1);
	ll r2 = query1(r) - r1;
	int mi, res = l;
	while (l <= r) {
		mi = l + r >> 1;
		if ((query1(mi) - r1) * 2 >= r2) {
			r = (res = mi) - 1;
		} else {
			l = mi + 1;
		}
	}
	return res;
}
ll cal(int l, int r, int p) {
	ll res1 = query2(r) - query2(p - 1) - (query1(r) - query1(p - 1)) % MOD * a[p];
	ll res2 = (query1(p) - query1(l - 1)) % MOD * a[p] - (query2(p) - query2(l - 1));
	return ((res1 + res2) % MOD + MOD) % MOD;
}
int main() {
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
		a[i] -= i;
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &w[i]);
	}
	for (int i = 1; i <= n; ++i) {
		add1(i, w[i]);
		add2(i, w[i] * a[i] % MOD);
	}
	while (q--) {
		int l, r;
		scanf("%d%d", &l, &r);
		if (l < 0) {
			add1(-l, r - w[-l]);
			add2(-l, (r - w[-l]) * a[-l] % MOD);
			w[-l] = r;
		} else {
			int p = get_p(l, r);
			printf("%lld\n", cal(l, r, p));
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long Long;
#define INF 1000000000000000000LL

Long SZ = 1000000000000000000LL;
map<Long,map<Long,Long>> mem;
map<Long,Long> go(Long i, Long j){
	map<Long,Long> D;
	if(i+1 == j)return D;
	if(mem[j-i].size() != 0)return mem[j-i];
	Long m = (i + j) / 2;
	Long d = min(j - m, m - i);
	D[d]++;
	map<Long,Long> A = go(i, m);
	map<Long,Long> B = go(m, j);
	for(const pair<Long,Long> &p : A){
		D[p.first] += p.second;
	}
	for(const pair<Long,Long> &p : B){
		D[p.first] += p.second;
	}
	return mem[j-i] = D;
}

void recurse(Long i, Long j, Long K, Long pp = 0){
	Long m = (i + j) / 2;
	Long d = min(m-i, j-m);
	map<Long,Long> D;
	map<Long,Long> A = go(i, m);
	map<Long,Long> B = go(m, j);
	for(const pair<Long,Long> &p : A){
		D[-p.first] += p.second;
	}
	for(const pair<Long,Long> &p : B){
		D[-p.first] += p.second;
	}
	D[-d]++;
	for(pair<Long,Long> p : D){
		if(-p.first != pp)continue;
//		cout << "\t" << -p.first << " " << (A.count(-p.first) ? A[-p.first] : 0) << endl;
		if(A.count(-p.first)){
			if(A[-p.first] >= K){
				recurse(i, m, K, -p.first);
				return;
			}else{
				K -= A[-p.first];
			}
		}
		if(-p.first == d){
			if(K == 1){
				cout << m+1 << endl;
				return;
			}else{
				K--;
			}
		}
//		cout << "\t" << -p.first << " " << (B.count(-p.first) ? B[-p.first] : 0) << endl;
		if(B.count(-p.first)){
			if(B[-p.first] >= K){
				recurse(m, j, K, -p.first);
				return;
			}else{
				K -= B[-p.first];
			}
		}
	}
}

int main(){
//	freopen("input.txt", "r", stdin);
	Long K;
	cin >> SZ >> K;
	if(K == 1){
		cout << 1 << endl;
		return 0;
	}
	if(K == 2){
		cout << SZ << endl;
		return 0;
	}
	K -= 2;
	map<Long,Long> D = go(0, SZ-1);
	Long pp = 0;
	for(map<Long,Long>::reverse_iterator it = D.rbegin(); it != D.rend(); ++it){
//		cout << it->first << " " << it->second << endl;
		if(it->second >= K){
			pp = it->first;
			break;
		}else{
			K -= it->second;
		}
	}
	recurse(0, SZ-1, K, pp);

}


#include<bits/stdc++.h>
using namespace std;
int p[10]={8,-1,-1,3,6,9,4,7,0,5};
string s;
int main(){
    ios::sync_with_stdio(false);
    cin>>s;
    for(int i=0;i<s.size();i++)
    	if(p[s[i]-48]!=s[s.size()-i-1]-48)
    	{
    		cout<<"No\n";
    		return 0;
		}
	cout<<"Yes\n";
	return 0;
}


// Dmitry _kun_ Sayutin (2020)

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;
using std::queue;

using std::pair;
using std::make_pair;

using std::tuple;
using std::make_tuple;
using std::get;

using std::min;
using std::abs;
using std::max;
using std::swap;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // code here
    int n, k;
    cin >> n >> k;

    vector<vector<int64_t>> arr(n);
    for (int i = 0; i < n; ++i) {
        arr[i].resize(input<int>());
        input_seq(ALL(arr[i]));

        arr[i].resize(min(SZ(arr[i]), k));
    }

    int64_t ans = 0;

    vector<pair<int64_t, int>> items(n);
    for (int i = 0; i < n; ++i) {
        items[i].second = SZ(arr[i]);
        items[i].first = std::accumulate(ALL(arr[i]), int64_t(0));
    }

    vector<int64_t> maxprofit(k + 1, TYPEMIN(int64_t));
    maxprofit[0] = 0;

    auto add_item = [&](int i) {
        for (int pos = k; pos >= items[i].second; --pos)
            if (maxprofit[pos - items[i].second] != TYPEMIN(int64_t))
                maxprofit[pos] = max(maxprofit[pos], maxprofit[pos - items[i].second] + items[i].first);
    };

    std::function<void(int, int)> go = [&](int l, int r) {
        if (l == r) {
            int64_t sm = 0;
            for (int i = 0; i < SZ(arr[l]); ++i) {
                sm += arr[l][i];

                if (maxprofit[k - (i + 1)] != TYPEMIN(int64_t))
                    ans = max(ans, maxprofit[k - (i + 1)] + sm);
            }

            return;
        }

        auto state = maxprofit;
        int m = (l + r) / 2;
        for (int i = l; i <= m; ++i)
            add_item(i);

        go(m + 1, r);

        maxprofit = state;
        for (int i = m + 1; i <= r; ++i)
            add_item(i);
        go(l, m);

        maxprofit = state;
    };

    go(0, n - 1);
    cout << ans << "\n";
    return 0;
}


#include<stdio.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
const int maxn=107;
typedef long long LL;

inline LL gcd(LL x,LL y)
{
	return !y?x:gcd(y,x%y);
}

inline LL times(LL a,LL b,LL P)
{
	a%=P;
	b%=P;
	LL ret=0;
	while(b){
		if(b&1){
			ret+=a;
			if(ret>=P) ret-=P;
		}
		a+=a;
		if(a>=P) a-=P;
		b>>=1;
	}
	return ret;
}
inline LL power(LL a,LL b,LL P)
{
	a%=P;
	LL c=1;
	for(;b;b>>=1,a=times(a,a,P))
		if(b&1) c=times(c,a,P);
	return c;
}

void findfac(LL v, LL res[], int &cnt){
	for(LL i=2;i*i<=v;++i){
		while((v%i)==0){
			res[cnt++]=i;
			v/=i;
		}
	}
	if(v!=1) res[cnt++]=v;
}

LL M,X;
LL facm[maxn],facr[maxn][maxn];
int pm[maxn],pr[maxn],cntm=0,cntr[maxn];

LL ans=0;

void dfs(int p,LL u,LL phi,LL tp){
	if(p==cntm){
		if(u!=1) ans+=phi/tp;
		return;
	}

	dfs(p+1,u,phi,tp);

	if(u!=1) assert(power(X,tp,u)==1);

	u*=facm[p];
	phi*=facm[p]-1;
	LL tt=tp*(facm[p]-1);
	for(int i=0;i<cntr[p];++i){
		if(power(X,tt/facr[p][i],u)==1) tt/=facr[p][i];
	}

	dfs(p+1,u,phi,tt);

	for(int i=2;i<=pm[p];++i){
		u*=facm[p];
		phi*=facm[p];
		if(power(X,tt,u)!=1) tt*=facm[p];
		dfs(p+1,u,phi,tt);
	}
}

int main(){
	std::cin>>M>>X;
	assert(gcd(M,X)==1);
	findfac(M,facm,cntm);
	std::sort(facm,facm+cntm);
	//for(int i=0;i<cntm;++i) std::cerr<<facm[i]<<std::endl;

	int t=0;
	for(int i=0;i<cntm;++i){
		if(i==0 || facm[i]!=facm[i-1]){
			facm[t++]=facm[i];
			pm[t-1]=1;
		}else{
			pm[t-1]++;
		}
	}
	cntm=t;

	//for(int i=0;i<cntm;++i) std::cerr<<facm[i]<<": "<<pm[i]<<std::endl;

	for(int i=0;i<cntm;++i){
	       	findfac(facm[i]-1,facr[i],cntr[i]);
		std::sort(facr[i],facr[i]+cntr[i]);
	}

	dfs(0,1,1,1);
	++ans;

	std::cout<<ans<<std::endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <set>

#define ulong unsigned long long
#define ll long long

using namespace std;

int cnt[5001];
ll dp[5001][5001];

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

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

    for (int i = 2; i <= 5000; ++i) {
        for (int j = 1; j <= 5000; ++j) {
            dp[i][j] += dp[i - 1][j];
        }

        int v = i;
        for (int j = 2; j <= sqrt(i); ++j) {
            while (v % j == 0) {
                v /= j;
                dp[i][j]++;
            }
        }

        if (v > 1)
            dp[i][v]++;
    }

    ll ans = 0;
    vector<int> best(5001, 1);
    for (int i = 0; i <= 5000; ++i) {
        if (cnt[i] == 0) {
            continue;
        }
        for (int j = 1; j <= 5000; ++j) {
            ans += dp[i][j] * cnt[i];
            if (dp[i][j] > 0) {
                best[i] = j;
            }
        }
    }

    while (*max_element(best.begin(), best.end()) > 1) {
        vector<int> freq(5001, 0);

        for (int i = 0; i <= 5000; ++i) {
            freq[best[i]] += cnt[i];
        }

        int prime = max_element(freq.begin(), freq.end()) - freq.begin();
        int c = freq[prime];

        if (c * 2 <= n)
            break;
        if (prime == 1)
            break;

        ans -= c;
        ans += (n - c);

        for (int i = 0; i <= 5000; ++i) {
            if (best[i] != prime) {
                best[i] = 1;
            }
            else {
                dp[i][best[i]] -= 1;
                while(best[i] > 1 && dp[i][best[i]] == 0) {
                    best[i]--;
                }
            }

        }

    }

    cout << ans << endl;

}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(a) (1ll<<(x))
#define endl "\n"
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
//----
const int mod = 1e9 + 7, N = 1e6 + 5;
int c[5];
int dp[N] = {1, 1, 2};
int add(int a, int b) {
	int res = a + b;
	if(res >= mod)res -= mod;
	return res;
}
int mul(int a, int b) {
	return a * b % mod;
}
int main() {
	int n; cin >> n;
	rep(i, 0, n) {
		int x; cin >> x; c[x]++;
	}
	dp[1] = 1;
	rep(i, 3, c[1] + 1)dp[i] = add(dp[i - 1], mul((i - 1), dp[i - 2]));
	rep(i, n - c[2] + 1, n + 1)dp[c[1]] = mul(dp[c[1]], i);
	cout << dp[c[1]] << endl;
	return 0;
}


#include <vector>
#include <list>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
#include <complex>
using namespace std;

#define ME(s) memset((s), 0, sizeof((s)))
#define MM(s,a) memset((s),(a),sizeof((s)))
#define MCP(s,a) memcpy((s), (a), sizeof(s))
#define LL long long
#define LD long double
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define PDD pair<double, double>
#define mkp(a,b) make_pair((a),(b))
#define y first
#define x second
#define sqr(a) ((a)*(a))
#define lowbit(x) ((x)&(-(x)))
#define two(x) (1<<(x))
#define contain(mask,x) (((mask)&two(x))!=0)

//#define OFFLINEJUDGE
#ifdef OFFLINEJUDGE
FILE *fin=freopen("a.in","r",stdin);
FILE *fout=freopen("a.out","w",stdout);
#else
FILE *fin=stdin;
FILE *fout=stdout;
#endif

int f[1<<19];

int to[20][6];

int get(int mask){
	int &ret=f[mask];
	if(ret>-1)
		return ret;
	ret=0;
	for(int i=0;i<19;i++)
		if(mask&(1<<i)){
			for(int j=0;j<6;j++){
				int sub=mask;
				int t=i;
				while(1){
				//	cout << sub << " " << t << endl;
					sub^=1<<t;
					if(get(sub)==0)
						return ret=1;
					t=to[t][j];
					if(t==-1||(sub&(1<<t))==0)
						break;
				}
			}
		}
	return ret=0;
}

int main(){

	memset(to,-1,sizeof(to));
	to[0][2]=1;
	to[0][3]=4;
	to[0][4]=3;
	//
	to[1][2]=2;
	to[1][3]=5;
	to[1][4]=4;
	to[1][5]=0;
	//
	to[2][3]=6;
	to[2][4]=5;
	to[2][5]=1;
	//
	to[3][1]=0;
	to[3][2]=4;
	to[3][3]=8;
	to[3][4]=7;
	//
	to[4][0]=0;
	to[4][1]=1;
	to[4][2]=5;
	to[4][3]=9;
	to[4][4]=8;
	to[4][5]=3;
	//
	to[5][0]=1;
	to[5][1]=2;
	to[5][2]=6;
	to[5][3]=10;
	to[5][4]=9;
	to[5][5]=4;
	//
	to[6][0]=2;
	to[6][3]=11;
	to[6][4]=10;
	to[6][5]=5;
	//
	to[7][1]=3;
	to[7][2]=8;
	to[7][3]=12;
	//
	to[8][0]=3;
	to[8][1]=4;
	to[8][2]=9;
	to[8][3]=13;
	to[8][4]=12;
	to[8][5]=7;
	//
	to[9][0]=4;
	to[9][1]=5;
	to[9][2]=10;
	to[9][3]=14;
	to[9][4]=13;
	to[9][5]=8;
	//
	to[10][0]=5;
	to[10][1]=6;
	to[10][2]=11;
	to[10][3]=15;
	to[10][4]=14;
	to[10][5]=9;
	//
	to[11][0]=6;
	to[11][4]=15;
	to[11][5]=10;
	//
	to[12][0]=7;
	to[12][1]=8;
	to[12][2]=13;
	to[12][3]=16;
	//
	to[13][0]=8;
	to[13][1]=9;
	to[13][2]=14;
	to[13][3]=17;
	to[13][4]=16;
	to[13][5]=12;
	//
	to[14][0]=9;
	to[14][1]=10;
	to[14][2]=15;
	to[14][3]=18;
	to[14][4]=17;
	to[14][5]=13;
	//
	to[15][0]=10;
	to[15][1]=11;
	to[15][4]=18;
	to[15][5]=14;
	//
	to[16][0]=12;
	to[16][1]=13;
	to[16][2]=17;
	//
	to[17][0]=13;
	to[17][1]=14;
	to[17][2]=18;
	to[17][5]=16;
	//
	to[18][0]=14;
	to[18][1]=15;
	to[18][5]=17;
	//

	char ch;
	int cnt=0;
	int mask=0;
	while(cnt<19){
		ch=getchar();
		if(ch==-1)
			break;
		if(ch=='.'||ch=='O'){
			if(ch=='O'){
				mask|=1<<cnt;
			//	cout << cnt << endl;
			}
			cnt++;
		}
	}

	memset(f,-1,sizeof(f));
	int ret=get(mask);

	printf(ret?"Karlsson\n":"Lillebror\n");

	return 0;
}


#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>

using namespace std;

#define tr(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); ++ i)
#define rep(i, n) for (int i = 0; i < n; ++ i)
#define pii pair <int, int>
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define ll long long

#define CharSet 26
#define Mod 12345

class Matrix: public vector < vector <int> >
{
    private:
        Matrix(int n, vector <int> a)
        {
            this->clear();
            rep (i, n)
                pb(a);
        }
    public:
        Matrix()
        {
            this->clear();
        }
        Matrix(int n, int m)
        {
            this->clear();
            *this = Zero(n, m);
        }
        Matrix Zero(int n, int m)
        {
            return Matrix (n, vector <int> (m, 0));
        }
        Matrix One(int n)
        {
            Matrix res(n, n);
            rep (i, n)
                res[i][i] = 1;
            return res;
        }
        Matrix operator * (const Matrix &b)
        {
            Matrix c;
            if (rows() == b.lines())
            {
                int u = lines();
                int v = rows();
                int w = b.rows();
                c = Zero(u, w);
                rep (i, u)
                    rep (j, v)
                        rep (k, w)
                        {
                            c[i][k] += (*this)[i][j] * b[j][k];
                            c[i][k] %= Mod;
                        }
            }
            else
                cerr << "error occurred at " << __LINE__ << endl;
            return c;
        }
        Matrix pow(ll n)
        {
            Matrix res;
            if (lines() == rows())
            {
                int l = lines();
                res = res.One(l);
                Matrix tmp(*this);
                for (int i = 0; (n >> i) > 0; ++ i)
                {
                    if ((n & (1LL << i)) > 0)
                        res = res * tmp;
                    tmp = tmp * tmp;
                }
            }
            else
                cerr << "error occurred at " << __LINE__ << endl;
            return res;
        }
        size_type lines() const
        {
            return size();
        }
        size_type rows() const
        {
            return (size() == 0) ? 0 : (*this)[0].size();
        }
};

ll N;
int C;
bool Appear[CharSet], isOne[CharSet];

int main()
{
    cin >> N >> C;
    fill(Appear, Appear + CharSet, 0);
    fill(isOne, isOne + CharSet, 0);
    vector <pii> cd;
    cd.clear();
    rep (i, C)
    {
        char Ch;
        int t;
        cin >> Ch >> t;
        Appear[Ch - 'A'] = 1;
        if (t != 1)
            cd.pb(mp(Ch - 'A', t));
        else
            isOne[Ch - 'A'] = 1;
    }
    vector <int> pd;
    pd.clear();
    int cds = cd.size();
    pd.pb(1);
    rep (i, cds)
        pd.pb(pd.back() * cd[i].y);
    int matsize = pd.back();
    Matrix mat(matsize, matsize);
    rep (i, matsize)
    {
        rep (ch, 26)
            if (Appear[ch])
            {
                int target = i;
                rep (k, cds)
                    if (cd[k].x == ch)
                    {
                        if ((target % pd[k + 1]) / pd[k] == cd[k].y - 1)
                            target -= (cd[k].y - 1) * pd[k];
                        else
                            target += pd[k];
                    }
                ++ mat[i][target];
            }
    }
    mat = mat.pow(N);
    ll ans = 0;
    rep (i, matsize)
    {
        bool Flag = 1;
        rep (ch, 26)
            if (Appear[ch] && !isOne[ch])
            {
                bool Satisfy = 0;
                bool Exist = 0;
                rep (k, cds)
                    if (cd[k].x == ch)
                    {
                        Exist = 1;
                        if ((i % pd[k + 1]) / pd[k] == 0)
                            Satisfy = 1;
                    }
                if (Exist && !Satisfy)
                {
                    Flag = 0;
                    break;
                }
            }
        if (Flag)
            ans = (ans + mat[0][i]) % Mod;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 200'010;
int a[maxN];
int cnt[maxN];
int sumx[maxN];
int presum[maxN];
int first_pos[maxN*2];
int n, x, ans, M;
vector<int> pos[maxN];

int find_most()
{
    int res = 0;
    for (int i = 1; i <= n; i++)
        if (cnt[i] > cnt[res])
            res = i;
    return res;
}
bool ans_is_n()
{
    int res = 0;
    for (int i = 1; i <= n; i++)
        if (cnt[i] == cnt[x])
            res++;
    return res >= 2;
}
int fun2(int t)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        presum[i] = presum[i-1];
        if (a[i] == x) presum[i]++;
        if (a[i] == t) presum[i]--;
    }
    for (int i = 0; i <= 2*n; i++) first_pos[i] = n + 1;
    first_pos[n] = 0;
    for (int i = 1; i <= n; i++)
    {
        res = max(res, i - first_pos[presum[i] + n]);
        first_pos[presum[i] + n] = min(first_pos[presum[i] + n], i);
    }
    return res;
}
int main()
{
    scanf("%d", &n);
    M = min((int) sqrt(n) - 3, 500);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        pos[a[i]].push_back(i);
        cnt[a[i]]++;
    }
    x = find_most();
    for (int i = 1; i <= n; i++)
        sumx[i] = sumx[i-1] + (int)(a[i] == x);
    if (ans_is_n())
    {
        printf("%d\n", n);
        return 0;
    }
    for (int i = 1; i <= min(n, 100); i++)
    {
        if (i == x) continue;
        ans = max(ans, fun2(i));
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

vector < int > ga[N], gb[N];

bool usa[N];
bool findb = false;
int cnta = 0;

void dfsa(int v, int b){
    usa[v] = true;
    if(v == b) findb = true;
    ++cnta;
    for(int to : ga[v]){
        if(usa[to]) continue;
        dfsa(to, b);
    }
}

bool usb[N];
bool finda = false;
int cntb = 0;

void dfsb(int v, int a){
    usb[v] = true;
    if(v == a) finda = true;
    ++cntb;
    for(int to : gb[v]){
        if(usb[to]) continue;
        dfsb(to, a);
    }
}

void solve(){
    int n, m, a, b;
    cin >> n >> m >> a >> b;
    --a; --b;
    for(int i = 0; i < m; ++i){
        int x, y; cin >> x >> y;
        --x; --y;
        if(x != a && y != a){
            ga[x].push_back(y);
            ga[y].push_back(x);
        }
        if(x != b && y != b){
            gb[x].push_back(y);
            gb[y].push_back(x);
        }
    }

    long long sum = 0;
    for(int i = 0; i < n; ++i){
        if(usa[i] == false && i != a){
            dfsa(i, b);
            if(!findb) sum += cnta;
            findb = false; cnta = 0;
        }
    }
    long long ans = 0;
    for(int i = 0; i < n; ++i){
        if(usb[i] == false && i != b){
            dfsb(i, a);
            if(!finda) ans += cntb * sum;
            finda = false; cntb = 0;
        }
    }

    cout << ans << endl;

    for(int i = 0; i < n; ++i){
        usa[i] = false;
        usb[i] = false;
        ga[i].clear();
        gb[i].clear();
    }
}

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

    int t;
    cin >> t;

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


#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <iterator>
#include <utility>
#include <string>
#include <algorithm>
#include <time.h>
#include <tuple>
#include <deque>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <cassert>
//#include <tr1/unordered_map>
//#include <tr1/unordered_set>

#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define pll pair<long long, long long>
#define all(v) v.begin(), v.end()

using namespace std;

typedef long long int ll;

mt19937 rnd(1337);

ostream& operator<< (ostream& a, const pll b) {
    cout << "{" << b.first << ", " << b.second << "}";
    return a;
}

ostream& operator<< (ostream& a, const vector<ll>& b) {
    for (auto& k : b) cout << k << " ";
    return a;
}

ostream& operator<< (ostream& a, const vector<pll>& b) {
    for (auto& k : b) cout << k << " ";
    return a;
}

#ifdef HOME
#define dbg(x) cout << #x << " : " << (x) << '\n';
#else
#define dbg(x)
#endif

const ll INF = (ll)1e18;
const ll MOD = 1000 * 1000 * 1000 + 7, MOD2 = 274876858367;
const ll maxn = (ll)1e6 + 10, L = 22;
const double pi = 3.14159265358979;

int main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  vector<ll> c(n);
  for (ll i = 0; i < n; ++i) {
    cin >> c[i];
  }
  sort(all(c));
  vector<vector<ll>> ans(k + 1);
  ll kek = 0, lst = n - 1;
  for (ll i = n - 1; i >= 0; --i) {
    if (kek + c[i] >= 0) {
      ans[0].pb(c[i]);
      lst = i;
      kek += c[i];
    } else {
      break;
    }
  }
  ll now = 0;
  for (ll i = 0; i < lst; ++i) {
    ans[now].pb(c[i]);
    now = (now + 1) % ans.size();
  }
  ll res = 0;
  for (auto& x : ans) {
    sort(all(x));
    now = 0;
    for (ll y : x) {
      res += y * now;
      ++now;
    }
  }
  cout << res << endl;
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <string.h>
#include <fstream>

using namespace std;

#define LL long long
#define N 1000002
#define M 1000100
#define MP make_pair
#define Pi acos(-1.0)
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define ls (rt << 1)
#define rs (ls | 1)
#define md ((ll+rr)/2)
#define lson ll, md, ls
#define rson md+1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define sqr(x) ((x)*(x))
#define eps 1e-6
#define uLL unsigned long long
#define ui unsigned int
LL powmod(LL a,LL b) {LL res=1;for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
#define F(x) ((x)/3+((x)%3 == 1 ? 0 : tb))
#define G(x) ((x)<tb ? (x)*3+1 : ((x) - tb)*3+2)
#define lowbit(x) ((x)&(-x))
#define fi first
#define se second
#define pii pair<int,int>
#define pli pair<LL,int>
#define pb push_back
#define MP make_pair
LL gcd(LL x,LL y){
    while(y){
        LL t = x % y;
        x = y;
        y = t;
    }
    return x;
}

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

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

int n, tot, dfn[N], fa[N][20], ed[N], dep[N];
int bel[N], belong[N];
vector<int> g[N];

void dfs(int u, int id)
{
	bel[u] = id;
	dfn[u] = ++tot;
	for(int i = 1; i < 20; i++)
	{
		fa[u][i] = fa[fa[u][i - 1]][i - 1];
	}
	for(auto v : g[u])
	{
		if(v == fa[u][0]) continue;
		fa[v][0] = u;
		dep[v] = dep[u] + 1;
		dfs(v, id);
	}
	ed[u] = tot;
}

int lca(int u, int v)
{
	if(bel[u] != bel[v]) return -1;
	if(dep[u] > dep[v]) swap(u, v);
	for(int i = 0; dep[u] != dep[v]; i++)
		if((dep[v] - dep[u]) >> i & 1) v = fa[v][i];
	if(u == v) return v;
	for(int i = 19; i >= 0; i--)
		if(fa[u][i] != fa[v][i])
		{
			u = fa[u][i];
			v = fa[v][i];
		}
	return fa[u][0];
}

inline bool cmp(int i, int j){return dfn[i] < dfn[j];}

struct BCC
{
	int fst[N], nxt[M], vv[M], e;
	int n, belong[N], dfn[N], low[N], stk[N], tot, in[N];
	void init()
	{
		e = 0;
		memset(fst, -1, sizeof fst);
		memset(dfn, -1, sizeof dfn);
	}

	void add(int u, int v)
	{
		vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
		vv[e] = u, nxt[e] = fst[v], fst[v] = e++;
	}

	void dfs(int u, int f = -1)
	{
		int tmp = tot;
		stk[dfn[u] = low[u] = tot++] = u;
		in[u] = 1;
		for(int i = fst[u]; ~i; i = nxt[i])
		{
			if(i == f) continue;
			int v = vv[i];
			if(dfn[v] == -1) dfs(v, i ^ 1), chkmin(low[u], low[v]);
			else if(in[v]) chkmin(low[u], dfn[v]);
		}
		if(dfn[u] == low[u])
		{
			while(tot != tmp)
			{
				tot--;
				in[stk[tot]] = 0;
				belong[stk[tot]] = n;
			}
			n++;
		}
	}
}bcc;

int stk[N];

int main()
{
	n = read();
	int m = read(), q = read();
	bcc.init();
	vector< pii > E;
	for(int i = 0; i < m; i++)
	{
		int u = read(), v = read();
		bcc.add(u, v);
		E.pb(MP(u,v));
	}
	bcc.n = bcc.tot = 1;
	for(int i = 1; i <= n; i++)
		if(bcc.dfn[i] == -1) bcc.dfs(i);
	for(int i = 1; i <= n; i++)
	{
		belong[i] = bcc.belong[i];
	}

	for(auto x : E)
	{
		int u = x.fi, v = x.se;
		if(belong[u] != belong[v])
		{
			g[belong[u]].pb(belong[v]), g[belong[v]].pb(belong[u]);
		}
	}

	for(int i = 1; i <= n; i++)
		if(!dfn[i]) dfs(i, i);

	int R = 0, id = 1;
	for(; q; q--, id++)
	{
		int nn = read(), mm = read();
		vector<int> x;
		x.resize(nn);
		for(int i = 0; i < nn; i++)
		{
			x[i] = (read() + R) % n, x[i] = belong[x[i] == 0 ? n : x[i]];
		}
		vector< pii > ee;
		vector< int > V(x);
		for(int i = 0 ; i < mm; i++)
		{
			int u = (read() + R) % n, v = (read() + R) % n;
			u = belong[u == 0 ? n : u], v = belong[v == 0 ? n : v];
			ee.pb(MP(u, v));
			V.pb(u) , V.pb(v);
		}
		sort(V.begin(), V.end(), cmp);
		nn = V.size();
		for(int i = 0; i < nn - 1; i++)
		{
			if(V[i] == V[i + 1]) continue;
			int lc = lca(V[i], V[i + 1]);
			if(lc > 0) V.pb(lc);
		}
		sort(V.begin(), V.end(), cmp);
		V.resize(unique(V.begin(), V.end()) - V.begin());
		nn = V.size();

		for(int i = 0; i < nn; i++)
			bcc.fst[V[i]] = -1; bcc.e = 0;
		tot = 0;
		for(int i = 0; i < nn; i++)
		{
			int u = V[i];
			while(tot && !(dfn[stk[tot - 1]] <= dfn[u] && dfn[u] <= ed[stk[tot - 1]])) tot--;
			if(tot) { bcc.add(u, stk[tot - 1]);}
			stk[tot++] = u;
		}
		for(auto u : ee)
			bcc.add(u.fi, u.se);
		bcc.tot = bcc.n = 1;
		for(int i = 0; i < nn; i++) bcc.dfn[V[i]] = -1;
		for(int i = 0; i < nn; i++)
			if(bcc.dfn[V[i]] == -1) bcc.dfs(V[i]);
		bool ans = 1;

		for(int i = 1; i < x.size(); i++)
			if(bcc.belong[x[i]] != bcc.belong[x[0]])
			{
				ans = 0;
				break;
			}
		puts(ans ? "YES" : "NO");
		R = (R + (ans ? id : 0)) % n;
	}
	return 0;

}


#include <iostream>
#include <stack>
#include <math.h>
#include <set>
#include <stdio.h>
#include <algorithm>
#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;
    }
}

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

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


#pragma comment(linker, "/STACK:16777216")
#pragma warning(disable:4786)

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cctype>
#include <string>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <numeric>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define MAX_SIZE 100100
#define MAX_LENGTH 100100
#define INF ( 1 << 29 );

#define CLR( a ) memset( a , NULL , sizeof( a ) )
#define MEM( a ) memset( a , -1 , sizeof( a ) )
#define fort( a , b ) for( ind = ( a ); ind <= ( b ); ind++ )
#define foriab( a , b ) for( i = ( a ); i < ( b ); i++ )
#define fori( a ) for( i = 0; i < ( a ); i++ )
#define forj( a ) for( j = 0; j < ( a ); j++ )
#define print1( a ) printf( "%d ---\n" , ( a ) )
#define print2( a , b ) printf( "%d %d ---\n" , ( a ) , ( b ) )

typedef __int64 lll ;

int MIN( int a , int b ) { return a < b ? a : b ; }
int MAX( int a , int b ) { return a > b ? a : b ; }
int GCD( int a , int b ) { while( b ) b ^= a ^= b ^= a %= b ; return a ; }
int LCM( int a , int b ) { return a * ( b / GCD( a , b ) ) ; }
void SWAP( int &a , int &b ) { a = a ^ b ; b = a ^ b ; a = a ^ b ; }

const double PI = acos( -1 ) ;
const double EPS = 1e-11 ;

struct st {
    int b , s , r ;
} arr[ 111 ][ 111 ] ;

struct ss {
    int p , in ;
    bool operator() ( const ss &a , const ss &b ) {
        return a.p > b.p ;
    }
} prof[ 111 ] , cmp ;
int N , M , K , mask , memo[ 11 ][ 111 ] ;

int dp( int i , int j ) {
    if( i >= N ) { return 0 ; }
    if( j >= M ) {
        return dp( i + 1 , 0 ) ;
    }
    int &ret = memo[ i ][ j ] ;
    if( ret != -1 ) {
        return ret ;
    }
    int res , k , l ;
    res = 0 ;
    int ma = -INF ;
    if( ( mask & ( 1 << i ) ) == 0 ) {
        for( k = 0 ; k < N ; k++ ) {
            if( k != i ) {
                ma = MAX( ma , arr[ k ][ j ].s ) ;
            }
        }
        for( l = 0 ; l <= arr[ i ][ j ].r ; l++ ) {
            int cost = arr[ i ][ j ].b * l ;
            if( l <= ma ) {
                int a = ma * l - cost ;
                if( a > 0 ) {
                    res += a ;
                }
            }
            else {
                break ;
            }
        }
    }
    res += dp( i , j + 1 ) ;
    //printf( "%d %d :: %d\n" , i , j , res ) ;
    return ret = res ;
}

int main() {
    //freopen( "C.in" , "r" , stdin ) ;
	//freopen( "out.txt" , "w" , stdout ) ;
	int T , res , n , i , ind , m , k , j , l ;
	while( scanf( "%d%d%d" , &n , &m , &k ) != EOF ) {
	    K = k ;
	    N = n ;
	    M = m ;
        CLR( arr ) ;
        res = 0 ;
        fori( n ) {
            char brr[ 200 ] ;
            scanf( "%s" , &brr ) ;
            forj( m ) {
                scanf( "%d%d%d" , &arr[ i ][ j ].b , &arr[ i ][ j ].s , &arr[ i ][ j ].r ) ;
            }
        }
        /*fori( n ) {
            forj( m ) {
                printf( "%d %d %d\n" , arr[ i ][ j ].b , arr[ i ][ j ].s , arr[ i ][ j ].r ) ;
            }
            printf( "========================\n" ) ;
        }*/
        res = 0 ;
        for( i = 0 ; i < n ; i++ ) {
            for( l = 0 ; l < n ; l++ ) {
                if( i != l ) {
                    k = K ;
                    int re = 0 ;
                    forj( m ) {
                        int sell = arr[ l ][ j ].s ;
                        int buy = arr[ i ][ j ].b ;
                        if( sell > buy ) {
                            prof[ j ].p = ( sell - buy ) ;
                        }
                        else {
                            prof[ j ].p = 0 ;
                        }
                        prof[ j ].in = arr[ i ][ j ].r ;
                    }
                    sort( prof , prof + m , cmp ) ;
                    forj( m ) {
                        if( i == 0 && l == 1 ) {
                            //print2( prof[ j ].p , prof[ j ].in ) ;
                        }
                        if( k < prof[ j ].in ) {
                            re += ( k * prof[ j ].p ) ;
                        }
                        else {
                            re += ( prof[ j ].in * prof[ j ].p ) ;
                        }
                        k -= prof[ j ].in ;
                        if( k <= 0 ) { break ; }
                    }
                    res = MAX( res , re ) ;
                }
            }
        }
		printf( "%d\n" , res ) ;
	}
	return 0 ;
}

/*bambhbdjgmlnhqsshhfpsnhftteroephehidadfgpilpebfdik*/


#include<bits/stdc++.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define huan putchar('\n')
#define kong putchar(' ')
using namespace std;

inline void read(int &x)
{
	x=0;char ch=getchar();int pd=1;
	while(ch<'0'||ch>'9'){if(ch=='-')pd=-pd;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x*=pd;
}
inline void write(const int &x)
{
	char ggg[10001];int s=0;int tmp=x;
	if(tmp==0){putchar('0');return;}
	if(tmp<0){tmp=-tmp;putchar('-');}
	while(tmp>0){ggg[s++]=tmp%10+'0';tmp/=10;}
	while(s>0){putchar(ggg[--s]);}
}

inline void wrl(const int &x)
{
	write(x);
	putchar('\n');
}

inline void wrs(const int &x)
{
	write(x);
	putchar(' ');
}

inline int F(int x)
{
	int ans=0;
	for(;!(x&1);x>>=1)++ans;
	return ans;
}

int n,x,p;
int k;
double f[500][500];

int main()
{
	read(x);
	read(k);
	read(p);
	double p2=p/100.0;
	double p1=1-p2;
	for(register int i=0;i<=k;++i)
	{
		f[0][i]=F(x+i);
	}
	for(register int i=0;i<k;++i)
	{
		for(register int j=0;j<=k;++j)
		{
			if(j)
			f[i+1][j-1]+=p1*f[i][j];
			if(j*2<=k)
			f[i+1][j<<1]+=(f[i][j]+1)*p2;
		}
	}
	printf("%.10lf",f[k][0]);
	return 0;
}


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

int dx[] = {1 , -1 , 0 ,  0};
int dy[] = {0 ,  0 , 1 , -1};
#define ll long long
#define vi vector <int>
#define pi pair< int , int >
#define pl pair< ll , ll >
#define pii pair< pi , int >
#define fr first
#define sc second
#define pb push_back
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PI acos(-1)
#define eps 1e-6
#define OO   INT_MAX
#define infin LLONG_MAX
#define mod 1000000007
#define SPEED_UP ios_base::sync_with_stdio(false); cin.tie(NULL);
#define SZ(x) ((int)(sizeof(x)))
#define sz(v) (int)v.size()
#define all(t) t.begin(),t.end()
#define frei freopen("in.in","r",stdin)
#define freo freopen("out.out","w",stdout)
#define SET(x,y)    x|=(1<<y)
#define CLEAR(x,y)  x&= ~(1<<y)
#define TOGGLE(x,y) x^=(1<<y)
#define READ(x,y)   ((bool)((ll)(x&(1<<y)) != 0))
#define I(x) scanf("%d",&x)
#define I2(x,y) scanf("%d%d",&x,&y)
#define I3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define I4(x,y,z,r) scanf("%d%d%d%d",&x,&y,&z,&r)
#define L(x) scanf("%I64d",&x)
#define L2(x,y) scanf("%I64d%I64d",&x,&y)
#define L3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define L4(x,y,z,r) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&r);
#define D4(x,y,z,r) scanf("%lf%lf%lf%lf",&x,&y,&z,&r);
#define MAX (int)2e5+100
string a , b , s;
int kmp[200][30];
int dp[200][200][200];
int get(int i , int j , int k)
{
    if(i == a.size() || j == b.size() )return k == s.size() ? -1e5 : 0;

    int &ret = dp[i][j][k];
    if(ret + 1)return ret;

    int res1 = get(i+1,j,k) ,   res2 = get(i,j+1,k) , res3 = -1e5;
    if(a[i] == b[j])res3 = 1 + get(i+1,j+1,kmp[k][a[i] - 'A']);

    return ret = max({res1,res2,res3});
}
void get_path(int i = 0 , int j = 0 , int k = 0 , bool took = 0)
{
    if(i == a.size() || j == b.size() ){ cout << (took ? "\n" : "0\n"); return; }

    int ret = get(i,j,k);
    if(ret == get(i+1,j,k))
        get_path(i+1,j,k,took);
    else if (ret == get(i,j+1,k))
        get_path(i,j+1,k,took);
    else if(a[i] == b[j] && ret == 1 + get(i+1,j+1,kmp[k][a[i] - 'A']))
    {
        cout << a[i];
        get_path(i+1,j+1,kmp[k][a[i] - 'A'] , 1);
    }
}
void preffixFunc(string s)
{
    vi ret(s.size() + 10);
    for(int i = 1 , k = 0 ; i < s.size() ; i++)
    {
        while(k > 0 && s[i] != s[k])
            k = ret[k-1];
        if(s[i] == s[k])k++;
        ret[i] = k;
    }

    for(int i = 0 ; i < s.size() ; i++)
        for(int j = 0 ; j < 26 ; j++)
            kmp[i][j] = ( j == s[i] - 'A' ? i+1 : i > 0 ? kmp[ret[i-1]][j] : 0);

    for(int j = 0 ; j < 26 ; j++)kmp[s.size()][j] = s.size();
}
int main()
{
    cin >> a >> b >> s;
    preffixFunc(s);
    memset(dp,-1,SZ(dp));
    get_path();
    return 0;
}


#include<cstdio>
#include<algorithm>

using namespace std;

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

//const double eps = 1e-15;
const double PI = 3.141592653589793238462643383279502; //acos(-1);

int main(){
    int n = read(), r = read(), v = read();
    //int n, r, v; scanf("%d %d %d", &n, &r, &v);
    while (n--){
        int s = read(), f = read();
        //int s, f; scanf("%d %d", &s, &f);
        double len = f - s; double c = PI * r * 2; int cnt = len / c; double ans = c * cnt / v;
        len -= c * cnt; len /= 2;
        double L = 0, R = len, mid;
        for (int i = 1; i <= 100; ++i){
            mid = (L + R) / 2;
            if (mid + r * sin(mid / r) >= len) R = mid;
            else L = mid;
        }
        ans += 2 * L / v;
        printf("%.9f\n", ans);
    }
    return 0;
}//hellobig_tq


#include <bits/stdc++.h>

using namespace std;

#define ll 		long long
#define mp 		make_pair
#define sz(v) 	(int)v.size()
#define pb    	push_back
#define b_p(n)		__builtin_popcount(n)
#define s second
#define f first

int const mxsz = 1000009;
bool vis1[mxsz], vis2[mxsz];

ll reach(ll h1, ll x1, ll y1, ll a1, ll m) {
	memset(vis1, 0, sizeof(vis1));
	int t = 0;
	while (!vis1[h1]) {
		vis1[h1] = 1;
		if (h1 == a1)
			return t;
		h1 = (x1 * h1 + y1) % m;
		t++;
	}
	return -1;
}
ll takestoreachgoalagain(ll h1, ll x1, ll y1, ll a1, ll m) {
	ll t = 0;
	h1 = a1;
	memset(vis1, 0, sizeof(vis1));
	while (true) {
		vis1[h1] = 1;
		if (t != 0 && h1 == a1)
			return t;
		h1 = (x1 * h1 + y1) % m;
		t++;
		if (t > mxsz + 10)
			return -1;
	}
	return -1;
}
int main() {
	ll m, h1, a1, x1, y1, h2, a2, x2, y2;
	cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
	ll t1 = reach(h1, x1, y1, a1, m);
	ll t2 = reach(h2, x2, y2, a2, m);
	if (t1 == t2 && t1 != -1)
		cout << t1 << endl;

	else if (t1 == -1 || t2 == -1)
		cout << -1 << endl;
	else {
		ll cyc1 = takestoreachgoalagain(h1, x1, y1, a1, m);
		ll cyc2 = takestoreachgoalagain(h2, x2, y2, a2, m);
		if (cyc1 == -1 && cyc2 == -1)
			cout << -1 << endl;
		else if (cyc1 == -1) {
			if (t1 > t2) {
				ll T = t1 - t2;
				if ((T + t2) % cyc2 == 0)
					cout << t1 << endl;
				else
					cout << -1 << endl;
			} else
				cout << -1 << endl;
		} else if (cyc2 == -1) {
			if (t2 > t1) {
				ll T = t2 - t1;
				if ((T + t1) % cyc1 == 0)
					cout << t2 << endl;
				else
					cout << -1 << endl;
			} else
				cout << -1 << endl;

		} else {
			ll res = max(t1, t2);
			ll mn = 9223372036854775807LL;
			t1 = abs(t1 - res) % cyc1;
			t2 = abs(t2 - res) % cyc2;
			for (ll i = 0; i <= mxsz; ++i) {
				if (t1 == 0) {
					if ((cyc1 * i + t2) % cyc2 == 0)
						mn = min(mn, cyc1 * i);
					else if ((cyc2 * i + abs(cyc2 - t2) + t1) % cyc1 == 0)
						mn = min(mn, cyc2 * i + abs(cyc2 - t2));
				}

				if (t2 == 0) {
					if ((cyc2 * i + t1) % cyc1 == 0)
						mn = min(mn, cyc2 * i);
					else if ((cyc1 * i + abs(cyc1 - t1) + t2) % cyc2 == 0)
						mn = min(mn, cyc1 * i + abs(cyc1 - t1));
				}
			}
			if (mn == 9223372036854775807LL)
				cout << -1 << endl;
			else
				cout << mn + res << endl;
		}
	}
//	cout << -1 << endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
int x, k, p, q, B, curx, n;
ll c[10],w;
map<int, ll> mp;
vector<ll> dp;
vector<int> bitset;
mat a, o;
void pow2(mat &a) {
	mat b(B, vector<ll>(B, 1e18));
	for (int k : bitset) {
		for (int i : bitset)
			for (int j : bitset)
				b[i][j] = min(b[i][j], a[i][k] + a[k][j]);
	}
	a = b;
}
void operator *=(vector<ll> &dp, mat &a) {
	vector<ll> dpn(B, 1e18);
	for (int i : bitset) {
		for (int j : bitset) {
			dpn[j] = min(dpn[j], dp[i] + a[i][j]);
		}
	}
	swap(dp, dpn);
}
void jump(ll to) {
	a = o;
	ll jump = to - curx;
	for (;jump;pow2(a), jump >>= 1) {
		if (jump & 1) dp *= a;
	}
	curx = to;
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> x >> k >> n >> q;
	B = 1 << k;
	dp.resize(B, 1e18);
	o.resize(B, vector<ll>(B, 1e18));
	for (int b = 1;b < B;b++)
		if (__builtin_popcount(b) == x) bitset.push_back(b);
	dp[bitset[0]] = 0;
	for (int i = 1;i <= k;i++) cin >> c[i];
	for (int cur : bitset) {
		int next = cur >> 1;
		if (cur & 1) {
			for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
				o[cur][next | b] = c[i];
			}
		}
		else o[cur][next] = 0;
	}
	for (int i = 1;i <= q;i++) {
		cin >> p >> w;
		if (w) mp[p] = w;
	}
	mp[n + k - x + 1] = 0;
	curx = k;
	for (auto it = mp.begin();curx < n + k - x;) {
		if (it->first <= curx - k) {
			it++; continue;
		}
		if (it->first <= curx + 1) {
			vector<ll> dpn(B, 1e18);
			curx++;
			for (int cur : bitset) {
				int next = cur >> 1;
				if (cur & 1) {
					for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
						dpn[next | b] = min(dpn[next | b], dp[cur] + c[i] + (mp.count(curx - k + i) ? mp[curx - k + i] : 0));
					}
				}
				else dpn[next] = min(dpn[next], dp[cur]);
			}
			swap(dp, dpn);
		}
		else jump(it->first - 1);
	}
	printf("%lld", dp[bitset[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], on[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);
	memset(com, 0, sizeof com);
	int c = 1;
	for(auto x : top){
		if(!mk[x]) dfs(x, x);
	}

	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(com[x] != x) continue;
		if(on[com[neg(x)]]) continue;
		on[x] = 1;
	}
	/*
	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 << on[com[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, x);
	}

	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<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)
#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)
#define mp(x,y) make_pair((x),(y))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
inline int read(){
    register int x=0,f=1;register char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}
    return f?x:-x;
}

const int N=2e5+5,M=2e6+5,mod=1e9+7;
int n,p[N],num[M],cnt[M],a[N];
inline int power(int b,int n){int ans=1;for(;n;n>>=1,b=1ll*b*b%mod)if(n&1)ans=1ll*ans*b%mod;return ans;}

int insert(int p){
	if(!num[p])return num[p]=cnt[p]=1,0;
	--p;
	for(int i=2;i<=sqrt(p);++i){
		if(p%i)continue;
		int c=0;
		while(p%i==0)p/=i,++c;
		if(num[i]<c)num[i]=c,cnt[i]=1;
		else if(num[i]==c)++cnt[i];
	}
	if(p^1){
		int c=1;
		if(num[p]<c)num[p]=c,cnt[p]=1;
		else if(num[p]==c)++cnt[p];
	}
	return 1;
}

bool check(int x){
	for(int i=2;i<=sqrt(x);++i){
		if(x%i)continue;
		int c=0;
		while(x%i==0)x/=i,++c;
		if(c==num[i]&&cnt[i]==1)return 0;
	}
	if(x^1&&num[x]==1&&cnt[x]==1)return 0;
	return 1;
}

int main(){
	//fruitea
	n=read();
	REP(i,1,n)p[i]=read();
	sort(p+1,p+n+1,[&](int a,int b){return a>b;});
	REP(i,1,n)a[i]=p[i]-insert(p[i]);
	int ans=1;
	REP(i,2,M-1)ans=1ll*ans*power(i,num[i])%mod;
	REP(i,1,n)if(check(a[i])){ans=(ans+1)%mod;break;}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#define REP(i,n) for(int i=0;i<n;i++)
#define PB push_back
using namespace std;
const int MAX_N = 100+10;
typedef long long int64;
int64 dp[MAX_N][2][2]={};//i,first,last
string s;
int n;

string my;

map<string,int64> mp[MAX_N];

string trans(string a,bool cyc=true){
	int n=a.size();
	static bool swp[MAX_N]={};
	for(int i=0;i<a.size();i++){
		if(a[i]=='A'&&a[(i+1)%n]=='B')
			swp[i]=true;
		else
			swp[i]=false;
	}
	if(!cyc)swp[n-1]=false;
	REP(i,a.size())if(swp[i])
		swap(a[i],a[(i+1)%n]);
	return a;
}

int64 rec(int at){
	//check it
	if(at>2){
		string t=trans(my.substr(0,at),false);
		if(t.size()>2 && t.substr(1,t.size()-2) != s.substr(1,t.size()-2))
			return 0;
	}

	string key=my.substr(0,min(3,at));
	key+=" "+my.substr(max(0,at-3),3);
	if(mp[at].count(key))
		return mp[at][key];
	int64&ret=mp[at][key]=0;
	if(at==n){
		string t=trans(my);
		return ret=s==t;
	}
	my[at]='A';
	ret+=rec(at+1);
	my[at]='B';
	ret+=rec(at+1);
	return ret;
}

int main(){
	cin>>s;
	n=s.size();
	my=s;
	cout<<rec(0)<<endl;
	return 0;
}