#include<bits/stdc++.h>
using namespace std;
int a,s,d[200002],f[200002][2],g,h,j,k,l,i,n,m,mx=1000000007;
struct tre{
    pair<int,int> val;
    tre *L;
    tre *R;
    tre(){
        val={mx,0};
        L=NULL;
        R=NULL;
    }
};
void add(tre *&it,int l,int r,int idx,pair<int,int> val){
    if(l>idx || r<=idx) return;
    if(!it) it=new tre();
    if(l+1==r) {it->val=val;return;}
    it->val=min(it->val,val);
    add(it->L,l,(l+r)/2,idx,val);
    add(it->R,(l+r)/2,r,idx,val);
}

pair<int,int> get(tre *&it,int l,int r,int lf){
    if(!it || it->val.first==mx || r<=lf) return {mx,0};
    if(l+1==r) {return it->val;}
    pair<int,int> r1=get(it->L,l,(l+r)/2,lf);
    if(r1!=mx) return r1;
    pair<int,int> r2=get(it->R,(l+r)/2,r,lf);
    return r2;
}

int gpar(int idx){
    if(d[idx]==idx) return idx;
    else return d[idx]=gpar(d[idx]);
}
tre *root;
main(){
    cin>>n;
    for(i=1;i<n;i++)d[i]=i;
    l=0;
    for(i=1;i<=n;i++){
        scanf("%d%d",&a,&s);
        f[i][0]=a;
        f[i][1]=s;
        l++;
        while(1){
            k=get(root,0,mx,a).second;
            if(!k || f[k][0]>s) break;
            l--;//cout<<k<<"*"<<endl;
            k=gpar(k);
            a=f[k][1]+1;
            f[i][0]=min(f[i][0],f[k][0]);
            f[i][1]=max(f[i][1],f[k][1]);
            d[k]=i;
        }//cout<<f[i][1]<<"R";
        add(root,0,mx,f[i][1],{f[i][0],i});
        printf("%d\n",l);
    }
}


#include <bits/stdc++.h>
#define itn int
#define pll pair<ll, ll>
#define pii pair<int, int>
#define full(a) a.begin(), a.end()
#define lb(a, n) lower_bound(a.begin(), a.end(), n)
#define ub(a, n) upper_bound(a.begin(), a.end(), n)
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
const ll mod7 = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int N = 1e5 + 5;
const int len = 500;
///////////////////////////////////////////////////

void solver()
{
    bool a, b, c, d; cin >> a >> b >> c >> d;

    cout << max(2 * (a && b && c && d), !a || !b || !c || !d) << '\n';
}
/*

*/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc; cin >> tc; while(tc--)
        solver();

    return 0;
}


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

void solve()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(a==0&&b==0&&c==0&&d==0)
        cout<<"0"<<endl;
    else if(a==1&&b==1&&c==1&&d==1)
        cout<<"2"<<end;
    else
        cout<<"1"<<endl;
}

int main()
{
    int n,t,sum=0;
    cin>>t;

    while(t--)
        solve();

    return 0;
}


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

int main() {
  long long int t,i,j,k,a,b,c,d,cnt;
  cin>>t;
  while(t--){
      cnt=0;
      for(i=0;i<4;i++){
          cin>>a;
      if(a==1){
          cnt++;
      }
      }
      if(cnt==4){
          cout<<2<<"\n";
      }
     else if(cnt==3){
          cout<<1<<"\n";
      }
      else if(cnt==2){
          cout<<1<"\n";
      }
      lse if(cnt==1){
          cout<<1<<"\n";
      }
      else{
          cout<<0<<"\n";
      }

  }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    int count_ = 0;
    while(n--){
        int arr[2];
        int arr_[2];
        for(int i=0;i<2;i++){
            cin >> arr[i];
        }
        for(int i=0;i<2;i++){
            cin >> arr_[i];
        }
        for(int i=0;i<2;i++){
            if(arr[i]==1){
                count_++;
            }
        }
        for(int i=0;i<2;i++){
            if(arr_[i]==1){
                count_++;
            }
        }
        if(count_==0){
            cout << 0 << endl;
        }
        else if(count_<=3){
            cout << 1 << endl;
        }
        else{
            cout << 2 << endl;
        }
        count = 0;

    }
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#pragma comment(linker, "/STACK:200000000")
#include <limits>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define mset(mas,val) memset(mas,val,sizeof(mas))
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define fornd(i,n) for (int i=int(n)-1; i>=0; --i)
#define forab(i,a,b) for (int i=int(a); i<int(b); ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int INF = (int) 1e9;
const long long INF64 = (long long) 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;

const int N = 100;
int n, m;
int a[N][N];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

struct edge {
	int to,	f, cap, cost, rev;
};

vector < edge > g[N * N];
int prio[N * N], curflow[N * N], prevedge[N * N], prevnode[N * N], q[N * N], pot[N * N], inqueue[N * N];

void add_edge(int s, int t, int cap, int cost) {
	edge fi = {t, 0, cap, cost, sz(g[t])};
	edge se = {s, 0, 0, -cost, sz(g[s])};
	g[s].push_back(fi);
	g[t].push_back(se);
}
int NN = N * N;
int mincost() {
	int flow = 0, maxflow = (n * m) >> 1;
	int answer = 0;
	int s = 0;
	int t = n * m + 2;
	while (flow < maxflow) {
		priority_queue< ll, vector <ll>, greater<ll> > q;
		q.push(s);
		fill(prio, prio + NN, INT_MAX);
		prio[s] = 0;
		curflow[s] = INT_MAX;
		while (!q.empty()) {
			ll cur = q.top();
			int d = cur >> 32;
			int u = cur;
			q.pop();
			if (d != prio[u]) continue;
			forn(i, sz(g[u])) {
				edge &e = g[u][i];
				int v = e.to;
				if (e.cap <= e.f) continue;
				int nprio = prio[u] + e.cost + pot[u] - pot[v];
				if (prio[v] > nprio) {
					prio[v] = nprio;
					q.push(((ll) nprio << 32) + v);
					prevnode[v]  = u;
					prevedge[v] = i;
					curflow[v] = min(curflow[u], e.cap - e.f);
				}
			}
		}
		if (prio[t] == INT_MAX)
			break;
		for (int i = 0; i < NN; i++)
			pot[i] += prio[i];
		int df = min(curflow[t], maxflow - flow);
			flow += df;
		for (int v = t; v != s; v = prevnode[v]) {
		  edge &e = g[prevnode[v]][prevedge[v]];
		  e.f += df;
		  g[v][e.rev].f -= df;
		  answer += df * e.cost;
		}
	}
	return answer;
}

inline bool read()
{
	if (scanf("%d %d", &n, &m) != 2) return false;
	forn(i,n) {
		forn(j, m) {
			assert(scanf("%d", &a[i][j]) == 1);
		}
	}
	return true;
}

void solve()
{
	forn(i,n) {
		forn(j,m) {
			if (((i + j) & 1) == 0) {
				forn(cur, 4) {
					int nx = i + dx[cur];
					int ny = j + dy[cur];
					if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
						add_edge(i * m + j + 1, nx * m + ny + 1, 1, (a[i][j] != a[nx][ny]));
					}
				}
				add_edge(0, i * m + j + 1, 1, 0);
			} else {
				add_edge(i * m + j + 1, n * m + 2, 1, 0);
			}
		}
	}
	cout << mincost() << endl;
}

int main()
{
	while (read()) solve();
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const int Maxm=12*6405,Maxn=6405;
const int dirx[4]={-1,1,0,0};
const int diry[4]={0,0,-1,1};

int Cou;
int v[Maxm],next[Maxm],root[Maxm],val[Maxm],c[Maxm],q[50*Maxn],from[Maxn],edge[Maxn],dis[Maxn];
bool yes[Maxn],vis[85][85];
int S,T,Ans,Sum,N,M,l[Maxn];
int a[85][85],b[85][85];

void insert(int father,int son,int cap,int cost)
{
    Cou++;
    v[Cou]=son;
    next[Cou]=root[father];
    c[Cou]=cap;
    val[Cou]=cost;
    root[father]=Cou;
}

bool SPFA()
{
    for (int i=S; i<=T; i++) dis[i]=1<<30;
    dis[S]=0;
    int tail=1;
    q[1]=S;
    memset(yes,true,sizeof(yes));
    yes[S]=false;
    for (int head=1; head<=tail; head++)
    {
      for (int x=root[q[head]]; x!=-1; x=next[x])
        if (c[x]>0)
        {
                   if (dis[q[head]]+val[x]<dis[v[x]])
                   {
                      from[v[x]]=q[head];
                      edge[v[x]]=x;
                      dis[v[x]]=dis[q[head]]+val[x];
                      if (yes[v[x]])
                      {
                                    yes[v[x]]=false;
                                    q[++tail]=v[x];
                      }
                   }
        }
      yes[q[head]]=true;
    }
    if (dis[T]==(1<<30)) return false;
    int Minf=1<<30;
    for (int x=T; x!=S; x=from[x])
        if (c[edge[x]]<Minf) Minf=c[edge[x]];
    for (int x=T; x!=S; x=from[x])
    {
        c[edge[x]]-=Minf;
        c[edge[x]^1]+=Minf;
        Ans+=Minf*val[edge[x]];
    }
    Sum+=Minf;
    return true;
}

int main()
{
    scanf("%d%d",&N,&M);
    memset(yes,false,sizeof(yes));
    for (int i=1; i<=N; i++)
      for (int j=1; j<=M; j++) scanf("%d",&a[i][j]);
    int k1=0, k2=0;
    for (int i=1; i<=N; i++)
      for (int j=1; j<=M; j++)
        if ((i+j)&1) b[i][j]=++k1;
        else b[i][j]=++k2;
    memset(root,255,sizeof(root));
    S=0;
    Cou=-1;
    for (int i=1; i<=N; i++)
      for (int j=1; j<=M; j++)
        if ((i+j)&1)
          for (int k=0; k<4; k++)
          {
              int i1=i+dirx[k], j1=j+diry[k];
              if (a[i1][j1]!=0)
              {
                        int tmp;
                        if (a[i][j]==a[i1][j1]) tmp=0;
                        else tmp=1;
                        insert(b[i][j],k1+b[i1][j1],1,tmp);
                        insert(k1+b[i1][j1],b[i][j],0,-tmp);
              }
          }
    for (int i=1; i<=k1; i++)
    {
        insert(S,i,1,0);
        insert(i,S,0,0);
    }
    T=N*M+1;
    for (int i=1; i<=k2; i++)
    {
        insert(k1+i,T,1,0);
        insert(T,k1+i,0,0);
    }
    Ans=0;
    for (; SPFA(); )
      T=N*M+1;
    printf("%d\n",Ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <ctime>
#include <cmath>

using namespace std;

const int nmax = 80 * 80 / 2;
const int Nm = nmax * 2 + 2;
const int Mm = 2 * nmax * 4;
const int INF = 1000000000;

class costflow {
    int s[Nm], q[Nm], pre[Nm], v[Nm]; // Ns
    int t[Mm], next[Mm], cf[Mm]; // Ms
    int w[Mm], d[Nm];
    int cnt;
    int N;

public:

    void init(int n) {
        cnt = 1;
        N = n;
        memset(s, 0, sizeof (s));
        memset(d, 0, sizeof (d));
        memset(q, 0, sizeof (q));
        memset(pre, 0, sizeof (pre));
        memset(v, 0, sizeof (v));

        memset(t, 0, sizeof (t));
        memset(cf, 0, sizeof (cf));
        memset(w, 0, sizeof (w));
        memset(next, 0, sizeof (next));
    }

    void add(int ss, int tt, int cc, int ww) {
        ++cnt;
        t[cnt] = tt;
        cf[cnt] = cc;
        w[cnt] = ww;
        next[cnt] = s[ss];
        s[ss] = cnt;
        ++cnt;
        t[cnt] = ss;
        cf[cnt] = 0;
        w[cnt] = -ww;
        next[cnt] = s[tt];
        s[tt] = cnt;
    }

    pair<int, int> getflow(int S, int T) {
        int cost = 0;
        int flow = 0;
        int i, j, head, tail;
        while (true) {
            head = 0;
            tail = 1;
            for (i = 0; i < N; i++) {
                d[i] = INF;
            }
            for (i = 0; i < N; i++) {
                v[i] = 0;
            }
            d[S] = 0;
            q[1] = S;
            while (head != tail) {
                v[i = q[head = (head + 1) % N]] = 0;
                for (j = s[i]; j != 0; j = next[j]) {
                    if (cf[j] != 0 && d[t[j]] > d[i] + w[j]) {
                        d[t[j]] = d[i] + w[j];
                        pre[t[j]] = j;
                        if (v[t[j]] == 0) {
                            v[t[j]] = 1;
                            q[tail = (tail + 1) % N] = t[j];
                        }
                    }
                }
            }
            if (d[T] == INF) {
                return make_pair(flow, cost);
            }
            cost += d[T];
            ++flow;
            for (i = T; i != S; i = t[pre[i] ^ 1]) {
                --cf[pre[i]];
                ++cf[pre[i] ^ 1];
            }
        }
    }
} network;

int a[80][80];

int main()
{
	//freopen("counter.in", "r", stdin);
	//freopen("counter.out", "w", stdout);

	string s;
	int i, j, k, n, m;

	cin >> n >> m;
	for(i = 0; i < n; i++)
		for(j = 0; j < m; j++)
			cin >> a[i][j];

	const int di[4] = {-1, 0, 1, 0};
	const int dj[4] = {0, 1, 0, -1};
	network.init(2 + n * m);
	for(i = 0; i < n; i++)
		for(j = 0; j < m; j++)
			if ((i + j) % 2 == 0)
			{
				for(int d = 0; d < 4; d++)
				{
					int ii = i + di[d];
					int jj = j + dj[d];
					if (ii < 0 || ii >= n || jj < 0 || jj >= m)
						continue;
					//cout << i << " " << j << " - " << ii << " " << jj << ": " << (a[i][j] != a[ii][jj]) << endl;
					network.add(i * m + j, ii * m + jj, 1, (a[i][j] != a[ii][jj]));
				}
				network.add(n * m, i * m + j, 1, 0);
			} else
				network.add(i * m + j, n * m + 1, 1, 0);
	cout << network.getflow(n * m, n * m + 1).second << endl;

	return 0;
}


#include <iostream>
#include <map>
#include <stdio.h>
#include <set>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <sstream>
#include <list>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
/**
 * @author: Shamir14 -- Amirhossein Shapoori
 * lordamir14@gmail.com
 * ACM team: AnotherChorMangz
 * Tehran, Iran
 * Still listening to Mark Knopfler...
 * :P
 */
 #define pb push_back

 typedef long long lli;
 const int maxn = 64 + 10, MOD = 1000 * 1000 * 1000 + 7;
 int n, m;
 int ori[maxn][maxn], vis[maxn][maxn], board[maxn][maxn];
 int ax[] = {1, 0, -1, 0}, ay[] = {0, -1, 0, 1};

 bool inrange(int x, int y){
        if(x >= 0 && x < m && y >= 0 && y < n)
            return true;
        return false;
}

 int Check(){
    int ans = n * m / 2, i, j, x, y;
    for(i = 0; i < maxn; i++)
        for(j = 0; j < maxn; j++)
            vis[i][j] = 0;
//  cerr << endl;
//   for(i = 0; i < n; i++){
//       for(j = 0; j < m; j++)
//          cerr << ori[j][i] << " ";
//       cerr << endl;
//   }
//    cerr << ans << endl;
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            if(!vis[i][j]){
                x = i + ax[ori[i][j]];
                y = j + ay[ori[i][j]];
                vis[i][j] = vis[x][y] = 1;
                if(board[i][j] == board[x][y])
            //        cerr << i << ", " << j << "    ";
                    ans--;
                //if(ans == 9){
                  //  cerr << i << ", " << j << "  ||  " << x << ", " << y << "      ";
                   // break;
               // }
            }
//    if (ans == 9){
//      cerr << endl;
//   for(i = 0; i < n; i++){
//       for(j = 0; j < m; j++)
//          cerr << ori[j][i] << " ";
//       cerr << endl;
//   }
//    }
    return ans;
 }

 int mini;

 void Make(int i, int j){
        int x, y;
        if(ori[i][j] == -1){
            for(int k = 0; k < 4; k++){
                x = i + ax[k];
                y = j + ay[k];
                if(inrange(x, y) && ori[x][y] == -1){
                    ori[i][j] = k;
                    ori[x][y] = (k + 2) % 4;
                    if(i + 1 < m)
                        Make(i + 1, j);
                    else
                        if(j + 1 < n)
                            Make(0, j + 1);
                        else
                            mini = min(Check(), mini);
                    if(mini == 0)
                        break;
                    ori[i][j] = ori[x][y] = -1;
                }
            }
        }else{
            if(i + 1 < m)
                Make(i + 1, j);
            else
                if(j + 1 < n)
                    Make(0, j + 1);
                else
                    mini = min(Check(), mini);
            if(mini == 0)
                break;
        }
 }

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    mini = 1000000;
    int i, j;
    cin >> n >> m;
    for(i = 0; i < maxn; i++)
        for(j = 0; j < maxn; j++)
            ori[i][j] = -1;
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            cin >> board[j][i];

    Make(0, 0);

    cout << mini << endl;

    return 0;
}


#include <bits/stdc++.h>
#define NN 300100
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int p[NN];
INT dp[NN][2];
int vst[NN];
vector<int> ans;
int N;
int val, flag = 1;
void calc(int n){
	if(dp[n][0] == val){
		if(n > 2 && min(dp[n - 2][0], dp[n - 2][1]) == val - max(p[n], p[n - 1])){
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
			if(n != N) {
				if(!vst[n]) {
					int V = min(p[n + 1], p[n]);
					if(V) ans.push_back(n);
					vst[n] = 1;
					val -= V;
					if(!val) return ;
					p[n + 1] -= V; p[n] -= V;
				}
			}
			if(!vst[n - 2]){
				int V = min(p[n - 2], p[n - 1]);
				if(V) ans.push_back(n - 2);
				vst[n - 2] = 1;
				val -= V;
				p[n - 2] -= V; p[n - 1] -= V;
			}
			flag = 2;
			return ;
		}
		if(n > 1) {
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
		}
		if(n != N) {
			if(!vst[n]){
				int V = min(p[n + 1], p[n]);
				if(V)ans.push_back(n);
				vst[n] = 1;
				val -= V;
				p[n + 1] -= V; p[n] -= V;
			}
		}
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) dp[i][j] = inf;
	dp[1][0] = p[1], dp[1][1] = 0;
	for(int i = 2; i <= n; i++){
		dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + p[i];
		if(i > 2) dp[i][0] = min(dp[i][0], min(dp[i - 2][0], dp[i - 2][1]) + max(p[i], p[i - 1]));
		dp[i][1] = dp[i - 1][0];
	}
	val = min(dp[n][1], dp[n][0]);
//	for(int i = 1; i <= n; i++) cout << dp[i][0]<<" "<<dp[i][1]<<endl;
	N = n;
	while(val >= 0) {
//		cout << val << " "<< n <<endl;
		calc(n);
		n -= flag;
		flag = 1;
	}
	cout << ans.size() <<endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
#define NN 300100
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int p[NN];
INT dp[NN][2];
int vst[NN];
vector<int> ans;
int N;
int val, flag = 1;
void calc(int n){
	if(dp[n][0] == val){
		if(n >= 2 && min(dp[n - 2][0], dp[n - 2][1]) == val - max(p[n], p[n - 1])){
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
			if(n != N) {
				if(!vst[n]) {
					int V = min(p[n + 1], p[n]);
					if(V) ans.push_back(n);
					vst[n] = 1;
					val -= V;
					if(!val) return ;
					p[n + 1] -= V; p[n] -= V;
				}
			}
			if(!vst[n - 2]){
				int V = min(p[n - 2], p[n - 1]);
				if(V) ans.push_back(n - 2);
				vst[n - 2] = 1;
				val -= V;
				p[n - 2] -= V; p[n - 1] -= V;
			}
			flag = 2;
			return ;
		}
		if(n > 1) {
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
		}
		if(n != N) {
			if(!vst[n]){
				int V = min(p[n + 1], p[n]);
				if(V)ans.push_back(n);
				vst[n] = 1;
				val -= V;
				p[n + 1] -= V; p[n] -= V;
			}
		}
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) dp[i][j] = inf;
	dp[1][0] = p[1], dp[1][1] = 0;
	for(int i = 2; i <= n; i++){
		dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + p[i];
		if(i >= 2) dp[i][0] = min(dp[i][0], min(dp[i - 2][0], dp[i - 2][1]) + max(p[i], p[i - 1]));
		dp[i][1] = dp[i - 1][0];
	}
	val = min(dp[n][1], dp[n][0]);
//	for(int i = 1; i <= n; i++) cout << dp[i][0]<<" "<<dp[i][1]<<endl;
	N = n;
	while(val) {
//		cout << val << " "<< n <<endl;
		calc(n);
		n -= flag;
		flag = 1;
	}
	cout << ans.size() <<endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
map<int,ll> dp[N];
map<int,PII> pre[N],way[N];
int n,a[N];
PII cur;
VI ret,ret2;
void upd(int p,int v,ll cost,PII ww) {
	if (!dp[p].count(v)||cost<dp[p][v]) {
		dp[p][v]=cost;
		way[p][v]=ww;
		pre[p][v]=cur;
	}
}
void dfs(int n,int v) {
	if (n==1) return;
	if (way[n][v].fi!=-1) ret.pb(way[n][v].fi);
	if (way[n][v].se!=-1) ret.pb(way[n][v].se);
	dfs(pre[n][v].fi,pre[n][v].se);
}
int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",a+i);
	rep(i,1,n+1) dp[i].clear(),pre[i].clear(),way[i].clear();
	dp[1][a[1]]=0;
	rep(i,1,n) {
		ll mv=1ll<<60;
		for (auto p:dp[i]) {
			int v=p.fi; ll w=p.se;
			mv=w;
			cur=mp(i,v);
			if (v==0) upd(i+1,a[i+1],w,mp(-1,-1));
			upd(i+1,max(a[i+1]-v,0),w+min(v,a[i+1]),mp(i,-1));
			if (i+2<=n&&a[i+1]<=a[i+2]) upd(i+2,a[i+2]-a[i+1],w+a[i+1],mp(i+1,-1));
			if (i+2<=n&&a[i+1]>=a[i+2]) upd(i+2,0,w+a[i+2]+min(v,(a[i+1]-a[i+2])),mp(i,i+1));
		}
	}
	int mv=-1; ll w=1ll<<60;
	for (auto p:dp[n]) if (p.se<w) w=p.se,mv=p.fi;
	dfs(n,mv);
	reverse(all(ret));
	for (auto p:ret) {
		if (a[p]>0&&a[p+1]>0) {
			int z=min(a[p],a[p+1]);
			a[p]-=z; a[p+1]-=z;
			ret2.pb(p);
		}
	}
	printf("%d\n",SZ(ret2));
	for (auto p:ret2) printf("%d ",p);
	puts("");
}


//安利少歌
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 3e4+10;
int n;
vector<int>e[N];

int cnt,p[N],fa[N],vis[N];
bool flag[N];
inline void dfs(int u){
	vis[u]=1;
	for (auto v:e[u]){
		if (v==fa[u]) continue;
		fa[v]=u;
		if (!vis[v]) dfs(v);
		else {
			for (int now=u;now!=v;now=fa[now]) flag[p[++cnt]=now]=1;
			flag[p[++cnt]=v]=1;
		}
		if (cnt) break;
	}
}

ll f[N],g[N],size[N];
inline void dfs(int u,int fa){
	size[u]=1;
	for (auto v:e[u]){
		if (v==fa||flag[v]) continue;
		dfs(v,u),f[u]+=g[v],size[u]+=size[v];
	}
	g[u]=f[u]+size[u];
}

ll dp[N],last[N],pre[N],ans;
inline void dfs(int u,int fa,ll sum){
	ans=max(ans,sum+n+f[u]);
	for (auto v:e[u]) if (v!=fa) dfs(v,u,sum+n-size[v]+f[u]-g[v]);
}
inline void solve(){
	ll sum=0;
	For(i,1,cnt*2) last[i]=size[p[i]],pre[i]=pre[i-1]+last[i],sum+=f[p[i]];
	For(i,2,cnt-1){
		For(l,1,cnt*2-i+1){
			int r=l+i-1;
			ll tmp=pre[r]-pre[l-1];
			dp[l]=max(last[l]+tmp,last[l+1]+tmp);
		}
		For(j,1,cnt*2-i+1) last[j]=dp[j];
	}
//	For(i,1,cnt) printf("%d ",dp[i+1]);puts("");
//	For(i,1,cnt) printf("%d ",f[p[i]]);puts("");
	ans=0;
	For(i,1,cnt){
		ans=max(ans,n+sum/2+dp[i+1]);
		for (auto v:e[p[i]]) if (!flag[v]) dfs(v,p[i],n-size[p[i]]+1+sum/2-f[p[i]]+dp[i+1]);
	}
	printf("%lld\n",ans);
}

int main(){
	n=read();
	For(i,1,n){
		int x=read()+1,y=read()+1;
		e[x].pb(y),e[y].pb(x);
	}
	dfs(1);
	For(i,1,cnt) dfs(p[i],0);
//	For(i,1,cnt) printf("%d ",p[i]);puts("");
	For(i,1,cnt) p[cnt+i]=p[i];
	solve();
}
/*
10
0 1
0 3
0 4
0 9
1 2
2 3
2 7
4 5
4 6
7 8
*/


#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

const int maxn = 15005;

int n;

struct Edge {
	int to, nxt;
} e[maxn];

int first[maxn];

inline void add_edge(int from, int to) {
	static int cnt = 0;
	e[++cnt].nxt = first[from];
	first[from] = cnt;
	e[cnt].to = to;
	e[++cnt].nxt = first[to];
	first[to] = cnt;
	e[cnt].to = from;
}

LL dpw[maxn];
LL dpn[maxn];
int siz[maxn];
int huan[maxn];
int hs;
stack<int> st;
bool vis[maxn];
bool oh[maxn];

inline bool dfsh(int now, int fa) {
	vis[now] = true;
	st.push(now);
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (to != fa) {
			if (vis[to]) {
				while(st.top() != to) {
					huan[++hs] = st.top();
					st.pop();
				}
				huan[++hs] = to;
				st.pop();
				return true;
			}
			if (dfsh(to, now)) {
				return true;
			}
		}
	}
	st.pop();
	return false;
}

inline void dfss(int now, int fa) {
	siz[now] = 1;
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfss(to, now);
			siz[now] += siz[to];
		}
	}
}

inline void dfs1(int now, int fa) {
	dpn[now] = siz[now];
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfs1(to, now);
			dpn[now] += dpn[to];
		}
	}
}

LL dp[maxn][maxn];
LL xx[maxn];
LL qzh[maxn];

inline LL sum(int l, int r) {
	if (l <= r) {
		return qzh[r] - qzh[l - 1];
	} else {
		swap(l, r);
		l++, r--;
		return qzh[hs] + qzh[l - 1] - qzh[r];
	}
}

inline int pre(int x) {
	return (x - 2 + hs) % hs + 1;
}

inline int nxt(int x) {
	return x % hs + 1;
}

inline void dfsall(int now, int fa) {
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dpw[to] = dpw[now] + dpn[now] - dpn[to] - siz[to];
			dfsall(to, now);
		}
	}
}

int main() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		int f, t;
		read(f), read(t);
		f++, t++;
		add_edge(f, t);
	}
	dfsh(1, 0);
//	cout << "huan = ";
	for (int i = 1; i <= hs; ++i) {
//		cout << huan[i] << ' ';
		oh[huan[i]] = true;
	}
//	cout << endl;
	for (int i = 1; i <= hs; ++i) {
		dfss(huan[i], 0);
		dfs1(huan[i], 0);
	}
//	cout << "dpn = ";
//	for (int i = 1; i <= n; ++i) {
//		cout << dpn[i] << ' ';
//	}
//	cout << endl;
	for (int i = 1; i <= hs; ++i) {
		xx[i] = dpn[huan[i]];
		qzh[i] = qzh[i - 1] + xx[i];
	}
	for (int i = 1; i <= hs; ++i) {
		dp[i][i] = xx[i];
	}
	for (int len = 2; len < hs; ++len) {
		for (int l = 1; l <= hs; ++l) {
			int r = (l + len - 2) % hs + 1;
			dp[l][r] = max(dp[nxt(l)][r], dp[l][pre(r)]) + sum(l, r);
//			cout << l << ' ' << r << ' ' << dp[l][r] << ' ' << sum(l, r) << ' ' << nxt(l) << ' ' << pre(r) << endl;
		}
	}
//	cout << "xx = ";
	for (int i = 1; i <= hs; ++i) {
		dpw[huan[i]] = dp[nxt(i)][pre(i)];
		dfsall(huan[i], 0);
//		cout << dpw[huan[i]] << ' ';
	}
//	cout << endl;
	LL ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans = max(ans, dpw[i] + dpn[i] + n - siz[i]);
	}
	printf("%lld", ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

const int maxn = 15005;

int n;

struct Edge {
	int to, nxt;
} e[maxn << 1];

int first[maxn];

inline void add_edge(int from, int to) {
	static int cnt = 0;
	e[++cnt].nxt = first[from];
	first[from] = cnt;
	e[cnt].to = to;
	e[++cnt].nxt = first[to];
	first[to] = cnt;
	e[cnt].to = from;
}

LL dpw[maxn];
LL dpn[maxn];
int siz[maxn];
int huan[maxn];
int hs;
stack<int> st;
bool vis[maxn];
bool oh[maxn];

inline bool dfsh(int now, int fa) {
	vis[now] = true;
	st.push(now);
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (to != fa) {
			if (vis[to]) {
				while(st.top() != to) {
					huan[++hs] = st.top();
					st.pop();
				}
				huan[++hs] = to;
				st.pop();
				return true;
			}
			if (dfsh(to, now)) {
				return true;
			}
		}
	}
	st.pop();
	return false;
}

inline void dfss(int now, int fa) {
	siz[now] = 1;
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfss(to, now);
			siz[now] += siz[to];
		}
	}
}

inline void dfs1(int now, int fa) {
	dpn[now] = siz[now];
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfs1(to, now);
			dpn[now] += dpn[to];
		}
	}
}

LL dp[maxn][maxn];
LL xx[maxn];
LL qzh[maxn];

inline LL sum(int l, int r) {
	if (l <= r) {
		return qzh[r] - qzh[l - 1];
	} else {
		swap(l, r);
		l++, r--;
		return qzh[hs] + qzh[l - 1] - qzh[r];
	}
}

inline int pre(int x) {
	return (x - 2 + hs) % hs + 1;
}

inline int nxt(int x) {
	return x % hs + 1;
}

inline void dfsall(int now, int fa) {
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dpw[to] = dpw[now] + dpn[now] - dpn[to] - siz[to];
			dfsall(to, now);
		}
	}
}

int main() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		int f, t;
		read(f), read(t);
		f++, t++;
		add_edge(f, t);
	}
	dfsh(1, 0);
//	cout << "huan = ";
	for (int i = 1; i <= hs; ++i) {
//		cout << huan[i] << ' ';
		oh[huan[i]] = true;
	}
//	cout << endl;
	for (int i = 1; i <= hs; ++i) {
		dfss(huan[i], 0);
		dfs1(huan[i], 0);
	}
//	cout << "dpn = ";
//	for (int i = 1; i <= n; ++i) {
//		cout << dpn[i] << ' ';
//	}
//	cout << endl;
	for (int i = 1; i <= hs; ++i) {
		xx[i] = dpn[huan[i]];
		qzh[i] = qzh[i - 1] + xx[i];
	}
	for (int i = 1; i <= hs; ++i) {
		dp[i][i] = xx[i];
	}
	for (int len = 2; len < hs; ++len) {
		for (int l = 1; l <= hs; ++l) {
			int r = (l + len - 2) % hs + 1;
			dp[l][r] = max(dp[nxt(l)][r], dp[l][pre(r)]) + sum(l, r);
//			cout << l << ' ' << r << ' ' << dp[l][r] << ' ' << sum(l, r) << ' ' << nxt(l) << ' ' << pre(r) << endl;
		}
	}
//	cout << "xx = ";
	for (int i = 1; i <= hs; ++i) {
		dpw[huan[i]] = dp[nxt(i)][pre(i)];
		dfsall(huan[i], 0);
//		cout << dpw[huan[i]] << ' ';
	}
//	cout << endl;
	LL ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans = max(ans, dpw[i] + dpn[i] + n - siz[i]);
	}
	printf("%lld", ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

const int maxn = 15005;

int n;

struct Edge {
	int to, nxt;
} e[maxn];

int first[maxn];

inline void add_edge(int from, int to) {
	static int cnt = 0;
	e[++cnt].nxt = first[from];
	first[from] = cnt;
	e[cnt].to = to;
	e[++cnt].nxt = first[to];
	first[to] = cnt;
	e[cnt].to = from;
}

LL dpw[maxn];
LL dpn[maxn];
int siz[maxn];
int huan[maxn];
int hs;
stack<int> st;
bool vis[maxn];
bool oh[maxn];

inline bool dfsh(int now, int fa) {
	st.push(now);
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (to != fa) {
			if (vis[to]) {
				while(st.top() != to) {
					huan[++hs] = st.top();
					st.pop();
				}
				huan[++hs] = to;
				st.pop();
				return true;
			}
			if (dfsh(to, now)) {
				return true;
			}
		}
	}
	return false;
}

inline void dfss(int now, int fa) {
	siz[now] = 1;
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfss(to, now);
			siz[now] += siz[to];
		}
	}
}

inline void dfs1(int now, int fa) {
	dpn[now] = siz[now];
	for (int i = first[now]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!oh[to] && to != fa) {
			dfs1(to, now);
			dpn[now] += dpn[to];
		}
	}
}

LL dp[maxn][maxn];
LL xx[maxn];
LL qzh[maxn];

inline LL sum(int l, int r) {
	if (l <= r) {
		return qzh[r] - qzh[l - 1];
	} else {
		swap(l, r);
		l++, r--;
		return qzh[hs] + qzh[l - 1] - qzh[r];
	}
}

inline int nxt(int x) {
	return (x - 2 + hs) % hs + 1;
}

inline int pre(int x) {
	return x % hs + 1;
}

int main() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		read(f), read(t);
		f++, t++;
		add_edge(f, t);
	}
	dfsh(1, 0);
	for (int i = 1; i <= hs; ++i) {
		oh[huan[i]] = true;
	}
	dfs1(1, 0);
	for (int i = 1; i <= hs; ++i) {
		xx[i] = dpn[huan[i]];
		qzh[i] = qzh[i - 1] + xx[i];
	}
	for (int i = 1; i <= hs; ++i) {
		dp[i][i] = xx[i];
	}
	for (int l = 1; l <= hs; ++l) {
		for (int r = nxt(l); l != r; r = nxt(r)) {
			dp[l][r] = max(dp[nxt(l)][r], dp[l][pre(r)]) + sum(l, r);
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<windows.h>
using namespace std;
const int N=50005,L=0,R=1;
struct node;
extern node *nil,*t[N];
struct node
{
	long long v,size,size_sqr,lazy_size,sum;
	node *s[2],*f,*next,*rf;
	node(): v(0),size(0),size_sqr(0),lazy_size(0),sum(0),
			f(nil),next(nil),rf(nil) {s[L]=s[R]=nil;}
	bool root() { return f->s[L]!=this && f->s[R]!=this; }
	int get_dir() { return f->s[R]==this; }
	long long addsize(long long d)
	{
		if(this==nil)
			return 0;
		lazy_size+=d;
		size+=d;
		if(rf!=nil)
		{
			long long tmp=size-d;
			rf->size_sqr+=-tmp*tmp + size*size;
		}
		long long tmp=sum,dv=0;
		node *now=this;
		for(now=this ; now->s[L]!=nil ; now=now->s[L]) ;
		dv-=now->rf->v;
		for(now=this ; now->s[R]!=nil ; now=now->s[R]) ;
		dv+=now->v;
		sum+=dv*d;
		return 2*d*tmp;
	}
	long long changev(long long d)
	{
		long long delta=(d-v)*(size*size-size_sqr);
		node *tmp=s[R];
		push();
		for(tmp=s[R]; tmp->s[L]!=nil; tmp=tmp->s[L])
			tmp->push();
		sum+=(d-v)*(size-tmp->size);
		long long tmpv=(d-v)*(-tmp->size);
		for(tmp=s[R]; tmp!=nil; tmp=tmp->s[L])
			tmp->sum+=tmpv;
		v=d;
		return delta;
	}
	void push()
	{
		if(lazy_size)
		{
			s[L]->addsize(lazy_size);
			s[R]->addsize(lazy_size);
			lazy_size=0;
		}
	}
	void update()
	{
		sum=s[L]->sum+s[R]->sum+size*(v-rf->v);
	}
	void set_f(node *d)
	{
		if(this!=nil)
			f=d;
	}
	void set_right(node *k)
	{
		push();
		for(node *tmp=s[R]; tmp!=nil; tmp=tmp->s[L])
			tmp->push();
		(s[R]=k)->set_f(this);
		update();
	}
}u[N]={},*t[N]={},*nil=&u[0];
int n,v[N]={},f[N]={};
long long ans=0;
vector<int> son[N];
int dfs(int s)
{
	for(vector<int>::iterator it=son[s].begin();it!=son[s].end();++it)
	{
		long long tmp=dfs(*it);
		t[s]->size+=tmp;
		t[s]->size_sqr+=tmp*tmp;
	}
	t[s]->size+=1;
	t[s]->v=v[s];
	t[s]->f=t[s]->rf=t[f[s]];
	ans+=(t[s]->size*t[s]->size - t[s]->size_sqr)*t[s]->v;
	t[s]->sum+=(t[s]->v - t[s]->rf->v) * t[s]->size;
	return t[s]->size;
}
void rotate(node *n)
{
	node *f=n->f;
	int d=n->get_dir();
	f->push();
	n->push();
	f->s[d]=n->s[!d];
	f->s[d]->set_f(f);
	if(!f->root())
		f->f->s[f->get_dir()]=n;
	n->f=f->f;
	n->s[!d]=f;
	f->f=n;
	f->update();
	n->update();
}
void Splay(node *n)
{
	while(!n->root())
		if(n->f->root())
			rotate(n);
		else
			if(n->get_dir() ^ n->f->get_dir())
				rotate(n),rotate(n);
			else
				rotate(n->f),rotate(n);
}
node* Access(node *x)
{
	node *y=nil;
	for(;x!=nil;y=x,x=x->f)
	{
		Splay(x);
		x->set_right(y);
	}
	return y;
}
void init()
{
	scanf("%d",&n);
	for(int i=0;i<=n;++i)
		t[i]=&u[i];
	for(int i=2;i<=n;++i)
	{
		scanf("%d",&f[i]);
		son[f[i]].push_back(i);
	}
	for(int i=1;i<=n;++i)
		scanf("%d",&v[i]);
}
void all_push()
{
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			t[j]->push();
}
void set_f(int x,int y)
{
	node *p=t[x],*q=t[y],*r=t[f[x]];
	long long delta=p->size;
	node *tmp=Access(r);
	ans+=tmp->addsize(-delta) + 2*r->v*delta*delta;
	all_push();
	r->size_sqr-=delta*delta;
	Splay(p);
	p->rf=p->f=q;
	f[x]=y;
	p->update();
	tmp=Access(q);
	ans+=tmp->addsize(delta);
	all_push();
	q->size_sqr+=delta*delta;
}
void work()
{
	int q,x,y;
	char ch;
	cin>>q;
	/*
			for(int j=1;j<=n;++j)
				cout<<f[j]<<' ';
			cout<<endl;
			for(int j=1;j<=n;++j)
				cout<<t[j]->size<<' ';
			cout<<endl;
			for(int j=1;j<=n;++j)
				cout<<t[j]->size_sqr<<' ';
			cout<<endl;
	*/
	printf("%.15f\n",ans*1.0/n/n);
	for(int i=1;i<=q;++i)
	{
		cin>>ch>>x>>y;
		if(ch=='P')
		{
			node *p=Access(t[y]),*q=t[x];
			while(!q->root())
				q=q->f;
			p!=q ? set_f(x,y) : set_f(y,x);
		}
		if(ch=='V')
		{
			Splay(t[x]);
			ans+=t[x]->changev(y);
		}
		//cout<<ch<<' '<<x<<' '<<y<<endl;
		printf("%.15f\n",ans*1.0/n/n);
		/*if(ch=='V')
		{
			for(int j=1;j<=n;++j)
				cout<<f[j]<<' ';
			cout<<endl;
			for(int j=1;j<=n;++j)
				cout<<t[j]->size<<' ';
			cout<<endl;
			for(int j=1;j<=n;++j)
				cout<<t[j]->size_sqr<<' ';
			cout<<endl;
		}*/
	}
}
int main()
{
	//freopen("now.txt","w",stdout);
	init();
	dfs(1);
	work();
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int N=50005,L=0,R=1;
struct node;
extern node *nil;
struct node
{
	long long v,size,size_sqr,lazy_size,sum;
	node *s[2],*f,*next,*rf;
	node(): v(0),size(0),size_sqr(0),lazy_size(0),sum(0),
			f(nil),next(nil),rf(nil) {s[L]=s[R]=nil;}
	bool root() { return f->s[L]!=this && f->s[R]!=this; }
	int get_dir() { return f->s[R]==this; }
	long long addsize(long long d)
	{
		if(this==nil)
			return 0;
		lazy_size+=d;
		size+=d;
		if(rf!=nil)
		{
			long long tmp=size-d;
			rf->size_sqr+=-tmp*tmp + size*size;
		}
		long long tmp=sum,dv=0;
		node *now=this;
		for(now=this ; now->s[L]!=nil ; now=now->s[L]) ;
		dv-=now->rf->v;
		for(now=this ; now->s[R]!=nil ; now=now->s[R]) ;
		dv+=now->v;
		sum+=dv*d;
		return 2*d*tmp;
	}
	long long changev(long long d)
	{
		long long delta=(d-v)*(size*size-size_sqr);
		node *tmp=s[R];
		push();
		for(tmp=s[R]; tmp->s[L]!=nil; tmp=tmp->s[L])
			tmp->push();
		sum+=(d-v)*(size-tmp->size);
		long long tmpv=(d-v)*(-tmp->size);
		for(tmp=s[R]; tmp!=nil; tmp=tmp->s[L])
			tmp->sum+=tmpv;
		v=d;
		return delta;
	}
	void push()
	{
		if(lazy_size)
		{
			s[L]->addsize(lazy_size);
			s[R]->addsize(lazy_size);
			lazy_size=0;
		}
	}
	void update()
	{
		sum=s[L]->sum+s[R]->sum+size*(v-rf->v);
	}
	void set_f(node *d)
	{
		if(this!=nil)
			f=d;
	}
	void set_right(node *k)
	{
		push();
		for(node *tmp=s[R]; tmp!=nil; tmp=tmp->s[L])
			tmp->push();
		(s[R]=k)->set_f(this);
		update();
	}
}u[N]={},*t[N]={},*nil=&u[0];
int n,v[N]={},f[N]={};
long long ans=0;
vector<int> son[N];
int dfs(int s)
{
	for(vector<int>::iterator it=son[s].begin();it!=son[s].end();++it)
	{
		long long tmp=dfs(*it);
		t[s]->size+=tmp;
		t[s]->size_sqr+=tmp*tmp;
	}
	t[s]->size+=1;
	t[s]->v=v[s];
	t[s]->f=t[s]->rf=t[f[s]];
	ans+=(t[s]->size*t[s]->size - t[s]->size_sqr)*t[s]->v;
	t[s]->sum+=(t[s]->v - t[s]->rf->v) * t[s]->size;
	return t[s]->size;
}
void rotate(node *n)
{
	node *f=n->f;
	int d=n->get_dir();
	f->push();
	n->push();
	f->s[d]=n->s[!d];
	f->s[d]->set_f(f);
	if(!f->root())
		f->f->s[f->get_dir()]=n;
	n->f=f->f;
	n->s[!d]=f;
	f->f=n;
	f->update();
	n->update();
}
void Splay(node *n)
{
	while(!n->root())
		if(n->f->root())
			rotate(n);
		else
			if(n->get_dir() ^ n->f->get_dir())
				rotate(n),rotate(n);
			else
				rotate(n->f),rotate(n);
}
node* Access(node *x)
{
	node *y=nil;
	for(;x!=nil;y=x,x=x->f)
	{
		Splay(x);
		x->set_right(y);
	}
	return y;
}
void init()
{
	scanf("%d",&n);
	for(int i=0;i<=n;++i)
		t[i]=&u[i];
	for(int i=2;i<=n;++i)
	{
		scanf("%d",&f[i]);
		son[f[i]].push_back(i);
	}
	for(int i=1;i<=n;++i)
		scanf("%d",&v[i]);
}
void set_f(int x,int y)
{
	node *p=t[x],*q=t[y],*r=t[f[x]];
	long long delta=p->size;
	node *tmp=Access(r);
	ans+=tmp->addsize(-delta) + 2*r->v*delta*delta;
	r->size_sqr-=delta*delta;
	Splay(p);
	p->rf=p->f=q;
	f[x]=y;
	p->update();
	tmp=Access(q);
	ans+=tmp->addsize(delta);
	q->size_sqr+=delta*delta;
}
void work()
{
	int q,x,y;
	char ch;
	cin>>q;
	printf("%.15f\n",ans*1.0/n/n);
	for(int i=1;i<=q;++i)
	{
		cin>>ch>>x>>y;
		if(ch=='P')
		{
			node *p=Access(t[y]),*q=t[x];
			while(!q->root())
				q=q->f;
			p!=q ? set_f(x,y) : set_f(y,x);
		}
		if(ch=='V')
		{
			Splay(t[x]);
			ans+=t[x]->changev(y);
		}
		printf("%.15f\n",ans*1.0/n/n);
	}
}
int main()
{
	//freopen("now.txt","w",stdout);
	init();
	dfs(1);
	work();
	return 0;
}


?#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define M 100010
#define LL long long
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define ULL unsigned int
#define ls son[p][0]
#define rs son[p][1]
LL ans;
int son[M][2], fa[M], cnt[M], val[M];
int is[M];
LL sumVal[M], sumCnt[M], sum[M], all[M], lazy[M];
void up( int p )
{
	sumCnt[p] = sumCnt[ls] + sumCnt[rs] + cnt[p];
	sumVal[p] = sumVal[ls] + sumVal[rs] + val[p];
	sum[p] = sum[ls] + sum[rs] + 1LL * cnt[p] * val[p];
}
void func( int p, LL v )
{
	if( !p ) return;
	all[p] += v * cnt[p];
	lazy[p] += v;
}
void down( int p )
{
	if( lazy[p] ){
		func( ls, lazy[p] );
		func( rs, lazy[p] );
		lazy[p] = 0;
	}
}
void Rotate( int x )
{
	int y = fa[x], z = fa[y];
	int o = son[y][0] == x;
	down( y ), down( x );
	son[y][!o] = son[x][o], fa[son[x][o]] = y;
	son[x][o] = y, fa[y] = x;
	fa[x] = z;
	if( is[y] ) is[y] = 0, is[x] = 1;
	else
		son[z][son[z][1]==y] = x;
	up( y );
}
void Splay( int x )
{
	int y, z;
	down( x );
	while( !is[x] ){
		y = fa[x], z = fa[y];
		if( is[y] ) Rotate( x );
		else if( ( son[y][0] == x ) ^ ( son[z][0] == y ) )
			Rotate( x ), Rotate( x );
		else
			Rotate( y ), Rotate( x );
	}
	up( x );
}
int Access( int x )
{
	int y = 0;
	for( ; x; x = fa[x] ){
		Splay( x );
		is[ son[x][1] ] = 1;
		if( son[x][1] ){
			cnt[x] += sumCnt[son[x][1]];
		}
		son[x][1] = y;
		if( y ){
			cnt[x] -= sumCnt[y];
		}
		is[y] = 0;
		y = x;
		up( x );
	}
	return y;
}
int vv[M], nxt[M], h[M], e;
void add( int u, int v )
{
	vv[e] = v, nxt[e] = h[u], h[u] = e++;
}
void dfs( int u, int f )
{
	fa[u] = f, is[u] = 1, cnt[u] = 1; son[u][0] = son[u][1] = 0;
	all[u] = 1;
	for( int i = h[u]; i + 1; i = nxt[i] ){
		int v = vv[i];
		dfs( v, u );
		all[u] += 1LL * cnt[v] * cnt[u] * 2;
		cnt[u] += cnt[v];
	}
	ans += all[u] * val[u];
	up( u );
}
void change( int x, LL v )
{
	Splay( x );
	ans += ( v - val[x] ) * all[x];
	val[x] = v;
	up( x );
}
int getFa( int x )
{
	Splay( x );
	if( !son[x][0] ) return fa[x];
	int y = son[x][0];
	while( son[y][1] ) y = son[y][1];
	return y;
}

void gao( int u, int v )
{
	Access( u ); Splay( v );
	if( !fa[v] ) swap( u, v );
	int f = getFa( v );
	if( f == u ) return;

	Access( f ), Splay( f );
	cnt[f] -= sumCnt[v], up( f );
	fa[v] = 0;
	ans -= 2*sum[f] * sumCnt[v];
	func( f, -2*sumCnt[v] );

	Access( u ), Splay( u );
	ans += 2 * sum[u] * sumCnt[v];
	func( u, 2 * sumCnt[v] );
	cnt[u] += sumCnt[v], up( u );
	fa[v] = u;
}
int main()
{
	int n;
	int u;
	scanf( "%d", &n );
	memset( h, -1, sizeof(h) ), e = 0;
	for( int i = 2; i <= n; ++i ){
		scanf( "%d", &u );
		add( u, i );
	}
	for( int i = 1; i <= n; ++i ) scanf( "%d", val + i );
	dfs( 1, 0 );
	int m, v;
	scanf( "%d", &m );
	char c[3];
	double mu = 1.0 * n * n;
	printf( "%.10lf\n", ans / mu );
	while( m-- ){
		scanf( "%s%d%d", c, &u, &v );
		if( c[0] == 'P' ) gao( v, u );
		else change( u, v );
		printf( "%.10lf\n", ans / mu );
	}
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
int mod = (int) 1e9 + 7;
const int logN = 18;
const int SQ = 250;
const int inf = 1e9;
const int N = 5e4 + 5;

ll all, ch[N], path[N];
int size[N], ok[N], dp[N], n, m, P[N], p[N], x, y, z, t, val[N], C[N];
vector< pair< int , pii > > q;
vector< int > v[N], nodes;
char c;

int up(int x, int y) {
    if(!x) return 0;
    int &r = dp[x];
    if(r != -1) return r;
    if(P[x] == y) return r = x;
    return r = up(P[x], y);
}

int up2(int x) {
    if(ok[x]) return x;
    int &r = dp[x];
    if(r != -1) return r;
    return r = up2(P[x]);
}

int dfs1(int node) {
    size[node] = 1;
    int s = ok[node] ? 2 : 0;
    ch[node] = 0;
    foreach(it, v[node]) {
        s += dfs1(*it);
        size[node] += size[*it];
        ch[node] -= size[*it] * (ll) size[*it];
    } ch[node] += size[node] * (ll) size[node];
    if(s >= 2) ok[node] = 1;
    all += ch[node] * (ll) val[node];
    return s != 0;
}

void dfs2(int node, int last, int root, int bef = 0) {
    path[node] = path[root] + (size[root] - size[node]) * (ll) val[root];
    C[node] = size[bef];
    if(ok[node]) { nodes.pb(node); P[node] = last; last = node; bef = -1; }
    foreach(it, v[node]) {
        if(bef == -1) dfs2(*it, node, node, *it);
        else dfs2(*it, last, node, bef);
    }
}

int ctr(int x, int y) {
    while(x) {
        if(x == y) return 1;
        x = P[x];
    } return 0;
}

int main() {

    scanf("%d", &n);
    FOR(i, 2, n) scanf("%d", &p[i]);
    FOR(i, 1, n) scanf("%d", &val[i]);

    scanf("%d", &m);

    FOR(i, 1, m) {
        scanf(" %c %d %d", &c, &y, &z);
        if(c == 'P') { ok[y] = ok[z] = 1; q.pb(mp(0, mp(y, z))); }
        if(c == 'V') { ok[y] = 1; q.pb(mp(1, mp(y, z))); }
        if(i == m || i % SQ == 0) {
            FOR(i, 1, n) v[i].clear();
            FOR(i, 2, n) v[p[i]].pb(i);
            nodes.clear();
            ok[1] = 1; all = 0;
            dfs1(1), dfs2(1, 0, 0, 1);
            FOR(i, 1, n) oko[i] = ok[i];
            memset(ok, 0, sizeof ok);
            foreach(it, q) {
                printf("%.12lf\n", all / (n * n * 1.0));
                foreach(it3, nodes) ok[*it3] = 0;
                if(it->st == 1) {
                    int node = it->nd.st, val = it->nd.nd - ::val[node];
                    all += val * ch[it->nd.st];
                    foreach(it3, nodes) dp[*it3] = -1;
                    foreach(it3, nodes) {
                        int take = up(*it3, node);
                        if(take == 0) continue;
                        path[*it3] += val * (ll) (size[node] - C[take]);
                    } :: val[node] += val;
                }
                else {
                    if(ctr(it->nd.nd, it->nd.st)) swap(it->nd.nd, it->nd.st);
                    p[it->nd.st] = it->nd.nd;
                    int node = it->nd.st, par = P[it->nd.st], sz = size[node];
                    all -= 2 * sz * path[node];
                    while(par) {
                        ok[par] = ok[node] = 1;
                        ch[par] -= 2LL * sz * (size[par] - C[node]);
                        size[par] -= sz; C[node] -= sz;
                        node = P[node]; par = P[par];
                    } C[1] -= sz; C[it->nd.st] = sz;
                    int ggg = path[it->nd.st];
                    foreach(it3, nodes) dp[*it3] = -1;
                    foreach(it3, nodes) {
                        if(!ok[*it3] || *it3 == it->nd.st) {
                            int take = up2(*it3);
                            if(take == it->nd.st) path[*it3] -= ggg;
                            else path[*it3] -= val[take] * (ll) sz;
                        }
                    }
                    foreach(it3, nodes) dp[*it3] = -1, ok[*it3] = 0; C[it->nd.st] = 0;
                    node = it->nd.st, par = it->nd.nd, P[node] = par;
                    while(par) {
                        ok[par] = ok[node] = 1;
                        size[par] += sz; C[node] += sz;
                        ch[par] += 2LL * sz * (size[par] - C[node]);
                        node = P[node]; par = P[par];
                    } C[1] += sz;
                    foreach(it3, nodes) dp[*it3] = -1;
                    ggg = path[it->nd.nd] + (ll) (size[it->nd.nd] - sz) * val[it->nd.nd];
                    foreach(it3, nodes) {
                       if(!ok[*it3] || *it3 == it->nd.st) {
                            int take = up2(*it3);
                            if(take == it->nd.st) path[*it3] += ggg;
                            else path[*it3] += val[take] * (ll) sz;
                        }
                    }
                    all += 2 * path[it->nd.st] * size[it->nd.st];
                    foreach(it3, nodes) dp[*it3] = -1, ok[*it3] = 0;
                }
            }
            q.clear();
            memset(ok, 0, sizeof ok);
        }
    }
    printf("%.12lf\n", all / (n * n * 1.0));
    return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const i64 P = 998244353;
const int maxn = 2100000;

i64 fact[maxn], tcaf[maxn];

i64 deg(i64 x, i64 d) {
    if (d < 0) d += P - 1;
    i64 y = 1;
    while (d) {
        if (d & 1) (y *= x) %= P;
        d /= 2;
        (x *= x) %= P;
    }
    return y;
}

i64 cnk(int n, int k) {
    return fact[n] * tcaf[k] % P * tcaf[n - k] % P;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    int n, m;
    cin >> n >> m;
    fact[0] = 1;
    forn(i, n) fact[i + 1] = fact[i] * (i + 1) % P;
    forn(i, n + 1) tcaf[i] = deg(fact[i], -1);
    i64 p = deg(m + 1, -1), q = (P + 1 - p) % P;
    i64 ans = 0;
    for1(k, n) {
        (ans += cnk(n, k) % P * (1 + m * deg(k, -1)) * (k % 2 ? 1 : -1)) %= P;
//        assert(t != 1);
    }
    if (ans < 0) ans += P;
//    cout << ans << '\n';
    (ans *= n * p % P + 1) %= P;
    cout << ans << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).resize(unique(all(V))-(V).begin())
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

const ll MOD = 998244353;
const ll MD = 998244351;

ll inv(ll n) {
	ll r=1;
	for (register int i=30; i>=0; i--) {
		r=r*r%MOD;
		if ((1ll<<i)&(MOD-2)) r=r*n%MOD;
	}
	return r;
}

int N, K;
ll I[4000005];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin>>N>>K;
	ll S=0, T=1, U=1;

	for (register i=1; i<=N+K; i++) {
		ll r=1;
		for (register int j=29; j>=0; j--) {
			r=r*r%MOD;
			if ((1ll<<i)&MD) r=r*i%MOD;
		}
		I[i]=r;
	}
	for (int i=1; i<=K-1; i++) T=T*I[i]%MOD, U=U*i%MOD;
	ll SS=(N*I[K+1]%MOD+1+MOD)%MOD;
	for (int i=1; i<=N; i++) {
		T=T*i%MOD*I[i+K]%MOD;
		U=U*(i+K-1)%MOD*I[i]%MOD;
		ll X=(i+K)*I[i]%MOD*(SS+T*S%MOD)%MOD;
		if (i==N) cout<<X<<'\n';
		S=(S+U*X)%MOD;
	}
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).resize(unique(all(V))-(V).begin())
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

const ll MOD = 998244353;
const ll MD = 998244351;
int N, K;
ll I[4000005];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin>>N>>K;
	ll S=0, T=1, U=1;

	I[1]=1;
	for (int i=2; i<=N+K; i++) I[i]=(MOD-(MOD/i)*I[MOD%I]%MOD)%MOD;

	for (int i=1; i<=K-1; i++) T=T*I[i]%MOD, U=U*i%MOD;
	ll SS=(N*I[K+1]%MOD+1+MOD)%MOD;
	for (int i=1; i<=N; i++) {
		T=T*i%MOD*I[i+K]%MOD;
		U=U*(i+K-1)%MOD*I[i]%MOD;
		ll X=(i+K)*I[i]%MOD*(SS+T*S%MOD)%MOD;
		if (i==N) cout<<X<<'\n';
		S=(S+U*X)%MOD;
	}
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).resize(unique(all(V))-(V).begin())
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

const ll MOD = 998244353;
const ll MD = 998244351;
int N, K;
ll I[4000005];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin>>N>>K;
	ll S=0, T=1, U=1;

	I[1]=1;
	for (int i=2; i<=N+K; i++) I[i]=(MOD-(MOD/i)*I[MOD%I]%MOD)%MOD;
	for (register int i=1; i<=N+K; i++) {
		ll r=1;
		for (register int j=29; j>=0; j--) {
			r=r*r%MOD;
			if ((1ll<<j)&MD) r=r*i%MOD;
		}
		I[i]=r;
	}
	for (int i=1; i<=K-1; i++) T=T*I[i]%MOD, U=U*i%MOD;
	ll SS=(N*I[K+1]%MOD+1+MOD)%MOD;
	for (int i=1; i<=N; i++) {
		T=T*i%MOD*I[i+K]%MOD;
		U=U*(i+K-1)%MOD*I[i]%MOD;
		ll X=(i+K)*I[i]%MOD*(SS+T*S%MOD)%MOD;
		if (i==N) cout<<X<<'\n';
		S=(S+U*X)%MOD;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[25],b[25];

#define INF 1e15
typedef long long ll;

ll memo[50];
int STATE,n,m;
ll dp[50][25];

ll ret[45][45];
ll adj[45][45];
void mult(ll m1[45][45],ll m2[45][45]){
	ll temp[45][45];
	for(int i=0;i<=40;i++)
	   for(int j=0;j<=40;j++){
		   temp[i][j]=INF;

		   for(int k=0;k<40;k++)
		       temp[i][j] = min (temp[i][j], min(INF,m1[i][k]+m2[k][j]));
	   }

   for(int i=0;i<=40;i++)
	   for(int j=0;j<=40;j++)
			ret[i][j] = temp[i][j];

}

void exp(int k){

    if(k==1){
	   for(int i=0;i<=40;i++)
	      for(int j=0;j<=40;j++)
			ret[i][j] = adj[i][j];
	   return;
   }

	exp(k/2);

	mult(ret,ret);
	if(k&1)mult(ret,adj);

}

ll solve(int state,int idx){
	if(dp[state+20][idx]!=-1)return dp[state+20][idx];

	if(idx==n){
		if(state == STATE)return 0;
		else return INF;
	}
	return dp[state+20][idx] = min(solve(state+1,idx+1)+a[idx],solve(state-1,idx+1)+b[idx]);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(int i=0;i<n;i++)
		scanf("%d",&b[i]);

	for(int state = -20;state<=20;state++){
		STATE = state;
		memset(dp,-1,sizeof dp);
		memo[state+20] = solve(0,0);

	}
	for(int i=0;i<40;i++)for(int j=0;j<40;j++)adj[i][j]=INF;

	for(int state1 = 0;state1<=40;state1++){
		for(int state2 = state1-20;state2<=state1+20 && state2<=40;state2++){
            if(state2<0)continue;
			int trans = state2-state1;
			adj[state1][state2] = memo[trans+20];
			//if(memo[trans+20]<INF)cout<<state1<<"  "<<state2<<"  "<<memo[trans+20]<<endl;
		}
	}

	exp(m);

	//cout<<ret[0][0]<<"\n";
printf("%I64d\n",ret[0][0]);
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int Max = 5e5 + 5, Mod = 998244353;

ll n, k, a[22], b[22], dp[22][22][22];
int OP, CL;
ll Try(int i, int op, int cl)
{
	if (op < 0)
		cl++, op = 0;
	if (i == n)
		return (op == OP && cl == CL ? 0 : 1e16);
	ll& ret = dp[i][op][cl];
	if (ret + 1)
		return ret;
	ll c1 = Try(i + 1, op + 1, cl) + a[i];
	ll c2 = Try(i + 1, op - 1, cl) + b[i];
	return ret = min({ (ll)1e16,c1,c2 });
}

typedef vector<vector<ll>> matrix;
int m;

matrix init(ll x)
{
	return matrix(m, vector<ll>(m, x));
}

matrix mult(matrix& a, matrix& b)
{
	matrix Res = init(1e15);
	for (int i = 0; i < m; i++)
		for (int k = 0; k < m; k++)
			for (int j = 0; j < m; j++)
				Res[i][j] = min(Res[i][j], a[i][k] + b[k][j]);
	return Res;
}

matrix fp(matrix& x, ll p)
{
	if ( p== 1 )
		return x;
	matrix y = fp(x, p >> 1);
	y = mult(y, y);
	if (p & 1)
		y = mult(y, x);
	return y;
}

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];
	if (n == 1)
	{
		a[n] = a[0];
		b[n] = b[0];
		n++;
		k /= 2;
	}
	vector<pair<int, int>> v;
	for (int i = 0; i < n; i++)
		for (int j = 0; i + j < n; j++)
			if ((n - (i + j)) % 2 == 0)
				v.push_back({ i,j });
	m = 2 * n;
	matrix T = init(1e16);
	for(int i=0;i<m;i++)
		for (int j = 0; j < v.size(); j++)
		{
			OP = v[j].first;
			CL = v[j].second;
			int jj = i - CL + OP;
			if (i-CL>=0 && jj< m && jj >= 0 )
			{
				memset(dp, -1, sizeof(dp));
				T[i][jj] = Try(0, 0, 0);
			}
		}
	T = fp(T, k);
	cout << T[0][0] << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int Max = 5e5 + 5, Mod = 998244353;
ll n, k, a[22], b[22], dp[22][22][22];
int OP, CL;
ll Try(int i, int op, int cl)
{
	if (op < 0)
		cl++, op = 0;
	if (i == n)
		return (op == OP && cl == CL ? 0 : 1e16);
	ll& ret = dp[i][op][cl];
	if (ret + 1)
		return ret;
	ll c1 = Try(i + 1, op + 1, cl) + a[i];
	ll c2 = Try(i + 1, op - 1, cl) + b[i];
	return ret = min({ (ll)1e16,c1,c2 });
}

typedef vector<vector<ll>> matrix;
int m;

matrix init(ll x)
{
	return matrix(m, vector<ll>(m, x));
}

matrix mult(matrix& a, matrix& b)
{
	matrix Res = init(1e15);
	for (int i = 0; i < m; i++)
		for (int k = 0; k < m; k++)
			for (int j = 0; j < m; j++)
				Res[i][j] = min(Res[i][j], a[i][k] + b[k][j]);
	return Res;
}

matrix fp(matrix& x, ll p)
{
	if ( p== 1 )
		return x;
	matrix y = fp(x, p >> 1);
	y = mult(y, y);
	if (p & 1)
		y = mult(y, x);
	return y;
}

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];
	if (n == 1)
	{
		a[n] = a[0];
		b[n] = b[0];
		n++;
		k /= 2;
	}
	vector<pair<int, int>> v;
	for (int i = 0;  i <= n; i++)
		for (int j = 0; i+j<=n j++)
			if ((n - (i + j)) % 2 == 0)
				v.push_back({ i,j });
	m = 2 * n;
	matrix T = init(1e16);
	for(int i=0;i<m;i++)
		for (int j = 0; j < v.size(); j++)
		{
			OP = v[j].first;
			CL = v[j].second;
			int jj = i - CL + OP;
			if (i-CL>=0 && jj< m && jj >= 0 )
			{
				memset(dp, -1, sizeof(dp));
				T[i][jj] = Try(0, 0, 0);
			}
		}
	T = fp(T, k);
	cout << T[0][0] << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int Max = 5e5 + 5, Mod = 998244353;
ll n, k, a[22], b[22], dp[22][22][22];
int OP, CL;
ll Try(int i, int op, int cl)
{
	if (op < 0)
		cl++, op = 0;
	if (i == n)
		return (op == OP && cl == CL ? 0 : 1e16);
	ll& ret = dp[i][op][cl];
	if (ret + 1)
		return ret;
	ll c1 = Try(i + 1, op + 1, cl) + a[i];
	ll c2 = Try(i + 1, op - 1, cl) + b[i];
	return ret = min({ (ll)1e16,c1,c2 });
}
typedef vector<vector<ll>> matrix;
int m;
matrix init(ll x)
{
	return matrix(m, vector<ll>(m, x));
}
matrix mult(matrix& a, matrix& b)
{
	matrix Res = init(1e15);
	for (int i = 0; i < m; i++)
		for (int k = 0; k < m; k++)
			for (int j = 0; j < m; j++)
				Res[i][j] = min(Res[i][j], a[i][k] + b[k][j]);
	return Res;

matrix fp(matrix& x, ll p)
{
	if ( p== 1 )
		return x;
	matrix y = fp(x, p >> 1);
	y = mult(y, y);
	if (p & 1)
		y = mult(y, x);
	return y;
}
int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];
	if (n == 1)
	{
		a[n] = a[0];
		b[n] = b[0];
		n++;
		k /= 2;
	}
	vector<pair<int, int>> v;
	for (int i = 0;  i <= n; i++)
		for (int j = 0; i+j<=n;j++)
			if ((n - (i + j)) % 2 == 0)
				v.push_back({ i,j });
	m = 2 * n+1;
	matrix T = init(1e16);
	for(int i=0;i<m;i++)
		for (int j = 0; j < v.size(); j++)
		{
			OP = v[j].first;
			CL = v[j].second;
			int jj = i - CL + OP;
			if (i-CL>=0 && jj< m && jj >= 0 )
			{
				memset(dp, -1, sizeof(dp));
				T[i][jj] = Try(0, 0, 0);
			}
		}
	T = fp(T, k);
	cout << T[0][0] << endl;
	return 0;
}


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.*;
import java.math.BigDecimal;
import java.math.MathContext;

public class Main {
	public static BigDecimal[] a = new BigDecimal[100000];
	public static boolean[] flag = new boolean[100000];
	public static MathContext mc = new MathContext(50);
	public static final BigDecimal eps = BigDecimal.ONE.movePointLeft(25);
	public static final BigDecimal neps = BigDecimal.ZERO.subtract(eps);
	public static void main(String args[]) {
		Scanner cin = new Scanner (new BufferedInputStream(System.in));
		PrintStream cout = System.out;
		int n=cin.nextInt();
		String s = cin.next();
		for(int i=0; i<n; i++) {
			a[i]=BigDecimal.valueOf(s.charAt(i)-'0');
			flag[i]=false;
		}
		for(int i=2; i<n; i++) {
			if(flag[i] == true) continue;
			for(int j=i; j<n; j+=i) flag[j]=true;
			if((n%i) != 0) continue;
			for(int r=0; r<(n/i); r++) {
				BigDecimal sum=BigDecimal.ZERO;
				for(int j=r; j<n; j+=(n/i)) sum=sum.add(a[j], mc);
				sum=sum.divide(BigDecimal.valueOf(i), mc);
				for(int j=r; j<n; j+=(n/i)) a[j]=a[j].subtract(sum, mc);
			}
		}
		boolean bad = false;
		for(int i=0; i<n; i++) {
			if(a[i].compareTo(eps) == 1 || neps.compareTo(a[i]) == 1) {
				bad=true;
				break;
			}
		}
		if(bad) cout.println("NO");
		else cout.println("YES");
	}
}


/**
 *    author:  tourist
 *    created: 16.09.2017 22:07:38
**/
#include <bits/stdc++.h>

using namespace std;

int solve(vector<long long> a) {
  int n = a.size();
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (n % (i * i) != 0) {
        for (int j = 0; j < n; j += i) {
          long long delta = a[j];
          if (delta)
            int k = j;
            do {
              a[k] -= delta;
              k = (k + n / i) % n;
            } while (k != j);
            break;
          }
        }
      }
      vector< vector<long long> > w(i);
      for (int j = 0; j < n; j++) {
        w[j % i].push_back(a[j]);
      }
      bool ok = true;
      for (int j = 0; j < i; j++) {
        if (!solve(w[j])) {
          ok = false;
          break;
        }
      }
      return ok;
    }
  }
  for (int i = 1; i < (int) a.size(); i++) {
    if (a[i] != a[0]) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    a[i] = s[i] - '0';
  }
  puts(solve(a) ? "YES" : "NO");
  return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

const int N=101000;
int n,m;
double dig[N];
double pd[N];
char s[N];
VI pr;
int main() {
	scanf("%d",&n); m=n;
	scanf("%s",s);
	rep(i,0,m) dig[i]=s[i]-'0', ss += dig[i];
	for (int i=2;i<m;i++) if (n%i==0) {
		while (n%i==0) n/=i;pr.pb(i);
	}
	pr.pb(m);
	for (auto p:pr) {
		for (int i=0;i<m/p;i++) {
			int pdn = 0;
			for (int j=i;j<m;j+=m/p) pd[pdn++]=dig[j];
			sort(pd, pd + pdn);
			double s = pdn/2 + 1 < pdn ? (pd[pdn/2] + pd[pdn/2+1])/2 : pd[pdn/2];
			for (int j=i;j<m;j+=m/p) dig[j]-=s;
		}
	}
	rep(i,0,m) {
		if (fabs(dig[i])>=1e-9) { puts("NO"); return 0;}
	}
	puts("YES");
}


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

int N;
vector<int> V;
vector<pair<int,int>> P;

bool solve(vector<int>& v, int pi)
{
    //cerr << "!";
    int n = v.size();
    if (n == 1)
        return v[0] == 0;
    int p = P[pi].first;
    int pp = P[pi].second;
    for (int i = 0; i < pp/p; i++)
    {
        vector<vector<int>> split;
        for (int j = 0; j < p; j++)
        {
            vector<int> pgon;
            for (int k = 0; k < n/pp; k++)
            {
                pgon.push_back(v[(i+j*n/p+k*pp)%n]);
            }
            split.push_back(pgon);
        }
        for (int j = 1; j < split.size(); j++)
        {
            for (int k = 0; k < split[j].size(); k++)
            {
                split[j][k] -= split[0][k];
            }
        }
        /*for (int j = 2; j < split.size(); j++)
        {
            for (int k = 1; k < split[j].size(); k++)
            {
                if (split[j][k] - split[1][k] != split[j][0]-split[1][0])
                {
                    if (N==3960)
                    {
                        cout << pi << " " << p << " " << i << " " << j << " " << k << " not the same stuff" << endl;
                    }
                    return false;
                }

            }
        }*/
        for (int j = 1; j < split.size(); j++)
        {
            if (!solve(split[j], pi+1))
            {
                if (N==3960)
                {
                    cout << "Got back false at " << pi << " " << p << " " << i << endl;
                }
                return false;
            }
        }
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        char c;
        cin >> c;
        V.push_back(c-'0');
    }
    int p = 2;
    int nn = N;
    while(nn > 1)
    {
        //cerr << "p " << p << ", nn " << nn << endl;
        //cerr << "?";
        if (nn % p != 0)
        {
            p++;
            continue;
        }
        int c = 1;
        while(nn % p == 0)
        {
            nn /= p;
            c *= p;
        }
        P.push_back({p, c});
        p++;
    }
    bool r = solve(V, 0);
    cout << (r ? "YES" : "NO") << endl;
}


#include <stdio.h>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
#include <unordered_map>
#include <queue>
#include <array>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;
const int mod = 1e9 + 7;

ll mpow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n /= 2;
  }
  return res;
}

int main() {
  //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  std::ios::sync_with_stdio(false); std::cin.tie(0);
  int n;
  cin >> n;
  vvl p(n, vl(n));
  vi pc(1 << n);
  for (int i = 0 ; i < pc.size(); ++i) pc[i] = __builtin_popcount(i);
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    cin >> p[i][j];
    p[i][j] = p[i][j] * mpow(100, mod - 2) % mod;
  }
  map<vi, int> d;
  d[vi(1 << n)] = 1;
  ll res = 0;
  for (int i = 0; i < n; ++i) {
    map<vi, int> nd;
    for (int mask = 0; mask < (1 << n); ++mask) {
      ll prod = 1;
      for (int j = 0; j < n; ++j) {
        if (mask & (1 << j)) {
          prod = prod * p[i][j] % mod;
        } else {
          prod = prod * (1 - p[i][j]) % mod;
        }
      }
      for (const auto& it : d) {
        vi nv = it.first;
        bool fail = 0;
        for (int nmask = mask; nmask < (1 << n); nmask = (nmask + 1) | mask) {
          nv[nmask]++;
          if (nv[nmask] > pc[nmask]) {
            fail = 1;
            break;
          }
        }
        if (!fail) {
          nd[nv] = (nd[nv] + prod * it.second) % mod;
        }
      }
    }
    d.swap(nd);
  }
  for (auto& it : d) {
    res = (res + it.second) % mod;
  }
  cout << (res % mod + mod) % mod << endl;
  return 0;
}


/*
Author: QAQ Automaton
Lang: C++
Prog: E.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	inline bool read (signed &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}

	inline bool read (long long &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}
	inline bool read (char &x) {
		x=gc();
		return x!=EOF;
	}
	inline bool read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
		while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
		*x=0;
		return 1;
	}
	template<typename A,typename ...B>
	inline bool read(A &x,B &...y){
		return read(x)&&read(y...);
	}
	// print a signed integer
	inline bool write (signed x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}

	inline bool write (long long x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}
	inline bool write (char x) {
		putc(x);
		return 0;
	}
	inline bool write(const char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	inline bool write(char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	template<typename A,typename ...B>
	inline bool write(A x,B ...y){
		return write(x)||write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int inf;
struct _init_{
    _init_(){
        memset(&inf,0x3f,sizeof(inf));
    }
};
_init_ ___INIT__;
int a[11][11],n;
int f[11][131];
int is[131],i100;
const int p=1000000007;
int fpm(int a,int b){int c=1;for(;b;b>>=1,a=a*a%p)if(b&1)c=c*a%p;return c;}
typedef __int128 lll;
map<lll,int> st[8];
lll mx;
int dfs(int x,lll w){
	if(x==n)return w==mx;
	if(st[x].count(w))return st[x][w];
	int ans=0;
	lll ng=0;

	int s[n];
	for(int j=0;j<n;++j)s[j]=0;
	for(int i=0;i<1<<n;++i)if(w&((lll)1<<i))for(int j=0;j<n;++j)if(!(i&(1<<j)))
		s[j]|=((lll)1)<<(i|(1<<j));
	for(int i=0;i<1<<n;++i){
		ng=w;
		for(int j=0;j<n;++j)if(i&(1<<j))ng|=s[j];
		ans=(ans+f[x][i]*dfs(x+1,ng))%p;
	}
	return st[x][w]=ans;
}
signed main(){
#ifdef QAQAutoMaton
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	read(n);
	mx=(lll)1<<((1<<n)-1);
	mx+=mx-1;
	i100=fpm(100,p-2);
	for(int i=0;i<n;++i)for(int j=0;j<n;++j){
		read(a[i][j]);
		a[i][j]=a[i][j]*i100%p;
	}
	for(int i=0;i<n;++i)is[1<<i]=i;
	for(int i=0;i<n;++i){
		for(int j=0;j<1<<n;++j){
			f[i][j]=1;
			for(int k=0;k<n;++k)if(j&(1<<k))f[i][j]=f[i][j]*a[k][i]%p;
			else f[i][j]=f[i][j]*(p+1-a[k][i])%p;
		}
	}
	write(dfs(0,1),'\n');
	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

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

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

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

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

const int maxn = (1<<20)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

int tab[maxk][maxk];
int elo[maxk][maxk];
int maska[maxn];

LL dp[maxn][2];

int daj(bool f)
{
  for(int i = 0;i <= 6;i++)
    for(int j = 0;j <= 6;j++)
      elo[i][j] = 0;
  for(auto s : kraw)
    elo[s.se][s.fi] = 1;
  vector<VI> ziom = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};
  int mask = 0;
  for(auto s : ziom)
    for(int i = 1;i <= 6;i++)
      for(int j = i + 1;j <= 6;j++)
        for(int k = j + 1;k <= 6;k++)
        {
          if(elo[i][s[0] + f * 3] and elo[j][s[1] + f * 3] and elo[k][s[2] + f * 3])
          {
            mask |= maska[(f ? 126 : 0) ^ ((1<<i) + (1<<j) + (1<<k))];
          }
      }

  // for(int i = 0;i < SZ(kraw);i++)
  //   for(int j = i + 1;j < SZ(kraw);j++)
  //     if(kraw[i].fi != kraw[j].fi and kraw[i].se != kraw[j].se)
  //       for(int k = j + 1;k < SZ(kraw);k++)
  //         if(kraw[i].fi != kraw[k].fi and kraw[j].fi != kraw[k].fi and kraw[i].se != kraw[k].se and kraw[j].se != kraw[k].se)
  //           mask |= maska[(f ? 126 : 0) ^ ((1<<kraw[i].se) + (1<<kraw[j].se) + (1<<kraw[k].se))];
  return mask;
}

LL pot(LL a, LL w)
{
  if(!w) return 1;
  LL b = pot(a, w / 2);
  b = b * b % mod;
  if(w & 1)
    b = b * a % mod;
  return b;
}

int main()
{
	_upgrade
  int n;
  cin>>n;
  for(int i = 1;i <= n;i++)
    for(int j = 1;j <= n;j++)
      cin>>tab[i][j];
  int xd = 0;
  for(int i = 1;i <= 6;i++)
    for(int j = i + 1;j <= 6;j++)
      for(int k = j + 1;k <= 6;k++)
        maska[(1<<i) ^ (1<<j) ^ (1<<k)] = (1<<xd++);
  for(int i = n + 1;i <= 6;i++)
    tab[i][i] = 100;
  VPII kraw;
  n = 6;
  for(int i = 1;i <= n;i++)
    for(int j = 1;j <= n;j++)
      kraw.pb({i, j});
  for(int k = 0;k < 2;k++)
    for(int i = 0;i < (1<<18);i++)
    {
      // dbg(i, k);
      LL p = 1LL;
      for(int j = 0;j < 18;j++)
        if((1<<j) & i)
        {
          p = p * tab[kraw[j + k * 18].fi][kraw[j + k * 18].se] % mod * pot(100, mod - 2) % mod;
          elo[kraw[j + k * 18].se][kraw[j + k * 18].fi] = 1;
        }
        else
        {
          p = p * (100 - tab[kraw[j + k * 18].fi][kraw[j + k * 18].se]) % mod * pot(100, mod - 2) % mod;
          elo[kraw[j + k * 18].se][kraw[j + k * 18].fi] = 0;
        }
      int cur = daj(k);
      dp[cur][k] += p;
      dp[cur][k] %= mod;
    }
  LL res = 1;
  for(int i = 0;i < 20;i++)
    for(int j = 0;j < (1<<20);j++)
      if((1<<i) & j)
      {
        dp[j][0] += dp[j ^ (1<<i)][0];
        dp[j][0] %= mod;
      }
  for(int i = 0;i < (1<<20);i++)
    res = (res - dp[i][0] * dp[((1<<20) - 1) ^ i][1]) % mod;
  res += mod;
  res %= mod;
  cout<<res;
	return 0;
}


// 16
// I remember reading the editorial...
// should've already solved this by now.

#ifndef LOCAL
#define NDEBUG 1
#endif
#include<bits/stdc++.h>
#include<Modular.h>

using modular=Modular<1000000007>;

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);
	int number; std::cin>>number;
	//std::vector<std::vector<modular>> probability(number, std::vector<modular>(number));
	std::vector<modular> probability(number*number);
	//for(auto& row: probability)
	for(auto& value: probability){
		int percentage; std::cin>>percentage;
		value=modular(1)/100*percentage;
	}

	std::vector<modular> total;

	int const middle=number/2;
	auto const maskValue=[&]{
		std::array<int, 1<<6> maskValue;
		assert(number<=6);
		int count=0;
		for(int matchMask=0; matchMask<(int)maskValue.size(); ++matchMask){
			maskValue[matchMask]=__builtin_popcount(matchMask)==middle ? count++: -1;
		}
		total.resize(1<<count);
		return maskValue;
	}();

	auto const process=[&](int left, int right, auto handle){
		auto const len=right-left;
		for(int edgeMask=0; edgeMask<(1<<(len*number)); ++edgeMask){
			modular totalProbability=1;

			// inefficient
			for(int tmp=edgeMask, i=left*number; i<right*number; ++i, tmp>>=1){
				totalProbability*=tmp&1 ?  probability[i]: 1-probability[i];
				if(totalProbability==0) goto continue_outer_2;
			}

			{ // compute matchSets and call handle
				// even more inefficient...?
				std::array<char, 3> data{};
				assert(len<=(int)data.size());
				int matchSets{};
				while(true){
					// process data
					int matchMask{};
					for(int index=0; index<len; ++index){
						if(not(edgeMask>>(index*number+data[index])&1)) goto continue_outer;
						if(matchMask&1<<data[index]) goto continue_outer;
						matchMask|=1<<data[index];
					}
					if(right==number) // second half, should invert
						matchMask^=(1<<number)-1;

					matchSets|=1<<maskValue[matchMask];

continue_outer:
					// increase data
					for(int i=len-1;;){
						if(i<0) goto break_outer;
						if(data[i]==number-1){data[i]=0; --i;}
						else{++data[i]; break;}
					}
				}
break_outer:;

				handle(matchSets, totalProbability);
			}

continue_outer_2:;
		}
	};
	assert(middle<number);
	process(0, middle,[&](int sets, modular probability){
		total[sets]+=probability;
	});
	for(int dim=1; dim<(int)total.size(); dim<<=1)
		for(int i=0; i<(int)total.size(); ++i)
			if(not(i&dim))
				total[i|dim]+=total[i];

	modular result{};
	process(middle, number,[&](int sets, modular probability){
		result+=probability*(total.back()-total[(1<<number)-1-sets]);
	});
	std::cout<<(int)result<<'\n';

}


#include <iostream>

using namespace std;

long long solve(long long a,long long b,long long c)
{
 if((a==0)+(b==0)+(c==0)>=2)
  return 0;

 if(a==0)
  return solve(min(b,c),b-min(b,c),c-min(b,c))+min(b,c);
 else if(b==0)
  return solve(a-min(a,c),b+min(a,c),c-min(a,c))+min(a,c);
 else
  return solve(a-min(a,b),b-min(a,b),c+min(a,b))+min(a,b);
}

int main()
{
 long long ans=0;
 long long a,b,c;
 long long tmp;

 cin>>a>>b>>c;

 ans=solve(a-min(a,b),b-min(a,b),c+min(a,b))+min(a,b);
 ans<?=solve(a-min(a,c),b+min(a,c),c-min(a,c))+min(a,c);
 ans<?=solve(a+min(b,c),b-min(b,c),c-min(b,c))+min(b,c);

 cout<<ans<<endl;

 return 0;
}


#include<iostream>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define pb push_back
long minim,i,j,rez;
vector <long> V[5];
long a,b,c;

int main()
{
    cin>>a>>b>>c;
    V[a%2].pb(a);
    V[b%2].pb(b);
    V[c%2].pb(c);
    sort ( V[0].begin(), V[0].end() );
    sort ( V[1].begin(), V[1].end() );
    minim=INF;
    for ( i=1; i<V[0].size(); i++ )
    {
        rez=V[0][i];
        if ( rez < minim )
            minim=rez;
    }
    for ( i=1; i<V[1].size(); i++ )
    {
        rez=V[1][i];
        if ( rez < minim )
            minim=rez;
    }
    cout<<minim<<"\n";
    return 0;
}


#include<stdio.h>
typedef long long ll;
ll min(ll a,ll b)
{
if(a<b)return a;
return b;
}
int main()
{
ll a,b,c,t;
cin>>a;
cin>>b;
cin>>c;
ll ans = 0;
while(1)
{
ll sum = a+b+c;
if(sum==a)break;
if(sum==b)break;
if(sum==c)break;
if(a>b){t=a; a=b; b=t;}
if(a>c){t=a;a=c;c=t;}
if(b>c){t=b;b=c;c=b;}
ans+=b;
a+=b;
b=0;
c-=b;

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


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

int main(){
	long long a[3];
	cin>>a[0]>>a[1]>>a[2];
	if((a[0]+a[i]) % 2 == 0)
		cout<<a[1]<<endl;
	else
		cout<<a[2]<<endl;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int Matsize=85;
const int MOD=1000000007;
ll n,k,C[Matsize][Matsize];
struct Mat
{
    ll mat[Matsize][Matsize];
    void init()
    {
        for(int i=0;i<Matsize;i++)
        {
            for(int j=0;j<Matsize;j++) mat[i][j]=0;
            mat[i][i]=1;
        }
    }
    Mat operator*(const Mat &t)const
    {
        Mat ans;
        memset(ans.mat,0,sizeof(ans.mat));
        for(int k=0;k<Matsize;k++)
            for(int i=0;i<Matsize;i++)
                for(int j=0;j<Matsize;j++)
                    ans.mat[i][j]=(ans.mat[i][j]+mat[i][k]*t.mat[k][j])%MOD;
        return ans;
    }
    Mat operator+(const Mat &t)const
    {
        Mat ans;
        for(int i=0;i<Matsize;i++)
            for(int j=0;j<Matsize;j++)
                ans.mat[i][j]=(mat[i][j]+t.mat[i][j])%MOD;
        return ans;
    }
};
Mat mt,a;
Mat pow_M(ll m)
{
    Mat res;
    res.init();
    while(m)
    {
        if(m&1) res=res*mt;
        mt=mt*mt;
        m/=2;
    }
    return res;
}
ll Fp(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x%MOD;
        x=x*x%MOD;
        n/=2;
    }
    return ans;
}
int main()
{
    C[0][0]=C[1][0]=C[1][1]=1;
    for(int i=2;i<Matsize;i++)
    {
        C[i][0]=1;
        for(int j=1;j<Matsize;j++)
        {
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
        }
    }
    scanf("%I64d%I64d",&n,&k);
    if(n==1) return 0*puts("1");
    if(n==2) return 0*printf("%I64d\n",1+Fp(2,k)*2);
    for(int j=0;j<=k;j++)
    {
        for(int i=k+1;i<=j+k+1;i++)
        {
            mt.mat[i][j]=C[j][i-(k+1)];
        }
    }
    for(int j=k+1;j<2*(k+1);j++)
    {
        for(int i=0;i<=j-(k+1);i++)
        {
            mt.mat[i][j]=C[j-(k+1)][i];
        }
    }
    for(int j=k+1;j<2*(k+1);j++)
    {
        for(int i=k+1;i<=j;i++)
        {
            mt.mat[i][j]=C[j-(k+1)][i-(k+1)];
        }
    }
    for(int i=0;i<2*(k+1);i++)
    {
        mt.mat[i][2*(k+1)]=mt.mat[i][2*(k+1)-1];
    }
    mt.mat[2*(k+1)][2*(k+1)]=1;
    for(int i=0;i<k+1;i++)
    {
        a.mat[0][i]=Fp(2,i);
    }
    for(int i=k+1;i<2*(k+1);i++)
    {
        a.mat[0][i]=2*Fp(2,i-(k+1));
    }
    a.mat[0][2*(k+1)]=1+Fp(2,k)*2;
    a=a*pow_M(n-2);
    printf("%I64d\n",a.mat[0][2*(k+1)]);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<sstream>
#include<set>
#include<utility>
#include<map>
#define In() freopen("in.txt","r",stdin)
#define Out() freopen("out.txt","w",stdout)
typedef long L;
typedef long long LL;
typedef unsigned long long ULL;
const double Pi=3.1415926535898;
const LL mod=1e9+7;
const int maxn=2*1e5+5;
const int INF=0x3f3f3f3f;
const double inf=200005.0;

using namespace std;
//------------------------------------------
const int N = 90;
const int M = 50;

struct mat {
    LL a[N][N];
    mat() { memset(a, 0, sizeof(a)); }
};

int n, k;
LL s, c[M][M];

void init () {
    cin >> s >> k;
    n = k * 2 + 3;
    for (int i = 0; i <= k; i++) {
        c[i][0] = c[i][i] = 1;;
        for (int j = 1; j < i; j++)
            c[i][j] = c[i-1][j-1] + c[i-1][j];
    }
}

mat mul(mat p, mat q) {
    mat ans;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {

            for (int t = 0; t < n; t++) {
                ans.a[i][j] += p.a[i][t]*q.a[t][j];
                ans.a[i][j] %= mod;
            }
        }
    }
    return ans;
}

mat mat_pow(mat base, LL t) {
    mat x;
    for (int i = 0; i < n; i++) x.a[i][i] = 1;

    while (t) {
        if (t&1) x = mul(x, base);
        base = mul(base, base);
        t /= 2;
    }
    return x;
}

int main () {
    init();
    mat base;
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j <= i; j++) {
            base.a[i][j] = base.a[i+k+1][j] = base.a[i][j+k+1] = c[i][j];
        }
    }
    base.a[2*k+2][k] = base.a[2*k+2][2*k+2] = 1;
    mat ans = mat_pow(base, s);
    LL res;
    for (int i = 0; i < 2*k+2; i++) {
        res= ans.a[2*k+2][i]) % mod;
    }
    cout <<res<< endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector <vector <ll> > matrix;

ll mod = 1000000007;

ll mnogo = 7e18;

matrix umnm (const matrix & a, const matrix & b)
{
    matrix res(a.size(), vector <ll> (b[0].size()));
    for (int i = 0; i < a.size(); i++)
        for (int j = 0; j < b[0].size(); j++)
            for (int g = 0; g < a.size(); g++) {
                res[i][j] += (a[i][g] * b[g][j]);
                if (res[i][j] >= mnogo)
                    res[i][j] %= mod;
            }
    for (int i = 0; i < res.size(); ++i) {
        for (int j = 0; j < res[0].size(); ++j)
            if (res[i][j] >= mod)
                res[i][j] %= mod;
    return res;
}

matrix pw (matrix a, matrix & res, ll n)
{
    while (n)
    {
        if (n & 1ll)
            res = umnm (a, res);
        a = umnm (a, a);
        n >>= 1;
    }
    return res;
}

ll pw (ll x, int st)
{
    if (st < 0) return 0;
    ll res = 1;
    while (st--)
    {
        res = (res * x) % mod;
    }
    return res;
}

int main()
{

    ll C[100][100];

    C[0][0] = 1;
    for (int j = 1; j < 100; j++)
        C[0][j] = 0;

    for (int i = 1; i < 100; i++)
    {
        C[i][0] = 1;
        for (int j = 1; j < 100; j++)
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }

    ll n;
    int k;
    cin >> n >> k;

    matrix base(2 * k + 3, vector <ll> (2 * k + 3));
    //1

    for (int i = 0; i <= k; i++)
        base[i][i + k + 1] = 1;

    for (int i = k + 1; i <= 2 * k + 1; i++) // - k - 1
    {
        for (int j = 0; j <= k; j++)
            base[i][j] = (C[i - k - 1][j] * pw (2, i - k - 1 - j)) % mod;
        for (int j = k + 1; j <= 2 * k + 1; j++)
            base[i][j] = C[i - k - 1][j - k - 1];
    }

    base[2 * k + 2][2 * k + 1] = 1;
    base[2 * k + 2][2 * k + 2] = 1;

    matrix A (2 * k + 3, vector <ll> (1));
    for (int i = 0; i <= k; i++)
        A[i][0] = 1;
    for (int i = k + 1; i <= 2 * k + 1; i++)
        A[i][0] = pw(2, i - k);
    A[2 * k + 2][0] = 1;
    base = pw (base, A, n - 1);
    cout << A[2 * k + 2][0];

}


#include <iostream>
using namespace std;
typedef long long LL;
const LL MOD = 1000000000 + 7;
const int NICO = 100;
const int MAKI = 100;

struct M
{
    LL a[NICO][NICO];
    M() { memset(a,0,sizeof(a)); }
};

int n, k;
LL s, c[NICO][NICO];

void init()
{
    n = 2*k+3;
    for(int i=0;i<=k;i++)
    {
        c[i][0] = c[i][i] = 1;
        for(int j=1;j<i;j++)
        {
            c[i][j] = c[i-1][j-1] + c[i-1][j];
        }
    }
}

M mul(M A , M B)
{
    M ans;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int t=0;t<n;t++)
            {
                ans.a[i][j] += A.a[i][t]*B.a[t][j];
                ans.a[i][j] %= MOD;
            }
        }
    }
    return ans;
}

M qpow(M A, LL t)
{
    M ans;
    for(int i=0;i<n;i++) ans.a[i][i] = 1;
    while(t)
    {
        if(t&1) ans = mul(ans, A);
        A = mul(A, A);
        t /= 2;
    }
    return ans;
}

int main()
{
    cin >> s >> k, init();
    M A;
    for (int i = 0; i <= k; ++i)
    {
        for (int j = 0; j <= i; ++j)
        {
            A.a[i][j] = A.a[i+k+1][j] = A.a[i][j+k+1] = c[i][j];
        }
    }
    A.a[2*k+2][k] = A.a[2*k+2][2*k+2] = 1;
    A = qpow(A, s);
    LL res = 0;
    for(int i=0;i<2*k+2;i++)
    {
        res = (res + A.a[2*k+2][i]) % MOD;
    }
    cout << res << endl;

}


\#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=2000+10;
const int mod=998244353;
ll mypow(ll x, ll res){
	ll ans=1;
	while(res){
		if(res&1) ans=ans*x%mod;
		x=x*x%mod;
		res>>=1;
	}
	return ans;
}
void update(ll& x, ll y){
	x=(x+y)%mod;
}
ll dp[maxn*2][maxn][2];
int main(){
	int n, k, l;
	scanf("%d%d%d", &n, &k, &l);
	dp[0][0][0]=1;
	for(int i=0; i<=2*n+1; i++)
		for(int j=0; j<=n; j++)
			for(int x=0; x<=1; x++){
				if(!dp[i][j][x]) continue;
				if(i+j<2*n+x) update(dp[i+1][j+1][x], dp[i][j][x]);
				if(j) update(dp[i+1][j-1][x], dp[i][j][x]*j);
				if(j>=k && !x) update(dp[i+1][j][1], dp[i][j][x]);
			}
	ll tmp=1;
	for(int i=1; i<=n; i++)
		tmp=tmp*i%mod;
	ll ans=l*dp[2*n+1][0][1]%mod*tmp%mod*mypow(2, n)%mod;
	for(int i=n+1; i<=2*n+1; i++)
		tmp=tmp*i%mod;
	ans=ans*mypow(tmp, mod-2)%mod;
	printf("%lld", ans);
	return 0;
}


hide code 1 #include<bits/stdc++.h>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = 4003, mod = 998244353;
int fac[N], inv[N], invfac[N], po[N];
inline void init(int m){
    fac[0] = 1;
    for(Rint i = 1;i <= m;i ++) fac[i] = (LL) i * fac[i - 1] % mod;
    inv[1] = 1;
    for(Rint i = 2;i <= m;i ++) inv[i] = (LL) (mod - mod / i) * inv[mod % i] % mod;
    invfac[0] = 1;
    for(Rint i = 1;i <= m;i ++) invfac[i] = (LL) invfac[i - 1] * inv[i] % mod;
    po[0] = 1;
    for(Rint i = 1;i <= m;i ++) po[i] = (po[i - 1] << 1) % mod;
}
inline int C(int n, int m){
    if(n < m || m < 0) return 0;
    return (LL) fac[n] * invfac[n - m] % mod * invfac[m] % mod;
}
int n, k, l, ans, f[N];
int main(){
    scanf("%d%d%d", &n, &k, &l);
    init(n << 1 | 1);
    for(Rint i = k;i <= n;i ++)
        for(Rint j = 0;j <= i;j ++){
            int t = (LL) C(i, j) * inv[i + j + 1] % mod;
            if(j & 1) f[i] = (f[i] + mod - t) % mod; else f[i] = (f[i] + t) % mod;
        }
    for(Rint i = k;i <= n;i ++){
        int t1 = 0;
        for(Rint j = 0;j <= n - i;j ++){
            int t2 = (LL) po[i + j] * C(n - i, j) % mod * f[i + j] % mod;
            if(j & 1) t1 = (t1 + mod - t2) % mod; else t1 = (t1 + t2) % mod;
        }
        ans = (ans + (LL) t1 * C(n, i) % mod) % mod;
    }
    printf("%d", (LL) l * ans % mod);
}

CF1153F


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

typedef long long LL;
const LL mod=998244353;
const LL maxn=2005;
LL dp[maxn<<1][maxn],fac[maxn<<1];
int N,K,L;

LL gcd(LL a,LL b,LL& d,LL& x,LL& y)
{
	if(!b)
	{
		d=a;
		x=1;
		y=0;
	}
	else
	{
		gcd(b,a%b,d,y,x);
		y-=x*(a/b);
	}
}
LL inv(LL a)
{
	LL d,x,y;
	gcd(a,mod,d,x,y);
	return d==1 ? (x+mod)%mod:-1;
}
int main()
{
	fac[1]=1;
	for(int i=2;i<2*maxn-1;i++)
		fac[i]=fac[i-1]*i%mod;
	scanf("%d%d%d",&N,&K,&L);
	dp[0][0]=1;
	for(int i=0;i<=(N<<1);i++)
		for(int j=0;j<=min(i,N);j++)
		{
			dp[i+1][j+1]=(dp[i][j]+dp[i+1][j+1])%mod;
			if(j)
				dp[i+1][j-1]=(dp[i+1][j-1]+(dp[i][j]*j)%mod)%mod;
		}
	LL ans=0;
	for(int i=1;i<=(N<<1);i++)
		for(int j=K;j<=min(i,N);j++)
			ans=(ans+(dp[i][j]*dp[N*2-i][j]%mod)*fac[j]%mod)%mod;
	ans=(ans*L)%mod;
	LL q=dp[N*2][0]*(2*N+1)%mod;
	printf("%I64d\n",ans*inv(q)%mod);
}


/*
*	ID : pallab81
* 	PROG : C
* 	LANG : C++
* 	2012-07-30-23.00.57 Monday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

template<class T1>
inline void debug(T1 _x) {
    cout<<_x<<'\n';
}
template<class T1, class T2>
inline void debug(T1 _x,T2 _y) {
    cout<<_x<<' '<<_y<<'\n';
}
template<class T1, class T2, class T3>
inline void debug(T1 _x,T2 _y,T3 _z) {
    cout<<_x<<' '<<_y<<' '<<_z<<'\n';
}
template<class T1, class T2, class T3, class T4>
inline void debug(T1 _x,T2 _y,T3 _z,T4 _zz) {
    cout<<_x<<' '<<_y<<' '<<_z<<' '<<_zz<<'\n';
}
template< class T1>
inline void debug(T1 _array,int _size) {
    cout<<"[";
    for (int i=0; i<_size; ++i) {
        cout<<' '<<_array[i];
    }
    puts(" ]");
}
inline bool CI(int &_x) {
    return scanf("%d",&_x)==1;
}
inline bool CI(int &_x, int &_y) {
    return CI(_x)&&CI(_y) ;
}
inline bool CI(int &_x, int &_y, int &_z) {
    return CI(_x)&&CI(_y)&&CI(_z) ;
}
inline bool CI(int &_x, int &_y, int &_z, int &_zz) {
    return CI(_x)&&CI(_y)&&CI(_z)&&CI(_zz) ;
}
inline void wait( double seconds ) {
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ) {
        ;
    }
}
int jobcount;
vector<int> dep[205];
vector<int> mechines[5];

inline void Read() {
    fo(i,205) {
        dep[i].clear();
    }
    fo(i,5) {
        mechines[i].clear();
    }

    CI(jobcount);
    int at;
    fo(i,jobcount) {
        CI(at);
        mechines[at-1].pb(i);
    }
    int tmp,depon;
    fo(i,jobcount) {
        CI(tmp);
        fo(j,tmp) {
            //job i dependes on job
            CI(depon);
            dep[i].pb(depon-1);
        }
    }

}
int finished[205];
inline bool allclear(int job) {
    foit(i, dep[job] ) {
        if(!finished[ (*i)] )return false;
    }
    return true;
}
int go(int M) {
    int ret=0;
    int cnt=0;
    while(true) {
        while(true) {
            int change=0;
            foit( i, mechines[M]) {
                if( finished[ (*i) ] )continue;
                if( allclear( (*i) ) ) {
                    finished[ (*i) ]=1;
                    ++ret;
                    ++cnt;
                    change=1;
                    any=1;
                }
            }
            if(!change)break;
        }

        if(cnt>=jobcount)break;
        ret= ret+1;
        M=(M+1)%3;
    }
    return ret;
}
inline void Proc() {
    int best = 1023456789;
    fo(i,3) {
        SET(finished,0);
        best = min( best, go(i) );
    }
    cout<<best<<'\n';
}
//#define IO
int main() {
    int tt=1;
#ifdef IO
    freopen("in","rt",stdin);
#endif
    foE(i,1,tt) {
        Read();
        Proc();
    }

    return 0;
}