#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

constexpr int N = 1e6+5;
constexpr int M = 1e9+7;

// #define DEBUG 1  //调试开关
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;

    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }

    inline char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
        return p1 == p2 ? ' ' : *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    template <class T>
    inline void read(T &x) {
    double tmp = 1;
    bool sign = 0;
    x = 0;
    char ch = gc();
    for (; !isdigit(ch); ch = gc())
        if (ch == '-') sign = 1;
    for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
    if (ch == '.')
        for (ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if (sign) x = -x;
    }
    inline void read(char *s) {
    char ch = gc();
    for (; blank(ch); ch = gc())
        ;
    for (; !blank(ch); ch = gc()) *s++ = ch;
    *s = 0;
    }
    inline void read(char &c) {
    for (c = gc(); blank(c); c = gc())
        ;
    }
    inline void push(const char &c) {
    #if DEBUG  //调试，可显示字符
    putchar(c);
    #else
    if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
    *pp++ = c;
    #endif
    }
    template <class T>
    inline void write(T x) {
    if (x < 0) x = -x, push('-');  // 负数输出
    static T sta[35];
    T top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) push(sta[--top] + '0');
    }
    template <class T>
    inline void write(T x, char lastChar) {
    write(x), push(lastChar);
    }
} io;

int n, m, k;
int fact[N];

int kpow(int a, int b) {
    int c = 1;
    while (b) {
        if (b & 1) c = c * a % M;
        a = a * a % M;
        b >>= 1;
    }
    return c;
}

void init() {
    fact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = fact[i-1] * i % M;
    }
}

inline int inv(int x) {
    return kpow(x, M-2);
}

inline int C(int n, int m) {
    return fact[n] * inv(fact[n-m] * fact[m] % M) % M;
}

int dp[N][N];
int ans;

signed main() {
    fastio;
    init();
    io.read(n), io.read(m), io.read(k);
    if (m == 1) {
        io.write(kpow(k, n), '\n');
        return 0;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j] = (j * dp[i-1][j] % M + dp[i-1][j-1]) % M;
        }
    }
    //io.write(dp[2][2], '\n');
    if (m == 2) {
        for (int j = 1; j <= k && j <= n; j++) {
            ans += C(k, j) * C(k, j) % M * (dp[n][j] * fact[j] % M * dp[n][j] % M * fact[j] % M) % M;
            ans %= M;
        }
        io.write(ans, '\n');
        return 0;
    }
    for (int j = 1; j <= k && j <= n; j++) {
        for (int i = 1; i <= j; i++) {
            int side = dp[n][j] % M * fact[j] % M;
            ans += C(k, i) * C(k-i, (j-i)*2) % M * C((j-i)*2, j-i) % M  * (side * side % M * kpow(i, n * (m-2)) % M) % M;
            ans %= M;
        }
    }
    io.write(ans, '\n');
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,fast-math")
#pragma GCC target("sse,sse1,sse2,sse3,sse4,sse4.1,sse4.2,avx,avx2,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int MAXMEM = 5e8;
char mem[MAXMEM];
int ptr = 0;
void* operator new( size_t sz ) {
	ptr += sz;
	if (ptr + sz > MAXMEM) {
		cout << "asfgasfgasfga";
		exit(0);
	}
	return mem + ptr - sz;
}
void operator delete( void* ) {}
#define int long long
const int MAXN = 1e5 + 7;
int d[MAXN], w[MAXN];
vector<int> g[MAXN];
int n, m, k;
map<int, int>* dfs(int v) {
	vector<map<int, int>*> a;
	for (int u : g[v])
		a.push_back(dfs(u));
	sort(a.begin(), a.end(), [](map<int, int>* a, map<int, int>* b) { return a->size() < b->size(); });
	if (a.size() == 0) {
		auto ans = new map<int, int>;
		if (d[v] != -1)
			(*ans)[d[v]] += w[v];
		return ans;
	}
	auto ans = a[0];
	a.erase(a.begin());
	for (auto mp : a)
		for (pair<int, int> dx : *mp)
			(*ans)[dx.first] += dx.second;
	int s = 0;
	if (d[v] != -1) {
		(*ans)[d[v]] += w[v];
		auto it = ans->upper_bound(d[v]);
		while (it != ans->end() && s < w[v]) {
			auto curr = it;
			++it;
			int d = curr->second;
			if (s + d >= w[v])
				(*ans)[curr->first] = s + d - w[v];
			else
				ans->erase(curr);
			s += d;
		}
	}
	return ans;
}
signed main() {
	ios_base::sync_with_stdio(false);

	cin >> n >> m >> k;
	++k;
	for (int i = 1; i < n; ++i) {
		int p;
		cin >> p;
		--p;
		g[p].push_back(i);
	}
	fill(d, d + n, -1);
	for (int i = 0; i < m; ++i) {
		int v;
		cin >> v;
		--v;
		cin >> d[v] >> w[v];
	}
	auto ansv = dfs(0);
	int ans = 0;
	for (auto p : *ansv)
		ans += p.second;
	cout << ans;
	return 0;
}


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

//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

//===================//
//  Added libraries  //
//===================//

//===================//
//end added libraries//
//===================//

void program();
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	program();
}

//===================//
//   begin program   //
//===================//

const int MX = 2e5;
int n, m, k, p[MX], v[MX], d[MX], w[MX], V[MX], D[MX], W[MX];
ll dp[MX][20];
vi chl[MX];

ll getAns(int u, int mx) {
	if(dp[u][mx] == -1) {
		ll ans=0;
		if(d[u] <= mx) {
			ans += w[u];
			for(int v:chl[u]) ans += getAns(v, d[u]);
		}
		ll cAns=0;
		for(int v:chl[u]) cAns += getAns(v, mx);
		ans = max(ans, cAns);
		dp[u][mx] = ans;
	}
	return dp[u][mx];
}

void program2();
void program3();
void program() {
	cin>>n>>m>>k;
	REP(i,1,n) cin>>p[i], p[i]--;
	RE(i,m) {
		cin>>V[i]>>D[i]>>W[i]; D[i]--; V[i]--;
		d[V[i]] = D[i];
		w[V[i]] = W[i];
	}
	bool subt3=1;
	REP(i,1,n) if(p[i] != i-1) subt3=0;
	if(subt3) {
		program3();
		return;
	}
	if(k > 20) {
		program2();
		return;
	}

	REP(i,1,n) chl[p[i]].pb(i);
	RE(i,n) RE(j,k) dp[i][j] = -1;
	cout<<getAns(0,k-1)<<endl;
}

void program2() {
	ll ans=0;
	RE(i,m) ans+=W[i];
	cout<<ans<<endl;
}

int SA[MX];
ll SEG[MX*4];

void setSeg(int i, int x, int p=0, int l=0, int r=k-1) {
	if(i < l || i > r) return;
	if(l == r) {
		SEG[p] = max(SEG[p], x);
		return;
	}
	int m=(l+r)/2;
	setSeg(i,x,p*2+1,l,m);
	setSeg(i,x,p*2+2,m+1,r);
	SEG[p] = max(SEG[p*2+1], SEG[p*2+2]);
}
ll getMax(int i, int j, int p=0, int l=0, int r=k-1) {
	if(j < l || i > r) return 0;
	if(i <= l && j >= r) return SEG[p];
	int m=(l+r)/2;
	ll a=getMax(i,j,p*2+1,l,m);
	ll b=getMax(i,j,p*2+2,m+1,r);
	return max(a,b);
}

void program3() {
	RE(i,m) SA[i] = i;
	sort(SA, SA+m, [](int i, int j) {return V[i]>V[j];});
	RE(i,k*4) SEG[i]=0;
	ll mx=0;
	RE(i,m) {
		ll ans=getMax(0,D[SA[i]])+W[SA[i]];
		setSeg(D[SA[i]], ans);
		mx=max(mx,ans);
	}
	cout<<mx<<endl;
}


#include <bits/stdc++.h>
#define File(_) freopen(#_ ".in", "r", stdin), freopen(#_ ".out", "w", stdout)
#define ALL(x) x.begin(), x.end()
#define mset(a, b) memset(a, b, sizeof a)
#define rep(i, a, b) for(int i(a), i##_END_(b); i <= i##_END_; i++)
#define drep(i, a, b) for(int i(a), i##_END_(b); i >= i##_END_; i--)
using namespace std;
template<class T> inline bool tomax(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<class T> inline bool tomin(T &a, T b) {return b < a ? a = b, 1 : 0;}
typedef long long ll;
typedef double db;

const int N = 1e5 + 5;

template<int N, int M, class T> struct Link {
#define erep(k, G, o) for(int k(G.HEAD[o]); k; k = G.NXT[k])
    int HEAD[N], NXT[M], tot; T W[M];
    void add(int x, T w) {NXT[++tot] = HEAD[x]; W[HEAD[x] = tot] = w;}
    T& operator[] (int x) {return W[x];}
};
Link<N, N, int> G;

int p[N], v[N], d[N], w[N], id[N], n, m, k;

const int NLogN = N * 40;
ll mx[NLogN];
int ls[NLogN], rs[NLogN], tot;
#define mid (L + R >> 1)
#define lson ls[o], L, mid
#define rson rs[o], mid + 1, R
void upd(int x, int d, int &o, int L, int R) {
    if(!o) o = ++tot;
    tomax(mx[o], d);
    if(L == R) return;
    if(x <= mid) upd(x, d, lson);
    else upd(x, d, rson);
}
ll qry(int l, int r, int o, int L, int R) {
    if(!o) return 0;
    if(l == L && r == R) return mx[o];
    if(r <= mid) return qry(l, r, lson);
    else if(l > mid) return qry(l, r, rson);
    else return max(qry(l, mid, lson), qry(mid + 1, r, rson));
}
void merge(int &u, int v) {
    if(!u || !v) {u |= v; return;}
    mx[u] += mx[v];
    merge(ls[u], ls[v]);
    merge(rs[u], rs[v]);
}

int dfs(int o) {
    int rt = 0, x;
    erep(k, G, o) {
        int v = G[k];
        merge(rt, dfs(v));
    }
    if(x = id[o]) {
        ll tmp = qry(1, x, rt, 1, k);
        upd(x, tmp + d[x], rt, 1, k);
    }
    return rt;
}

int main() {
    // freopen("data.in", "r", stdin);
    scanf("%d%d%d", &n, &m, &k);
    rep(i, 2, n) {
        scanf("%d", &p[i]);
        G.add(p[i], i);
    }
    rep(i, 1, m) scanf("%d%d%d", &v[i], &d[i], &w[i]);
    rep(i, 1, m) id[v[i]] = i;
    int rt = dfs(1);
    printf("%I64d\n", qry(1, k, rt, 1, k));
    return 0;
}


#include <bits/stdc++.h>

#define long long long
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 1e5+5;

int n, m, k;
int day[N], cost[N];
vector<int> g[N];

int in[N], out[N], pos[N], hv[N];

int pre_process(int u) {
    static int idx = 0;
    pos[in[u] = ++idx] = u;
    int sz = 1; pii ret(0, -1);
    for(int v : g[u]) {
        int z = pre_process(v);
        sz += z, ret = max(ret, pii(z, v));
    }
    hv[u] = ret.y, out[u] = idx;
    return sz;
}

long dp[N], mx;
map<int, long> mp;

void sack(int u, bool fill) {
    for(int v : g[u]) if(v != hv[u]) sack(v, 0), dp[u] += dp[v];
    if(hv[u] != -1) sack(hv[u], 1), dp[u] += dp[hv[u]];

    if(u == 1) continue;
    if(day[u] != 0) {
        mp[day[u]] += cost[u];
        mx = max(mx, mp[day[u]]);
    }
    for(int v : g[u]) if(v != hv[u]) for(int i = in[v]; i <= out[v]; i++) {
        int now = pos[i];
        if(day[now] == 0) continue;
        mp[day[now]] += cost[now];
        mx = max(mx, mp[day[now]]);
    }
    dp[u] = max(dp[u], mx);
    if(!fill) mx = 0, mp.clear();
}

int main() {
    scanf("%d %d %d", &n, &m, &k);
    for(int i = 2, a; i <= n; i++) {
        scanf("%d", &a);
        g[a].emplace_back(i);
    }
    for(int i = 1, a, b, c; i <= m; i++) {
        scanf("%d %d %d", &a, &b, &c);
        day[a] = b, cost[a] = c;
    }
    pre_process(1), sack(1, 1);
    printf("%lld\n", dp[1]);

    return 0;
}


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

const long double EPS = 1E-8;

class Point {
public:
	long double x, y;

	Point() {}

	Point(long double x, long double y) : x(x), y(y) {}

	inline long double length() const {
		return hypot(x, y);
	}

	inline Point operator + (const Point & other) const {
		return Point(x + other.x, y + other.y);
	}

	inline Point operator - (const Point & other) const {
		return Point(x - other.x, y - other.y);
	}

	inline long double operator * (const Point & other) const {
		return x * other.y - y * other.x;
	}

	inline Point operator * (const long double scale) {
		return Point(x * scale, y * scale);
	}
};

class Lattice {
public:
	long long x, y;

	Lattice() {}

	Lattice(long long x, long long y) : x(x), y(y) {}

	inline void init() {
		scanf("%lld%lld", &x, &y);
	}

	inline long long length2() const {
		return x * x + y * y;
	}

	inline Lattice operator + (const Lattice & other) const {
		return Lattice(x + other.x, y + other.y);
	}

	inline Lattice operator - (const Lattice & other) const {
		return Lattice(x - other.x, y - other.y);
	}

	inline Lattice operator * (const long long scale) const {
		return Lattice(x * scale, y * scale);
	}

	inline long long operator * (const Lattice & other) const {
		return x * other.y - y * other.x;
	}

	inline long long operator ^ (const Lattice & other) const {
		return x * other.x + y * other.y;
	}
};

inline bool isOnlineIn(const Lattice & p, const Lattice & l1, const Lattice & l2) {
	return !((l1 - l2) * (p - l2)) && (l1.x - p.x) * (l2.x - p.x) <= 0 && (l1.y - p.y) * (l2.y - p.y) <= 0;
}

inline bool isOnRayIn(const Lattice & p, const Lattice & l1, const Lattice & l2) {
	return isOnlineIn(p, l1, l2) || isOnlineIn(l2, l1, p);
}

inline bool parallel(const Lattice & u1, const Lattice & u2, const Lattice & v1, const Lattice & v2) {
	return !((u2 - u1) * (v2 - v1));
}

inline int sgn(long long x) {
	if (!x) return 0;
	return x < 0 ? -1 : 1;
}

inline int decideSide(const Lattice & p1, const Lattice & p2, const Lattice & l1, const Lattice & l2) {
	return sgn((l1 - l2) * (p1 - l2)) * sgn((l1 - l2) * (p2 - l2));
}

inline bool isBetween(const Lattice & p, const Lattice & A, const Lattice & P, const Lattice & Q) {
	return decideSide(p, Q, A, P) >= 0 && decideSide(p, P, A, Q) >= 0;
}

inline bool crossLineSeg(const Lattice & u1, const Lattice & u2, const Lattice & v1, const Lattice & v2) {
	if (parallel(u1, u2, v1, v2) || decideSide(v1, v2, u1, u2) > 0) return false;
	if (decideSide(u1, u2, v1, v2) <= 0) return true;
	return llabs((u1 - v2) * (v1 - v2)) > llabs((u2 - v2) * (v1 - v2));
}

inline Point intersection(const Point & u1, const Point & u2, const Point & v1, const Point & v2) {
	return u1 + (u2 - u1) * (((u1 - v1) * (v1 - v2)) / ((u1 - u2) * (v1 - v2)));
}

inline bool zero(long double x) {
	return -EPS < x && x < EPS;
}

const int MAXN = 10010 + 1;

int n;
Lattice lattice[MAXN], A, V, U, P, Q;
Point point[MAXN], a, v, u, p, q;
long long Fdown, Vz, Uz;
long double t1, t2;
bool opposite;

inline void update1(const Point & qq, bool positive) {
	long double len = (a - qq).length();
	if (!positive) len = -len;
	int sgn = opposite ? -1 : 1;
	long double t = len / (v.length() + sgn * u.length() * Vz / Uz);
	if (t > 0) t1 = min(t1, t);
}

inline void update2(const Point & qq) {
	Point pp0 = qq + u, pp = intersection(pp0, qq, a, p);
	long double tt1 = (pp - a).length() / v.length(), tt2 = max(tt1 * Vz - (pp - qq).length() / u.length() * Uz, (long double)0) / Fdown;
	if (tt1 < t1 - EPS || (zero(tt1 - t1) && tt2 < t2 - EPS)) {
		t1 = tt1;
		t2 = tt2;
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		lattice[i].init();
		point[i] = Point((long double)lattice[i].x, (long double)lattice[i].y);
	}
	lattice[n] = lattice[0];
	point[n] = point[0];
	A.init();
	a = Point((long double)A.x, (long double)A.y);
	V.init();
	v = Point((long double)V.x, (long double)V.y);
	scanf("%lld%lld", &Vz, &Fdown);
	Fdown = -Fdown;
	U.init();
	u = Point((long double)U.x, (long double)U.y);
	scanf("%lld", &Uz);
	Uz = -Uz;
	if (!V.length2() && !U.length2()) {
		puts("-1.000000000000000 -1.000000000000000");
		return 0;
	}
	t1 = t2 = 1E100;
	if (!(V * U)) {
		opposite = ((V ^ U) < 0);
		if (opposite) {
			P = A + V;
			p = a + v;
			Q = A + U;
			q = a + u;
			if ((V * Uz).length2() == (U * Vz).length2()) {
				puts("-1.000000000000000 -1.000000000000000");
				return 0;
			}
		} else {
			if (V.length2() > 0) {
				P = A + V;
				p = a + v;
			} else {
				P = A + U;
				p = a + u;
			}
		}
		t2 = 0;
		for (int i = 0; i < n; i++) {
			if (!((lattice[i] - A) * (P - A))) continue;
			update1(point[i], isOnRayIn(lattice[i], A, P));
		}
		for (int i = 0; i < n; i++) {
			if (opposite) {
				if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
					Point pp = intersection(a, p, point[i], point[i + 1]);
					update1(pp, true);
				} else if (crossLineSeg(A, Q, lattice[i], lattice[i + 1])) {
					Point qq = intersection(a, q, point[i], point[i + 1]);
					update1(qq, false);
				}
			} else {
				if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
					Point pp = intersection(a, p, point[i], point[i + 1]);
					update1(pp, true);
				}
			}
		}
	} else {
		P = A + V * Uz;
		p = a + v;
		Q = P + U * Vz;
		q = p + u * ((long double)Vz / Uz);
		for (int i = 0; i < n; i++) {
			if (isBetween(lattice[i], A, P, Q)) update2(Point((long double)lattice[i].x, (long double)lattice[i].y));
		}
		for (int i = 0; i < n; i++) {
			if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
				Point pp = intersection(a, p, point[i], point[i + 1]);
				update2(pp);
			}
			if (crossLineSeg(A, Q, lattice[i], lattice[i + 1])) {
				Point qq = intersection(a, q, point[i], point[i + 1]);
				update2(qq);
			}
		}
	}
	if (t1 > 1E90) t1 = t2 = -1;
	printf("%.15Lf %.15Lf\n", t1, t2);
	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<int,pii> p3i;
typedef long double ld;
typedef vector<ld> vd;

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

int n;
int x[11111];
int y[11111];
int xa,ya;
int xv,yv,zv;
int f;
int xu,yu,zu;

ll a[2][3];
ll det(ll a11,ll a12,ll a21, ll a22){
  return a11*a22-a12*a21;
}

double b[2][3];
double det2(double a11,double a12,double a21,double a22){
  return a11*a22-a12*a21;
}

double r_t1=-1,r_t2=-1;

const double eps = 1e-9;

int main(){
#ifdef LocalHost
  freopen("input.txt","r",stdin);
#endif
  cin>>n;
  REP(i,n) scanf("%d %d",x+i,y+i);
  x[n]=x[0],y[n]=y[0];
  scanf("%d %d",&xa,&ya);
  scanf("%d %d %d",&xv,&yv,&zv);
  scanf("%d",&f);
  scanf("%d %d %d",&xu,&yu,&zu);

  REP(i,n){
    a[0][0]=xv;
    a[0][1]=-(x[i+1]-x[i]);
    a[0][2]=x[i]-xa;
    a[1][0]=yv;
    a[1][1]=-(y[i+1]-y[i]);
    a[1][2]=y[i]-ya;

    ll d = det(a[0][0],a[0][1],a[1][0],a[1][1]);
    if(d){
      ll T1 = det(a[0][2],a[0][1],a[1][2],a[1][1]);
      double t = double(T1)/d;
      ll T2 = det(a[0][0],a[0][2],a[1][0],a[1][2]);
      double t2 = double(T2)/d;
      if(t2>=-eps && t2<=1+eps)
	if(t>0 && (r_t1 < -0.5 || r_t1 > t)){
	  r_t1=t;
	  r_t2 = t * zv/(-f);
	}
    }
  }

  REP(i,n){
    double t = hypot(x[i]-xa,y[i]-ya)/hypot(xv,yv);
    double xx = xa + xv*t;
    double yy = ya + yv*t;
    if(fabs(xx-x[i])<eps && fabs(yy-y[i])<eps){
      double t2 = t * zv / (-f);
      if(r_t1 < -0.5 || r_t1 > t + eps || fabs(r_t1-t)<eps && r_t2 > t2){
	r_t1=t;
	r_t2=t2;
      }
    }
  }

  if(zu<0)
    REP(i,n){
      a[0][0]=xv;
      a[0][1]=-xu;
      a[0][2]=x[i]-xa;
      a[1][0]=yv;
      a[1][1]=-yu;
      a[1][2]=y[i]-ya;

      ll d = det(a[0][0],a[0][1],a[1][0],a[1][1]);
      if(d){
	ll T1 = det(a[0][2],a[0][1],a[1][2],a[1][1]);
	double t1 = double(T1)/d;

	ll T2 = det(a[0][0],a[0][2],a[1][0],a[1][2]);
	double t2 = double(T2)/d;

	t2=-t2;

	if(t1>0 && t2>=-1e-8){
	  double t3 = (t1*zv + t2*zu)/(-f);
	  if(t3>=-1e-8){
	    if(r_t1<-0.5 || r_t1 > t1 + eps || fabs(r_t1-t1)<eps && r_t2 > t3 + eps){
	      r_t1 = t1;
	      r_t2 = t3;
	    }
	  }
	}
      }
    }
  if(zu<0 && det(xv,-xu,yv,-yu)==0){
    REP(i,n){
      double X = xv + xu * double(zv)/double(-zu);
      double Y = yv + yu * double(zv)/double(-zu);

      double t = hypot(x[i]-xa,y[i]-ya)/hypot(X,Y);
      double xx = xa + X*t;
      double yy = ya + Y*t;
      if(fabs(xx-x[i])<eps && fabs(yy-y[i])<eps){
	double t2 = 0;//t * zv / (-f);
	if(r_t1 < -0.5 || r_t1 > t + eps || fabs(r_t1-t)<eps && r_t2 > t2){
	  r_t1=t;
	  r_t2=t2;
	}
      }

    }
  }
  if(zu<0){
    REP(i,n){
      double X = xv + xu * double(zv)/double(-zu);
      double Y = yv + yu * double(zv)/double(-zu);

      b[0][0]=X;
      b[0][1]=-(x[i+1]-x[i]);
      b[0][2]=x[i]-xa;
      b[1][0]=Y;
      b[1][1]=-(y[i+1]-y[i]);
      b[1][2]=y[i]-ya;

      double d = det2(b[0][0],b[0][1],b[1][0],b[1][1]);
      if(fabs(d)>eps){
	ll T1 = det2(b[0][2],b[0][1],b[1][2],b[1][1]);
	double t = double(T1)/d;
	ll T2 = det2(b[0][0],b[0][2],b[1][0],b[1][2]);
	double t2 = double(T2)/d;
	if(t2>=-eps && t2<=1+eps)
	  if(t>0 && (r_t1 < -0.5 || r_t1 > t)){
	    r_t1=t;
	    r_t2=0;
	  }
      }

  }

  printf("%.9lf %.9lf\n",r_t1,r_t2);
  return 0;
}


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

const long double EPS = 1E-8;

class Point {
public:
	long double x, y;

	Point() {}

	Point(long double x, long double y) : x(x), y(y) {}

	inline long double length() const {
		return hypot(x, y);
	}

	inline Point operator + (const Point & other) const {
		return Point(x + other.x, y + other.y);
	}

	inline Point operator - (const Point & other) const {
		return Point(x - other.x, y - other.y);
	}

	inline long double operator * (const Point & other) const {
		return x * other.y - y * other.x;
	}

	inline Point operator * (const long double scale) {
		return Point(x * scale, y * scale);
	}
};

class Lattice {
public:
	long long x, y;

	Lattice() {}

	Lattice(long long x, long long y) : x(x), y(y) {}

	inline void init() {
		scanf("%lld%lld", &x, &y);
	}

	inline long long length2() const {
		return x * x + y * y;
	}

	inline Lattice operator + (const Lattice & other) const {
		return Lattice(x + other.x, y + other.y);
	}

	inline Lattice operator - (const Lattice & other) const {
		return Lattice(x - other.x, y - other.y);
	}

	inline Lattice operator * (const long long scale) const {
		return Lattice(x * scale, y * scale);
	}

	inline long long operator * (const Lattice & other) const {
		return x * other.y - y * other.x;
	}

	inline long long operator ^ (const Lattice & other) const {
		return x * other.x + y * other.y;
	}
};

inline bool isOnlineIn(const Lattice & p, const Lattice & l1, const Lattice & l2) {
	return !((l1 - l2) * (p - l2)) && (l1.x - p.x) * (l2.x - p.x) <= 0 && (l1.y - p.y) * (l2.y - p.y) <= 0;
}

inline bool isOnRayIn(const Lattice & p, const Lattice & l1, const Lattice & l2) {
	return isOnlineIn(p, l1, l2) || isOnlineIn(l2, l1, p);
}

inline bool parallel(const Lattice & u1, const Lattice & u2, const Lattice & v1, const Lattice & v2) {
	return !((u2 - u1) * (v2 - v1));
}

inline int sgn(long long x) {
	if (!x) return 0;
	return x < 0 ? -1 : 1;
}

inline int decideSide(const Lattice & p1, const Lattice & p2, const Lattice & l1, const Lattice & l2) {
	return sgn((l1 - l2) * (p1 - l2)) * sgn((l1 - l2) * (p2 - l2));
}

inline bool isBetween(const Lattice & p, const Lattice & A, const Lattice & P, const Lattice & Q) {
	return decideSide(p, Q, A, P) >= 0 && decideSide(p, P, A, Q) >= 0;
}

inline long long abs(long long x) {
	return x < 0 ? -x : x;
}

inline bool crossLineSeg(const Lattice & u1, const Lattice & u2, const Lattice & v1, const Lattice & v2) {
	if (parallel(u1, u2, v1, v2) || decideSide(v1, v2, u1, u2) > 0) return false;
	if (decideSide(u1, u2, v1, v2) <= 0) return true;
	return abs((u1 - v2) * (v1 - v2)) > abs((u2 - v2) * (v1 - v2));
}

inline Point intersection(const Point & u1, const Point & u2, const Point & v1, const Point & v2) {
	return u1 + (u2 - u1) * (((u1 - v1) * (v1 - v2)) / ((u1 - u2) * (v1 - v2)));
}

inline bool zero(long double x) {
	return -EPS < x && x < EPS;
}

const int MAXN = 10010 + 1;

int n;
Lattice lattice[MAXN], A, V, U, P, Q;
Point point[MAXN], a, v, u, p, q;
long long Fdown, Vz, Uz;
long double t1, t2;
bool opposite;

inline void update1(const Point & qq, bool positive) {
	if (positive && opposite) {
		long double tt1 = (a - qq).length() / v.length(), tt2 = tt1 * Vz / Fdown;
		if (tt1 < t1 - EPS || (zero(tt1 - t1) && tt2 < t2 - EPS)) {
			t1 = tt1;
			t2 = tt2;
		}
		return ;
	}
	long double len = (a - qq).length();
	if (!positive) len = -len;
	int sgn = opposite ? -1 : 1;
	long double t = len / (v.length() + sgn * u.length() * Vz / Uz);
	if (t < EPS) return ;
	if (t < t1 + EPS) {
		t1 = min(t1, t);
		t2 = 0;
	}
}

inline void update2(const Point & qq) {
	Point pp0 = qq + u, pp = intersection(pp0, qq, a, p);
	long double tt1 = (pp - a).length() / v.length(), tt2 = max(tt1 * Vz - (pp - qq).length() / u.length() * Uz, (long double)0) / Fdown;
	if (tt1 < t1 - EPS || (zero(tt1 - t1) && tt2 < t2 - EPS)) {
		t1 = tt1;
		t2 = tt2;
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		lattice[i].init();
		point[i] = Point((long double)lattice[i].x, (long double)lattice[i].y);
	}
	lattice[n] = lattice[0];
	point[n] = point[0];
	A.init();
	a = Point((long double)A.x, (long double)A.y);
	V.init();
	v = Point((long double)V.x, (long double)V.y);
	scanf("%lld%lld", &Vz, &Fdown);
	Fdown = -Fdown;
	U.init();
	u = Point((long double)U.x, (long double)U.y);
	scanf("%lld", &Uz);
	Uz = -Uz;
	if (!V.length2() && !U.length2()) {
		puts("-1.000000000000000 -1.000000000000000");
		return 0;
	}
	t1 = t2 = 1E100;
	if (!(V * U)) {
		opposite = ((V ^ U) < 0);
		if (opposite) {
			P = A + V;
			p = a + v;
			Q = A + U;
			q = a + u;
		} else {
			if (V.length2() > 0) {
				P = A + V;
				p = a + v;
			} else {
				P = A + U;
				p = a + u;
			}
		}
		for (int i = 0; i < n; i++) {
			if ((lattice[i] - A) * (P - A)) continue;
			update1(point[i], isOnRayIn(lattice[i], A, P));
		}
		for (int i = 0; i < n; i++) {
			if (opposite) {
				if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
					Point pp = intersection(a, p, point[i], point[i + 1]);
					update1(pp, true);
				} else if (crossLineSeg(A, Q, lattice[i], lattice[i + 1])) {
					Point qq = intersection(a, q, point[i], point[i + 1]);
					update1(qq, false);
				}
			} else {
				if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
					Point pp = intersection(a, p, point[i], point[i + 1]);
					update1(pp, true);
				}
			}
		}
	} else {
		P = A + V * Uz;
		p = a + v;
		Q = P + U * Vz;
		q = p + u * ((long double)Vz / Uz);
		for (int i = 0; i < n; i++) {
			if (isBetween(lattice[i], A, P, Q)) update2(Point((long double)lattice[i].x, (long double)lattice[i].y));
		}
		for (int i = 0; i < n; i++) {
			if (crossLineSeg(A, P, lattice[i], lattice[i + 1])) {
				Point pp = intersection(a, p, point[i], point[i + 1]);
				update2(pp);
			}
			if (crossLineSeg(A, Q, lattice[i], lattice[i + 1])) {
				Point qq = intersection(a, q, point[i], point[i + 1]);
				update2(qq);
			}
		}
	}
	if (t1 > 1E90) puts("-1.000000000000000 -1.000000000000000");
	else printf("%.15Lf %.15Lf\n", fabs(t1), fabs(t2));
	return 0;
}


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

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

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

template<class T> T abs(T x) {
	re x > 0 ? x : -x;
}

const int MAX = 300;
const ld eps = 1e-13;
const ld eps2 = 1e-11;

int n;
int m;
int x1, y1, z1, x2, y2, z2, f, x[10001], y[10001];

int vect (int x1, int y1, int x2, int y2) {
	re x1 * y2 - x2 * y1;
}

int scal (int x1, int y1, int x2, int y2) {
	re x1 * x2 + y1 * y2;
}

int _in (ld xx, ld yy) {
	ld sum = 0, rl = 0;
	for (int i = 0; i < n; i++) {
		sum += fabs ((x[i] - xx) * (y[i + 1] - yy) - (x[i + 1] - xx) * (y[i] - yy));
		rl += x[i] * y[i + 1] - x[i + 1] * y[i];
	}
	if (fabs (sum - fabs (rl)) > 1e-10) re 0;
	re 1;
}

int cross (ld x1, ld y1, ld x2, ld y2, ld x3, ld y3, ld x4, ld y4, ld *t) {
	ld a = x2 - x1;
	ld b = x3 - x4;
	ld c = x3 - x1;
	ld d = y2 - y1;
	ld e = y3 - y4;
	ld f = y3 - y1;
	if (fabs (a * e - b * d) > eps) {
		ld T = (c * e - b * f) / (a * e - b * d);
		ld S = (a * f - c * d) / (a * e - b * d);
//		printf ("%.10f %.10f\n", T, S);
		if (T > -eps2 && T < 1 + eps2 && S > -eps2 && S < 1 + eps2) {
			*t = T;
			re 1;
		}
	}
	re 0;
}

int cross2 (ld x1, ld y1, ld x2, ld y2, ld x3, ld y3, ld x4, ld y4, ld *t) {
	ld a = x2 - x1;
	ld b = x3 - x4;
	ld c = x3 - x1;
	ld d = y2 - y1;
	ld e = y3 - y4;
	ld f = y3 - y1;
	if (fabs (a * e - b * d) > eps) {
		ld T = (c * e - b * f) / (a * e - b * d);
		ld S = (a * f - c * d) / (a * e - b * d);
//		printf ("%.10f %.10f\n", T, S);
		if (T > -eps2 && S > -eps2 && S < 1 + eps2) {
			*t = T;
			re 1;
		}
	}
	re 0;
}

int check (ld x1, ld y1, ld x2, ld y2, ld *t) {
//	printf ("%.15f %.15f %.15f %.15f\n", (double)x1, (double)y1, (double)x2, (double)y2);
	if (_in (x1, y1)) {
		*t = 0;
		re 1;
	}
	int ok = 0;
	for (int i = 0; i < n; i++) {
		ld nt;
		if (cross (x1, y1, x2, y2, x[i], y[i], x[i + 1], y[i + 1], &nt)) {
//			printf ("%d: %.10f\n", i, (double)nt);
			if (!ok) *t = nt; else *t = min (*t, nt);
			ok = 1;
		}
	}
	re ok;
}

int check2 (ld x1, ld y1, ld x2, ld y2, ld *t) {
//	printf ("%.5f %.5f %.5f %.5f\n", x1, y1, x2, y2);
	if (_in (x1, y1)) {
		*t = 0;
		re 1;
	}
	int ok = 0;
	for (int i = 0; i < n; i++) {
		ld nt;
		if (cross2 (x1, y1, x2, y2, x[i], y[i], x[i + 1], y[i + 1], &nt)) {
			if (!ok) *t = nt; else *t = min (*t, nt);
			ok = 1;
//			printf ("%d: %.10f\n", i, *t);
		}
	}
//	printf ("%d\n", ok);
	re ok;
}

int check3 (ld x1, ld y1, ld x2, ld y2, int i) {
	re int ((x[i] - x1) * (x[i] - x2) + (y[i] - y1) * (y[i] - y2) < eps);
}

int calc (int i, ld x1, ld y1, ld x2, ld y2) {
	ld v = (x[i] - x1) * (y2 - y1) - (x2 - x1) * (y[i] - y1);
	re v > eps ? 1 : (v < -eps ? -1 : 0);
}

int main () {
	scanf ("%d", &n);
	for (int i = 0; i < n; i++) scanf ("%d%d", &x[i], &y[i]);
	int X, Y;
	scanf ("%d%d", &X, &Y);
	for (int i = 0; i < n; i++) { x[i] -= X; y[i] -= Y; }
	x[n] = x[0];
	y[n] = y[0];
	scanf ("%d%d%d", &x1, &y1, &z1);
	scanf ("%d", &f); f = -f;
	scanf ("%d%d%d", &x2, &y2, &z2); z2 = -z2;
	ld x3 = x1 + x2 * ((ld)z1 / z2), y3 = y1 + y2 * ((ld)z1 / z2);
	ld t1 = 1e20, t2 = 1e20, t, tt;
	if (check2 (0, 0, x1, y1, &t)) {
		check2 (x1 * t + x2 * ((z1 * t) / z2), y1 * t + y2 * ((z1 * t) / z2), x1 * t, y1 * t, &tt);
		tt *= ((z1 * t) / f);
		if (t1 > t + eps || fabs (t - t1) < eps && t2 > tt + eps) {
			t1 = t;
			t2 = tt;
		}
	}
	if (check2 (0, 0, x3, y3, &t)) {
		check2 (x1 * t + x2 * ((z1 * t) / z2), y1 * t + y2 * ((z1 * t) / z2), x1 * t, y1 * t, &tt);
		tt *= ((z1 * t) / f);
		if (t1 > t + eps || fabs (t - t1) < eps && t2 > tt + eps) {
			t1 = t;
			t2 = tt;
		}
	}
	if (vect (x1, y1, x2, y2) != 0) {
		ld T = 1e20;
		for (int i = 0; i < n; i++) {
			ld l = 0, r = 1e20, t;
			int need = calc (i, x1 * r, y1 * r, x1 * r + x2 * ((z1 * r) / z2), y1 * r + y2 * ((z1 * r) / z2)), ok = 0;
			for (int it = 0; it < MAX; it++) {
				ld s = (l + r) / 2;
				int tmp = calc (i, x1 * s, y1 * s, x1 * s + x2 * ((z1 * s) / z2), y1 * s + y2 * ((z1 * s) / z2));
				if (tmp == need) r = s; else { ok = 1; l = s; }
			}
			if (r < T && check3 (x1 * r, y1 * r, x1 * r + x2 * ((z1 * r) / z2), y1 * r + y2 * ((z1 * r) / z2), &t)) T = r;
		}
		if (T < 5e19) {
			ld t = T, tt;
			if (check (x1 * T + x2 * ((z1 * T) / z2), y1 * T + y2 * ((z1 * T) / z2), x1 * T, y1 * T, &tt)) {
				tt *= ((z1 * T) / f);
				if (t1 > t + eps || fabs (t - t1) < eps && t2 > tt + eps) {
					t1 = t;
					t2 = tt;
				}
                	}
		}
	}
	if (t1 > 5e19) printf ("-1 -1\n"); else printf ("%.15f %.15f\n", (double)t1, (double)t2);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 2e5 + 5;

typedef long long LL;

const LL mod = 998244353LL;

LL qpow(LL a, LL n){
    LL ret = 1;
    while(n > 0){
        if(n & 1){
            ret = ret * a % mod;
        }
        a = a * a % mod;
        n >>= 1;
    }
    return ret;
}

LL inv(LL x){
    return qpow(x, mod - 2);
}

int a[maxn];
LL w[maxn];
int n, m;
int main(){
    while(cin>>n>>m){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        LL w0=0,w1=0;
        for(int i=0;i<n;i++){
            scanf("%lld",&w[i]);
            if(a[i]==0)
                w0+=w[i];
            else
                w1+=w[i];
        }
        vector<vector<LL>> dp(m+1,vector<LL>(m+1));
    	dp[0][0] = 1;
    	for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(dp[i][j]&&i+j<m)
				{
					LL w00 = w0-j, w11 = w1+i;
					if(w00<0 || w00+w11<=0) continue;
					dp[i+1][j]+=((dp[i][j]*w11%mod*inv(w00+w11))%mod);
					dp[i+1][j]%=mod;
					dp[i][j+1]+=((dp[i][j]*w00%mod*inv(w00+w11))%mod);
					dp[i][j+1]%=mod;
				}
			}
		 }
		LL exp_w0=0,exp_w1=0;
		LL inv_w0=inv(w0),inv_w1=inv(w1);
		for(int i=0;i<=m;i++)
		{
			exp_w0+=(dp[m-i][i]*i%mod);
			exp_w0%=mod;
			exp_w1+=(dp[i][m-i]*i%mod);
			exp_w1%=mod;
		}
		//cout<<exp_w0<<" "<<exp_w1<<endl;
		//cout<<exp_w0*inv_w0%mod<<endl;
        for(int i = 0;i < n;i++){
            LL tmp;
            if(a[i] == 0){
                tmp = exp_w0;
                tmp = tmp * inv_w0 % mod;
                tmp = tmp * w[i] % mod;
                tmp = (-tmp + w[i] + mod) % mod;
            }else{

                tmp = (exp_w1 * inv_w1 % mod * w[i] + w[i]) % mod;
            }
            cout << tmp << endl;
        }
        //cout<<124754432*inv(40180224)%mod<<endl;
    }
    return 0;
}


///题解：https://ouuan.github.io/Codeforces-Round-564-%E4%B8%AD%E6%96%87%E9%A2%98%E8%A7%A3/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define ULL unsigned long long
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define INF 0x3f3f3f3f
#define mem(i, j) memset(i, j, sizeof(i))
#define make(i, j) make_pair(i, j)
#define pb push_back
using namespace std;
const int N = 1e5 + 10, mod = 998244353, M = 3e3 + 10;
int read()
{
	sl = 0; fh = 1;
	char ch = getchar();
	while(ch < '0' || '9' < ch) {if(ch == '-') fh = -1; ch = getchar();}
	while('0' <= ch && ch <= '9') sl = sl * 10 + ch-'0',ch = getchar();
	return sl * fh;
}
int ksm(int x, int y) {
    int ans = 1;
    while(y) {
        if( y & 1 ) ans = (LL) ans * x % mod;
        x = (LL) x * x % mod;
        y >>= 1;
    }
    return ans;
}
int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3];
///sum[0] : don't like ; sum[1]: like; sum[2]:all
int main() {
    n = read(); m =read();
    //cout << n << " " << m <<endl;
    rep(i, 1, n) a[i] = read();
    rep(i, 1, n) {
        w[i] = read();
        sum[a[i]] += w[i]; sum[2] += w[i];
    }
    rep(i, 0, 2 * m) inv[i] = ksm(sum[2] + i - m, mod - 2); ///预处理逆元
    dep(i, 0, m) {
        f[i][m - i] = g[i][m - i] = 1;
        dep(j, 0, m - i - 1) {
            f[i][j] = (1LL * (sum[1] + i + 1) * f[i + 1][j] + 1LL * (sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod;
			g[i][j] = (1LL * (sum[1] + i) * g[i + 1][j] + 1LL * (sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod;
        }
    }
    rep(i, 1, n) printf("%I64d\n", 1LL * w[i] * (a[i] ? f[0][0] : g[0][0]) % mod);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    function<ll(ll, ll)> power = [&](ll x, ll y) {
        if (y == 0) {
            return 1LL;
        } else {
            ll z = power(x, y / 2);
            return y % 2 == 0 ? (z * z) % 998244353 : ((z * z) % 998244353 * x) % 998244353;
        }
    };
    unordered_map<ll, ll> divs;
    auto div = [&](ll x) {
        if (divs.find(x) != divs.end()) {
            return divs[x];
        }
        return divs[x] = power(x, 998244353 - 2);
    };
    ll n, m;
    cin >> n >> m;
    vector<bool> a((size_t) n);
    vector<ll> w((size_t) n);
    for (ll i = 0; i < n; i++) {
        ll v;
        cin >> v;
        a[i] = v == 1;
    }
    for (ll i = 0; i < n; i++) {
        cin >> w[i];
    }
    ll zeros = 0, ones = 0;
    for (ll i = 0; i < n; i++) {
        if (a[i]) {
            ones += w[i];
        } else {
            zeros += w[i];
        }
    }
    map<tuple<ll, ll>, ll> dp0;
    function<ll(ll, ll)> f0 = [&](ll z, ll o) {
        if (z - o <= zeros - ones - m) {
            return 1LL;
        }
        auto it = dp0.find({z, o});
        if (it != dp0.end()) {
            return it->second;
        }
        ll r = z * f0(z - 1, o) + (o + 1) * f0(z, o + 1);
        r %= 998244353;
        r *= div(z + o);
        r %= 998244353;
        return dp0[{z, o}] = r;
    };
    map<tuple<ll, ll>, ll> dp1;
    function<ll(ll, ll)> f1 = [&](ll z, ll o) {
        if (z - o <= zeros - ones - m) {
            return 1LL;
        }
        auto it = dp1.find({z, o});
        if (it != dp1.end()) {
            return it->second;
        }
        ll r = (z - 1) * f1(z - 1, o) + o * f1(z, o + 1);
        r %= 998244353;
        r *= div(z + o);
        r %= 998244353;
        return dp1[{z, o}] = r;
    };
    for (ll i = 0; i < n; i++) {
        if (a[i]) {
            cout << (f0(zeros, ones) * w[i]) % 998244353 << endl;
        } else {
            cout << (f1(zeros, ones) * w[i]) % 998244353 << endl;
        }
    }
    return 0;
}


///题解：https://ouuan.github.io/Codeforces-Round-564-%E4%B8%AD%E6%96%87%E9%A2%98%E8%A7%A3/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define ULL unsigned long long
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define INF 0x3f3f3f3f
#define mem(i, j) memset(i, j, sizeof(i))
#define make(i, j) make_pair(i, j)
#define pb push_back
using namespace std;
const int N = 1e5 + 5, mod = 998244353, M = 3e3 + 5
int ksm(int x, int y) {
    int ans = 1;
    while(y) {
        if( y & 1 ) ans = (LL) ans * x % mod;
        x = (LL) x * x % mod;
        y >>= 1;
    }
    return ans;
}
int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3];
///sum[0] : don't like ; sum[1]: like; sum[2]:all
int main() {
    scanf("%d%d", &n, &m);
    //cout << n << " " << m <<endl;
    rep(i, 1, n) scanf("%d", a + i);
    rep(i, 1, n) {
        scanf("%d", w + i);
        sum[a[i]] += w[i]; sum[2] += w[i];
    }
    for (int i = max(0, m - sum[0]) ; i <= 2 * m; ++i) inv[i] = ksm(sum[2] + i - m, mod - 2); ///预处理逆元
    dep(i, 0, m) {
        f[i][m - i] = g[i][m - i] = 1;
        for(int j = min(m - i - 1, sum[0]); j >= 0; j--) {
            f[i][j] = ((LL) (sum[1] + i + 1) * f[i + 1][j] + (LL) (sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod;
			g[i][j] = ((LL) (sum[1] + i) * g[i + 1][j] + (LL) (sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod;
        }
    }
    rep(i, 1, n) printf("%d\n", int((LL) w[i] * (a[i] ? f[0][0] : g[0][0]) % mod));
    return 0;
}


#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#include <bits/stdc++.h>
#define int unsigned long long
#define double long double
#define x first
#define y second
#define yy cout<<"YES\n"
#define nn cout<<"NO\n"
#define rt return
#define ct continue
#define br break
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mmod 1000000007
#define pmod 998244353
using namespace std;
int gcd(int a,int b){
    while (b) b^=a^=b^=a%=b;
    return a;
}
int lcm(int a,int b){
    return a*b/gcd(a,b);
}
int bigmod(int b,int p,int m){
    int ans=1;
    b%=m;
    if (!b) return 0;
    while (p){
        if (p&1) ans=(ans*b)%m;
        p>>=1;
        b=(b*b)%m;
    }
    return ans;
}
int ncr(int n,int r){
	if (n<r) return 0ll;
	int ans=1,k=min(n-r,r);
	for (int i=1; i<=k; i++) ans=ans*(n-i+1)/i;
	return ans;
}
double eu(int x1,int y1,int x2,int y2){
	int dx=abs(x1-x2),dy=abs(y1-y2);
	return sqrt(dx*dx+dy*dy);
}
int ham(string s,string t){
	int ans=0;
	for (int i=0; i<s.size(); i++) ans+=(s[i]!=t[i]);
	return ans;
}
int ce(int a,int b){
	if (a%b==0) return a/b;
	else return a/b+1;
}
bool cmp(){
	return 1;
}
void solve(){
	int n;
	cin>>n;
	if (n==1) cout<<"1\n";
	else if (n==2||n==3) cout<<"2\n";
	else {
		int h=n/2;
		if (h%2==0){
			int t=h/2+1;
			cout<<bigmod(t,2,mmod)<<endl;
		} else {
			int t=h/2+1;
			cout<<(bigmod(t,2,mmod)+t)%mmod<<endl;
		}
	}

}
signed main(){
	//freopen("input.in","r",stdin);
	//freopen("output.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin>>t;
    while (t--) solve();
}


// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef long long ll;

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

const int mod=1e9+7;

int main() {
	int T=read();
	while (T--) {
		ll n=read();
		__int128 ans=(__int128)(n/2+2)*(n/2+2)/4;
		printf("%d\n",ans%mod);
	}
	return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int ub[7][2];
int lb[7][2];
map<ll,int>mp;
void read(ll &x) {
        x = 0; char ch = getchar();
        while(!isdigit(ch)) ch = getchar();
        while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
}
void my_add(int &x,int v){
    x+=v;
    if(x>=mod)x-=mod;
}
void my_sub(int &x,int v){
    x-=v;
    if(x<0)x+=mod;
}
void go() {
    ll x;
    read(x);
    if (mp.count(x)) {
        cout << mp[x] << '\n';
        return;
    }
    vector<int>v;
    while (x) {
        v.pb(x & 1);
        x >>= 1;
    }
    reverse(all(v));
    dp[0][0] = 1;
    dp[0][1] = 1;
    vector<int>pre{0,1,2,2,2,2,2,2};
    for (int i = 1 ; i < v.size() ; i++) {
        vector<int>now{0};
        for (int j = 0 ; j < 7 ; j++) {
            int x = pre[ub[j][v[i]]];
            int y = now.back();
            my_sub(x, pre[lb[j][v[i]]]);
            my_add(y, x);
            now.pb(y);
        }
        pre = now;
    }
    mp[x] = pre[1];
    cout << pre[1] << '\n';
}
int main() {
    f(7) {
        fr(j,0,2) {
            ub[i][j] = min(6,(i+7-j)/2) + 1;
            lb[i][j] = max(0,(i-j+1) / 2);
        }
    }
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int c = 1;
    ll t;
    if (!c) {
        t = 1;
    }
    else {
        read(t);
    }
    while (t--) {
        go();
    }

}


#include <bits/stdc++.h>

using ll = long long;
const int N = 100010;
const long double eps = 1e-9;
int sig(long double p){return (p > eps) - (p < -eps);}
long double sqr(long double p){return p * p;}
ll sqr(int p){return 1ll * p * p;}

struct P{
    long double x, y;

    explicit P(long double x = 0, long double y = 0):x(x), y(y){}

    P operator +(const P &p)const{return P(x + p.x, y + p.y);}
    P operator -(const P &p)const{return P(x - p.x, y - p.y);}
    P operator *(const long double &p)const{return P(x * p, y * p);}
    long double operator %(const P &p)const{ return x * p.x + y * p.y; }
    long double abs2()const{ return *this % *this; }
    P rot90()const{ return P(-y, x); }
};

int n;
int x[N], y[N];

struct C{
    P o;
    long double r;

    C(P o, long double r):o(o), r(r){}
};

std::vector <P> isCC(C c1, C c2){
    if (!sig(c1.x - c2.x) && !sig(c1.y - c2.y)){
        return {};
    }
    long double d = (c1.o - c2.o).abs2();
    long double x = ((c1.r - c2.r) / d + 1) / 2;
    long double y = std::max(c1.r / d - sqr(x), 0.0L);
    P q1 = c1.o + (c2.o - c1.o) * x;
    P q2 = ((c2.o - c1.o) * std::sqrt(y)).rot90();
    return {q1 - q2, q1 + q2};
}

void solve(){
    std::vector <ll> dis(n);
    for (int i = 0; i < n; ++ i){
        scanf("%lld", &dis[i]);
    }
    std::sort(dis.begin(), dis.end());
    long double avgx = 0, avgy = 0, avgx2 = 0, avgy2 = 0, avgdis = 0;
    for (int i = 0; i < n; ++ i){
        avgx += x[i];
        avgy += y[i];
        avgx2 += sqr(x[i]);
        avgy2 += sqr(y[i]);
        avgdis += dis[i];
    }
    long double r = avgdis / n + sqr(avgx / n) + sqr(avgy / n) - avgx2 / n - avgy2 / n;
    C basic(P(avgx / n, avgy / n), r);
    std::vector <P> points;
    using pii = std::pair <int, int>;
    std::vector <pii> ans;
    for (int i = 0; i < n; ++ i){
        C circle(P(x[0], y[0]), dis[i]);
        auto ps = isCC(circle, basic);
        for (auto p : ps){
            int xx = std::round(p.x), yy = std::round(p.y);
            const int S = 10;
            for (int u = xx - S; u <= xx + S; ++ u){
                for (int v = yy - S; v <= yy + S; ++ v){
                    if (sqr(u - x[0]) + sqr(v - y[0]) == dis[i]){
                        ans.emplace_back(u, v);
                    }
                }
            }
        }
    }
    std::sort(ans.begin(), ans.end());
    ans.erase(std::unique(ans.begin(), ans.end()), ans.end());
    std::vector <pii> ret;
    for (auto p : ans){
        std::vector <ll> real_dis(n);
        for (int i = 0; i < n; ++ i){
            real_dis[i] = sqr(p.first - x[i]) + sqr(p.second - y[i]);
        }
        std::sort(real_dis.begin(), real_dis.end());
        if (dis == real_dis){
            ret.push_back(p);
        }
    }
    printf("%d", (int) ret.size());
    for (auto p : ret){
        printf(" %d %d", p.first, p.second);
    }
    puts("");
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i){
        scanf("%d%d", &x[i], &y[i]);
    }
    int test;
    scanf("%d", &test);
    while (test --){
        solve();
    }
    return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1600002;
typedef long long ll;
const long double eps=1000,Eps=1e-8;
struct Uzi{
	int x,y;
	bool operator<(const Uzi &k)const{
		if(k.x!=x)
			return k.x>x;
		return k.y>y;
	}
}p[N];
int n,m,x[N],y[N],i,cnt,ans;
long long d[N],D[N],s8,s9;
long double Cx,Cy,Sxy,Sd,R;
void init(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d%d",x+i,y+i),Cx+=x[i],Cy+=y[i];
	Cx/=n;Cy/=n;
	for(i=1;i<=n;i++)
		Sxy+=(Cx-x[i])*(Cx-x[i])+(Cy-y[i])*(Cy-y[i]);
}
void Check(ll X,ll Y){
	int i;
	if(fabs((X-Cx)*(X-Cx)+(Y-Cy)*(Y-Cy)-R)>eps)
		return;
	//printf("%lld %lld\n",X,Y);
	for(i=1;i<=n;i++)
		D[i]=1ll*(X-x[i])*(X-x[i])+1ll*(Y-y[i])*(Y-y[i]);
	sort(D+1,D+n+1);
	for(i=1;i<=n;i++)
		if(D[i]!=d[i])
			return;
	cnt++;
	p[cnt]=(Uzi){(int)X,(int)Y};
}
void GetNode(long double X1,long double Y1,long double R1,long double X2,long double Y2,long double R2,long double R12,long double R22){
	long double dis=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
	long double dis2=(X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2);
	//printf("   %lf %lf %lf\n",dis,R1,R2);
	if(dis>R1+R2+Eps)return;
	long double t=atan2(Y2-Y1,X2-X1);
	long double p=(R12+dis2-R22)/2/R1/dis;
	if(p>1)p=1;
	if(p<-1)p=-1;
	long double k=acos(p);
	long double X3=X1+R1*cos(t+k);
	long double Y3=Y1+R1*sin(t+k);
	//printf("%.6Lf %.6Lf %.6Lf %.6Lf %.6Lf %.6Lf %.6Lf\n",R1,R2,dis,t,k,X3,Y3);
	int u,v;
	if(fabs(X3)<(1e10)&&fabs(Y3)<(1e10)){
		for(u=-5;u<=5;u++)
			for(v=-5;v<=5;v++){
				Check((ll)(X3+u),(ll)(Y3+v));
				//Check((ll)(X3+u+0.6),(ll)(Y3+v+0.6));
			}
	}
	long double X4=X1+R1*cos(t-k);
	long double Y4=Y1+R1*sin(t-k);
	if(fabs(X4)<(1e10)&&fabs(Y4)<(1e10)){
		for(u=-5;u<=5;u++)
			for(v=-5;v<=5;v++){
				Check((ll)(X4+u),(ll)(Y4+v));
				//Check((ll)(X4+u+0.6),(ll)(Y4+v+0.6));
			}
	}
}
void work(){
	cnt=0;Sd=0;ans=0;
	s8=s9=0;
	for(i=1;i<=n;i++){
		scanf("%lld",d+i);
		s8+=d[i]/1000000000;
		s9+=d[i]%1000000000;
	}
	sort(d+1,d+n+1);
	d[0]=233333333;
	R=(((long double)s8)*((long double)1000000000)+((long double)s9)-Sxy)/n;
	//printf("%lf %lf %lf\n\n",R,Cx,Cy);
	for(i=1;i<=n;i++)
		if(d[i]!=d[i-1])
			GetNode(Cx,Cy,sqrt(R),x[1],y[1],sqrt(d[i]),R,d[i]);
	p[0].x=p[0].y=192608170;
	sort(p+1,p+cnt+1);
	for(i=1;i<=cnt;i++)
		if(p[i].x!=p[i-1].x||p[i].y!=p[i-1].y)
			ans++;
	printf("%d ",ans);
	for(i=1;i<=cnt;i++)
		if(p[i].x!=p[i-1].x||p[i].y!=p[i-1].y)
			printf("%d %d ",p[i].x,p[i].y);
	cout << '\n';
}
int main(){
	init();
	cin >> m;
	while(m--) work();
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const int mn=1e5+10;
ll x[mn],y[mn],d[mn];
inline ld sq(ld x){return x*x;}
inline ll sq(ll x){return x*x;}
ld eps=1e-8;
ld i1x,i1y,i2x,i2y;
bool intersect(ld x1,ld y1,ld r1,ld x2,ld y2,ld r2,ld r12,ld r22){
    if(r12==0){
        i1x=i2x=x1,i1y=i2y=y1;
        return 1;
    }
    ld dis=sqrt(sq(x1-x2)+sq(y1-y2));
    ld dis2=sq(x1-x2)+sq(y1-y2);
    if(dis>r1+r2+eps)return 0;
    ld t=atan2(y2-y1,x2-x1);
    ld p=(r12+dis2-r22)/2/r1/dis;
    if(p>1)p=1;
    if(p<-1)p=-1;
    ld k=acos(p);
    i1x=x1+r1*cos(t+k);
    i1y=y1+r1*sin(t+k);
    i2x=x1+r1*cos(t-k);
    i2y=y1+r1*sin(t-k);
    return 1;
}
vector<pll>ans;
void idk(ll xx,ll yy,int i,int n){
    if(sq(xx-x[0])+sq(yy-y[0])!=d[i])return;
    vector<ll>v;
    for(int j=0;j<n;j++)v.push_back(sq(xx-x[j])+sq(yy-y[j]));
    sort(v.begin(),v.end());
    for(int j=0;j<n;j++)if(d[j]!=v[j])return;
    ans.push_back({xx,yy});
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%lld%lld",x+i,y+i);
    ld sx=0,sy=0,sxy=0;
    for(int i=0;i<n;i++)sx+=x[i],sy+=y[i];
    ld cx=sx/n,cy=sy/n;
    for(int i=0;i<n;i++)sxy+=sq(cx-x[i])+sq(cy-y[i]);
    int m;
    scanf("%d",&m);
    while(m--){
        ans.clear();
        for(int i=0;i<n;i++)scanf("%lld",d+i);
        ll h1=0,h2=0;
        for(int i=0;i<n;i++)h1+=d[i]/1000000000,h2+=d[i]%1000000000;
        ld r2=(((ld)h1)*1e9+((ld)(h2)-sxy))/n;
        for(int i=0;i<n;i++){
            if(i&&d[i]==d[i-1])continue;
            if(!intersect(x[0],y[0],sqrt(d[i]),cx,cy,sqrt(r2),d[i],r2))continue;
            for(int j=-6;j<=6;j++){for(int k=-6;k<=6;k++) {
                    idk(round(i1x+j), round(i1y+k), i, n);
                    idk(round(i2x+j), round(i2y+k), i, n);
            }}
        }
        sort(ans.begin(),ans.end());
        ans.erase(unique(ans.begin(),ans.end()),ans.end());
        printf("%d",ans.size());
        for(auto hail:ans)printf(" %lld %lld",hail.first,hail.second);
        printf("\n");
    }
}
//1 22424303 86152320


#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;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
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

typedef pair<ll, ll> pll;

namespace decompose {
typedef long double bi;
typedef long long ll;
inline ll cf(ll a,ll b,ll p) {
	a%=p; b%=p; if (a<0) a+=p; if (b<0) b+=p;
	ll d=(ll)floor(a*(long double)b/p+0.5);
	ll ret=(a*b-d*p)%p;
	if (ret<0) ret+=p;
	return ret;
}
ll qp(ll x, ll y, ll m) {
    if (y == 0)
        return 1;
    ll hf = qp(x, y >> 1, m);
    hf = cf(hf, hf, m);
    if (y & 1)
        hf = cf(hf, x, m);
    return hf;
}
pll operator*(pll a, pll b) { return pll(a.fi * b.fi - a.se * b.se, a.fi * b.se + a.se * b.fi); }
pll operator-(pll a, pll b) { return pll(a.fi - b.fi, a.se - b.se); }
pll gcd(pll a, pll b) {
    if (b.fi == 0 && b.se == 0)
        return a;
    bi fm = bi(b.fi) * b.fi + bi(b.se) * b.se, aa = bi(a.fi) * b.fi + bi(a.se) * b.se,
       bb = bi(a.se) * b.fi - bi(a.fi) * b.se;
    pll q(llround(aa / (long double)fm), llround(bb / (long double)fm));
    return gcd(b, a - q * b);
}
pll solve(ll x) {
    ll k;
    while (1) {
        ll n = rand() % (x - 2) + 2;
        if (qp(n, (x - 1) / 2, x) == x - 1) {
            k = qp(n, (x - 1) / 4, x);
            break;
        }
    }
    pll g = gcd(pll(x, 0), pll(k, 1));
    g.fi = abs(g.fi);
    g.se = abs(g.se);
    if (g.fi > g.se)
        swap(g.fi, g.se);
    return g;
}
}

typedef pair<ll,ll> PLL;
namespace Factor {
	const int N=1010000;
	ll C,fac[10010],n,mut,a[1001000];
	int T,cnt,i,l,prime[N],p[N],psize,_cnt;
	ll _e[100],_pr[100];
	vector<ll> d;
	inline ll mul(ll a,ll b,ll p) {
		if (p<=1000000000) return a*b%p;
		else if (p<=1000000000000ll) return (((a*(b>>20)%p)<<20)+(a*(b&((1<<20)-1))))%p;
		else {
			ll d=(ll)floor(a*(long double)b/p+0.5);
			ll ret=(a*b-d*p)%p;
			if (ret<0) ret+=p;
			return ret;
		}
	}
	void prime_table(){
		int i,j,tot,t1;
		for (i=1;i<=psize;i++) p[i]=i;
		for (i=2,tot=0;i<=psize;i++){
			if (p[i]==i) prime[++tot]=i;
			for (j=1;j<=tot && (t1=prime[j]*i)<=psize;j++){
				p[t1]=prime[j];
				if (i%prime[j]==0) break;
			}
		}
	}
	void init(int ps) {
		psize=ps;
		prime_table();
	}
	ll powl(ll a,ll n,ll p) {
		ll ans=1;
		for (;n;n>>=1) {
			if (n&1) ans=mul(ans,a,p);
			a=mul(a,a,p);
		}
		return ans;
	}
	bool witness(ll a,ll n) {
		int t=0;
		ll u=n-1;
		for (;~u&1;u>>=1) t++;
		ll x=powl(a,u,n),_x=0;
		for (;t;t--) {
			_x=mul(x,x,n);
			if (_x==1 && x!=1 && x!=n-1) return 1;
			x=_x;
		}
		return _x!=1;
	}
	bool miller(ll n) {
		if (n<2) return 0;
		if (n<=psize) return p[n]==n;
		if (~n&1) return 0;
		for (int j=0;j<=7;j++) if (witness(rand()%(n-1)+1,n)) return 0;
		return 1;
	}
	ll gcd(ll a,ll b) {
		ll ret=1;
		while (a!=0) {
			if ((~a&1) && (~b&1)) ret<<=1,a>>=1,b>>=1;
			else if (~a&1) a>>=1; else if (~b&1) b>>=1;
			else {
				if (a<b) swap(a,b);
				a-=b;
			}
		}
		return ret*b;
	}
	ll rho(ll n) {
		for (;;) {
			ll X=rand()%n,Y,Z,T=1,*lY=a,*lX=lY;
			int tmp=20;
			C=rand()%10+3;
			X=mul(X,X,n)+C;*(lY++)=X;lX++;
			Y=mul(X,X,n)+C;*(lY++)=Y;
			for(;X!=Y;) {
				ll t=X-Y+n;
				Z=mul(T,t,n);
				if(Z==0) return gcd(T,n);
				tmp--;
				if (tmp==0) {
					tmp=20;
					Z=gcd(Z,n);
					if (Z!=1 && Z!=n) return Z;
				}
				T=Z;
				Y=*(lY++)=mul(Y,Y,n)+C;
				Y=*(lY++)=mul(Y,Y,n)+C;
				X=*(lX++);
			}
		}
	}
	void _factor(ll n) {
		for (int i=0;i<cnt;i++) {
			if (n%fac[i]==0) n/=fac[i],fac[cnt++]=fac[i];}
		if (n<=psize) {
			for (;n!=1;n/=p[n]) fac[cnt++]=p[n];
			return;
		}
		if (miller(n)) fac[cnt++]=n;
		else {
			ll x=rho(n);
			_factor(x);_factor(n/x);
		}
	}
	void dfs(ll x,int dep) {
		if (dep==_cnt) d.pb(x);
		else {
			dfs(x,dep+1);
			for (int i=1;i<=_e[dep];i++) dfs(x*=_pr[dep],dep+1);
		}
	}
	void norm() {
		sort(fac,fac+cnt);
		_cnt=0;
		rep(i,0,cnt) if (i==0||fac[i]!=fac[i-1]) _pr[_cnt]=fac[i],_e[_cnt++]=1;
			else _e[_cnt-1]++;
	}
	vector<ll> getd() {
		d.clear();
		dfs(1,0);
		return d;
	}
	vector<ll> factor(ll n) {
		cnt=0;
		_factor(n);
		norm();
		return getd();
	}
	vector<PLL> factorG(ll n) {
		cnt=0;
		_factor(n);
		norm();
		vector<PLL> d;
		rep(i,0,_cnt) d.pb(mp(_pr[i],_e[i]));
		return d;
	}
	pll operator * (const pll &a, const pll &b) {
		return mp(a.fi*b.fi-a.se*b.se,a.fi*b.se+a.se*b.fi);
	}
	vector<pll> gao(ll n) {
		cnt=0;
		_factor(n);
		norm();
		vector<PLL> d;
		d.pb(mp(1,0));
		rep(i,0,_cnt) {
			vector<PLL> d2;
			if (_pr[i]%4==3) {
				pll x(1,0);
				if (_e[i]%2) return vector<pll>(0);
				rep(j,0,_e[i]/2) x=x*pll(_pr[i],0);
				for (auto p:d) d2.pb(p*x);
			} else if (_pr[i]%4==2) {
				pll x(1,0);
				rep(j,0,_e[i]) x=x*pll(1,1);
				for (auto p:d) d2.pb(p*x);
			} else {
				pll dd=decompose::solve(_pr[i]);
				pll a=dd,b=dd; b.se*=-1;
				vector<pll> mt;
				rep(j,0,_e[i]+1) {
					pll y(1,0);
					rep(c,0,j) y=y*a;
					rep(c,j,_e[i]) y=y*b;
					mt.pb(y);
				}
				for (auto p:d) for (auto q:mt) d2.pb(p*q);
			}
			d=d2;
		}
		set<pll> hs;
		for (auto p:d) {
			hs.insert(mp(p.fi,p.se));
			hs.insert(mp(p.fi,-p.se));
			hs.insert(mp(-p.fi,p.se));
			hs.insert(mp(-p.fi,-p.se));
			hs.insert(mp(p.se,p.fi));
			hs.insert(mp(p.se,-p.fi));
			hs.insert(mp(-p.se,p.fi));
			hs.insert(mp(-p.se,-p.fi));
		}
		d=vector<pll>(all(hs));
		return d;
	}

	bool is_primitive(ll a,ll p) {
		assert(miller(p));
		vector<PLL> D=factorG(p-1);
		rep(i,0,SZ(D)) if (powl(a,(p-1)/D[i].fi,p)==1) return 0;
		return 1;
	}
}

const int N=101000;
ll d[N];
int n,m,x[N],y[N],ord[N];
ll dis[N];
void gao() {
	unordered_map<ll,int> hs;
	set<pll> ans;
	rep(i,0,n) scanf("%lld",d+i);
	rep(i,0,n) hs[d[i]]++;
	rep(i,0,n) ord[i]=i;
	random_shuffle(ord,ord+n);

	ll hv1=0,hv2=0,hv3=0,hv4=0;
	rep(i,0,n) {
		hv1=(hv1+(ll)x[i]*x[i]+(ll)y[i]*y[i])%mod;
		hv2=(hv2+x[i])%mod;
		hv3=(hv3+y[i])%mod;
		hv4=(hv4+d[i])%mod;
	}
	auto dd=Factor::gao(d[rnd(n)]);
	auto check=[&](ll px,ll py) {
		auto distTo=[&](int id) {
			return (px-x[id])*(px-x[id])+(py-y[id])*(py-y[id]);
		};
		ll z=(n*px%mod*px+n*py%mod*py+hv1-2*hv2*px-2*hv3*py)%mod;
		if (z<0) z+=mod;
		if (z!=hv4) return 0;
		rep(i,0,n) {
			dis[i]=distTo(ord[i]);
			if (hs[dis[i]]==0) {
				rep(j,0,i) ++hs[dis[j]];
				return 0;
			}
			--hs[dis[i]];
		}
		rep(i,0,n) ++hs[dis[i]];
		return 1;
	};
	rep(i,0,n) for (auto p:dd) {
		ll px=x[i]+p.fi,py=y[i]+p.se;
		if (check(px,py)) ans.insert(mp(px,py));
	}

	printf("%d",SZ(ans));
	for (auto p:ans) printf(" %lld %lld",p.fi,p.se);
	puts("");
}

int main() {
	Factor::init(100000);
	scanf("%d",&n);
	rep(i,0,n) scanf("%d%d",x+i,y+i);
	scanf("%d",&m);
	rep(i,0,m) gao();
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
const int maxn=505;
int p;
int po(int a,int b)
{
    if(b==0) return 1;
    if(b==1) return a;
    if(b%2==0)
    {
        int u=po(a,b/2);
        return (u*u)%p;
    }
    else
    {
        int u=po(a,b-1);
        return (a*u)%p;
    }
}
int inv(int x)
{
    return po(x,p-2);
}
int fact[maxn];
int invf[maxn];
int c[maxn][maxn];
int c1(int n,int k)
{
    int ans=fact[n];
    ans*=invf[k];
    ans%=p;
    ans*=invf[n-k];
    ans%=p;
    return ans;
}
int c(int n,int k)
{
    return c[n][k];
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    cin>>p;
    int u1=1;
    for(int i=0;i<maxn;++i)
    {
        fact[i]=u1;
        invf[i]=inv(fact[i]);
        u1*=(i+1);
        u1%=p;
    }
    int c[n][n];
    for(int i=0;i<maxn;++i)
    {
        for(int j=0;j<maxn;++j)
        {
            if(i>=j) c[i][j]=c1(i,j);
        }
    }
    int dp1[n+1];
    dp1[0]=1;
    int dp2[n+1];
    dp2[0]=0;
    for(int i=1;i<=n;++i)
    {
        dp1[i]=0;
        dp2[i]=0;
        for(int j=0;j<i;++j)
        {
            dp1[i]+=((((dp1[j]*dp1[i-1-j])%p)*(i+1))%p)*c(i-1,j);
            dp2[i]+=((((dp1[j]*dp2[i-j-1])%p)*(i+1)+((dp2[j]*dp1[i-j-1])%p)*(i+1)+((dp1[j]*dp1[i-1-j])%p)*((((i-1-j)*(i-j)+j*(j+1))/2)%p))%p)*c(i-1,j);
            dp2[i]%=p;
            dp1[i]%=p;
            //cout<<dp1[i]<<" "<<dp2[i]<<endl;
        }
    }
    //cout<<dp2[2]<<endl;
    int dp3[n+1][m+1];
    int dp4[n+1][m+1];
    for(int i=0;i<=n;++i)
    {
        for(int j=0;j<=m;++j)
        {
            dp3[i][j]=0;
            dp4[i][j]=0;
        }
    }
    for(int i=0;i<=n;++i)
    {
        for(int j=0;j<=i;++j)
        {
            for(int k=0;k<=j;++k)
            {
                 if(k!=j)
                 dp3[i][j]+=((dp3[i-k-1][j-k]*dp1[k])%p)*c(j,k);
                 else
                 dp3[i][j]+=dp1[k];
                 dp3[i][j]%=p;
                 if(k!=j)
                 dp4[i][j]+=((dp4[i-k-1][j-k]*dp1[k]+dp2[k]*dp3[i-k-1][j-k])%p)*c(j,k);
                 else
                 dp4[i][j]+=dp2[k];
                 dp4[i][j]%=p;
            }
        }
    }
    cout<<dp4[n][m];
    return 0;
}


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

using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!

using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;

// pairs
#define mp make_pair
#define f first
#define s second

// vectors
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

// helper funcs
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down

tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

tcTU> T fstTrue(T lo, T hi, U f) {
	hi ++; assert(lo <= hi); // assuming f is increasing
	while (lo < hi) { // find first index such that f is true
		T mid = lo+(hi-lo)/2;
		f(mid) ? hi = mid : lo = mid+1;
	}
	return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
	lo --; assert(lo <= hi); // assuming f is decreasing
	while (lo < hi) { // find first index such that f is true
		T mid = lo+(hi-lo+1)/2;
		f(mid) ? lo = mid : hi = mid-1;
	}
	return lo;
}
tcT> void remDup(vector<T>& v) { // sort and remove duplicates
	sort(all(v)); v.erase(unique(all(v)),end(v)); }
tcTU> void erase(T& t, const U& u) { // don't erase
	auto it = t.find(u); assert(it != end(t));
	t.erase(it); } // element that doesn't exist from (multi)set

// INPUT
#define tcTUU tcT, class ...U
tcT> void re(complex<T>& c);
tcTU> void re(pair<T,U>& p);
tcT> void re(vector<T>& v);
tcT, size_t SZ> void re(AR<T,SZ>& a);

tcT> void re(T& x) { cin >> x; }
void re(db& d) { str t; re(t); d = stod(t); }
void re(ld& d) { str t; re(t); d = stold(t); }
tcTUU> void re(T& t, U&... u) { re(t); re(u...); }

tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; }
tcTU> void re(pair<T,U>& p) { re(p.f,p.s); }
tcT> void re(vector<T>& x) { trav(a,x) re(a); }
tcT, size_t SZ> void re(AR<T,SZ>& x) { trav(a,x) re(a); }
tcT> void rv(int& n, vector<T>& x) { re(n); x.rsz(n); trav(a,x) re(a); }

// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
	#ifdef LOCAL
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}
tcT> str ts(complex<T> c) {
	stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
	str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
	res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
	str res = ""; F0R(i,SZ) res += char('0'+b[i]);
	return res; }
tcTU> str ts(pair<T,U> p);
tcT> str ts(T v) { // containers with begin(), end()
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;

	#endif
}
tcTU> str ts(pair<T,U> p) {
	#ifdef LOCAL
		return "("+ts(p.f)+", "+ts(p.s)+")";
	#else
		return ts(p.f)+" "+ts(p.s);
	#endif
}

// OUTPUT
tcT> void pr(T x) { cout << ts(x); }
tcTUU> void pr(const T& t, const U&... u) {
	pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
tcTUU> void ps(const T& t, const U&... u) {
	pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
	cerr << ts(t); if (sizeof...(u)) cerr << ", ";
	DBG(u...); }
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
	#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif

// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(str s = "") {
	unsyncIO();
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

/**
 * Description: Barrett reduction computes $a \% b$ about 4 times faster than usual
 	* where $b$ is constant but not known at compile time. Fails for $b = 1$.
 * Source: KACTL
 	* https://github.com/kth-competitive-programming/kactl/blob/master/content/various/FastMod.h
 	* https://en.wikipedia.org/wiki/Barrett_reduction
 * Verification: http://www.usaco.org/index.php?page=viewproblem2&cpid=1045
 */

typedef unsigned long long ul; typedef __uint128_t L;
struct ModFast {
	ul b,m; ModFast(ul b) : b(b), m(ul((L(1)<<64)/b)) {}
	ul reduce(ul a) {
		ul q = (ul)((L(m)*a)>>64), r = a-q*b;
		return r>=b?r-b:r; }
};

ModFast MF(1);

/**
 * Description: modular arithmetic operations
 * Source:
	* KACTL
	* https://codeforces.com/blog/entry/63903
	* https://codeforces.com/contest/1261/submission/65632855 (tourist)
	* https://codeforces.com/contest/1264/submission/66344993 (ksun)
	* also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal)
 * Verification:
	* https://open.kattis.com/problems/modulararithmetic
 */

template<int RT> struct mint {
	// static const int mod = MOD;
	static constexpr mint rt() { return RT; } // primitive root for FFT
	int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
	mint() { v = 0; }
	mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
		if (v < 0) v += MOD; }
	friend bool operator==(const mint& a, const mint& b) {
		return a.v == b.v; }
	friend bool operator!=(const mint& a, const mint& b) {
		return !(a == b); }
	friend bool operator<(const mint& a, const mint& b) {
		return a.v < b.v; }
	friend void re(mint& a) { ll x; re(x); a = mint(x); }
	friend str ts(mint a) { return ts(a.v); }

	mint& operator+=(const mint& m) {
		if ((v += m.v) >= MOD) v -= MOD;
		return *this; }
	mint& operator-=(const mint& m) {
		if ((v -= m.v) < 0) v += MOD;
		return *this; }
	mint& operator*=(const mint& m) {
		v = MF.reduce((ll)v*m.v); return *this; }
	mint& operator/=(const mint& m) { return (*this) *= inv(m); }
	friend mint pow(mint a, ll p) {
		mint ans = 1; assert(p >= 0);
		for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans; }
	friend mint inv(const mint& a) { assert(a.v != 0);
		return pow(a,MOD-2); }

	mint operator-() const { return mint(-v); }
	mint& operator++() { return *this += 1; }
	mint& operator--() { return *this -= 1; }
	friend mint operator+(mint a, const mint& b) { return a += b; }
	friend mint operator-(mint a, const mint& b) { return a -= b; }
	friend mint operator*(mint a, const mint& b) { return a *= b; }
	friend mint operator/(mint a, const mint& b) { return a /= b; }
};

typedef mint<5> mi; // 5 is primitive root for both common mods
typedef vector<mi> vmi;
typedef pair<mi,mi> pmi;
typedef vector<pmi> vpmi;

vector<vmi> scmb; // small combinations
void genComb(int SZ) {
	scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1;
	FOR(i,1,SZ) F0R(j,i+1)
		scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0);
}

mi ways[501], mad[501];
mi WAYS[501][501], MAD[501][501];
int N,M;

int main() {
	setIO(); re(N,M,MOD);
	MF = ModFast(MOD);
	genComb(N+5);
	ways[0] = 1;
	FOR(n,1,N+1) {
		FOR(i,1,n+1) {
			ways[n] += (n+1)*ways[i-1]*ways[n-i]*scmb[n-1][i-1];
			FOR(j,1,i+1) {
				mad[n] += (j-1)*ways[i-1]*ways[n-i]*scmb[n-1][i-1];
				mad[n] += mad[i-1]*ways[n-i]*scmb[n-1][i-1];
				mad[n] += mad[n-i]*ways[i-1]*scmb[n-1][i-1];
			}
			FOR(j,1,n-i+2) {
				mad[n] += (j-1)*ways[i-1]*ways[n-i]*scmb[n-1][i-1];
				mad[n] += mad[i-1]*ways[n-i]*scmb[n-1][i-1];
				mad[n] += mad[n-i]*ways[i-1]*scmb[n-1][i-1];
			}
		}
	}
	F0R(n,N+1) WAYS[n][0] = 1;
	FOR(n,1,N+1) FOR(m,1,n+1) {
		if (n == m) {
			WAYS[n][m] = ways[n];
			MAD[n][m] = mad[n];
		} else {
			WAYS[n][m] = WAYS[n-1][m];
			MAD[n][m] = MAD[n-1][m];
			FOR(l,1,m+1) {
				WAYS[n][m] += WAYS[n-l-1][m-l]*ways[l]*scmb[m][l];
				MAD[n][m] += (WAYS[n-l-1][m-l]*mad[l]+ways[l]*MAD[n-l-1][m-l])*scmb[m][l];
			}
		}
	}
	dbg(WAYS[N][M],MAD[N][M]);
	ps(MAD[N][M]);
	// you should actually read the stuff at the bottom
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define debug(x) cout << #x" = " << (x) << endl;
inline ll read(){
	register ll x=0,f=1;register char c=gc();
	for(;!isdigit(c);c=gc())if(c=='-')f=-1;
	for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void wri(ll x){write(x),pc(' ');}
void writeln(ll x){write(x),pc('\n');}
const int mod = 998244353;
const int maxn = 1<<19|233;
int n,k,f[1<<21];
int main(){
	n = read(),k = read();
	if(k==1){writeln(3);return ;}
	Rep(i,1,n) f[i]=i;
	Rep(i,1,n) for(int j=i+i;j<=n;j+=i) f[j]-=f[i];
	sort(f+1,f+1+n);ll ans=0;Rep(i,1,k+2) ans+=f[i];writeln(ans);
	return 0;
}


#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
using namespace std;
int n,k,phi[1000005];
ll ans;
int main(){
	scanf("%d%d",&n,&k);
	if (k==1) return puts("3"),0;
	For(i,1,n) phi[i]=i;
	For(i,2,n) if (phi[i]==i)
		For(j,1,n/i) phi[i*j]-=phi[i*j]/i;
	sort(phi+1,phi+n+1);
	For(i,1,k+2) ans+=phi[i];
	printf("%lld\n",ans);
}


//https://codeforces.com/contest/1208/problem/G
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+100;
int phi[N],p[N/10];
bool vis[N];
int tot=0;
void eular(int n)
{
	for(int i=2;i<=n;i++)
	{
		if(vis[i]==0) p[tot++]=i,phi[i]=i-1;
		for(int j=0;j<tot&&i*p[j]<=n;j++)
		{
			vis[i*p[j]]=1;
			if(i%p[j]==0)
			{
				phi[i*p[j]]=phi[i]*p[j];
				break;
			}
			else phi[i*p[j]]=phi[i]*phi[p[j]];
		}
	}
}
int main()
{
	eular(N-10);
	int n,k;
	cin>>n>>k;
	if(k==1) cout<3<<endl;
	else
	{
		ll ans=0;
		sort(phi+3,phi+1+n);
		for(int i=1;i<=k;i++) ans+=phi[i+2];
		cout<<ans+2<<endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

const int MAX = 1e6 + 3;
int dp[MAX];

void precalc(int n) {
    for(int i = 1; i <= n; ++i) {
        dp[i] = i;
    }
    for(int i = 2; i <= n; ++i) {
        if(dp[i] == i) {
            --dp[i];
            for(int j = i * 2; j <= n; j += i) {
                dp[j] = dp[j] / i * (i - 1);
            }
        }
    }
}

void solve() {
    int n, k;
    cin >> n >> k;
    if(n == 3) {
        cout << 3 << endl;
        return;
    }
    precalc(n);
    sort(dp, dp + n + 1);

    long long res = 0;
    for(int i = 1; i <= k + 2; ++i) {
        res += dp[i];
    }

    cout << res << endl;
}

int main() {
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
map<tuple<long long ,long long,long long>, long long >mem ;
long long dp(long long l,long long r,long long dis,long long back)
{
    back=min(back,r);
    back=max(back,l);
    if(mem[make_tuple(r-l,dis,back-l)])
        return mem[make_tuple(r-l,dis,book-l)];
    long long mid=l+(r-l)/2;
    if(mid-l>dis||mid-l==dis && mid<=back)
        return mem[make_tuple(r-l,dis,back-l)]=dp(l,mid,dis,back)+dp(mid,r,dis,back)+1;
    return mem[make_tuple(r-l,dis,back-l)]=0;
}
int main()
{
    long long n,k;
    scanf("%I64d %I64d",&n,&k);
    if(k==1)
    {
        printf("1\n");
        return 0;
    }
    if(k==2)
    {
        printf("%d\n",n);
        return 0;
    }
    long long x=1,y=n;
    while(x<=y)
    {
        long long mid=x+(y-x)/2;
        if(dp(x,y,mid,n)>=k) y=mid-1;
        else x=mid+1;
    }
    long long anslen=x-1;
    x=1,y=n;
    while(x<=y)
    {
        long long mid=x+(y-x)/2;
        if(dp(x,y,anslen,mid)>=k) y=mid-1;
        else x=mid+1;
    }
    printf("%I64d\n",x-1);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define 2333 6666
using namespace std;
ll get(ll x,ll v){
	if (x<=v*2) return 0;
	ll s1=1,s2=0,p1=x,p2=x-1,ans=0;
	for (;p2>=v*2;p1/=2,p2=p1-1){
		if (p2==v*2) return s1+ans;
		ans+=s1+s2;
		if (p1&1) s1=s1*2+s2;
		else s2=s2*2+s1;
	}
	return ans;
}
ll solve(ll l,ll r,ll p,ll k){
	ll mid=(l+r)/2;
	if (k==1) return mid;
	ll tmp=get(mid-l,p),Tmp=get(r-mid,p+1);
	if (k>tmp+Tmp+1) return solve(mid+1,r,p,k-tmp-1);
	return solve(l,mid-1,p,k-Tmp-1);
}
int main(){
	ll n,k;
	scanf("%lld%lld",&n,&k);
	if (k==1) return puts("1"),0;
	if (k==2) return printf("%lld",n),0;
	n-=2; k-=2;
	ll l=0,r=n/2+1,mid;
	while (r-l>1){
		mid=(l+r)/2;
		if (get(n,mid)<k) r=mid;
		else l=mid;
	}
	printf("%lld\n",solve(1,n,l,k)+1);
}


#include <bits/stdc++.h>

#define f(_i, _n) for (auto _i = 0; _i < _n; _i++)
#define F(_i, _k, _n) for (auto _i = _k; _i < _n; _i++)
#define fr(_i, _k, _n) for (auto _i = _k; _i < _n; _i++)
#define r(_t, _n)     _t _n;     cin >> _n;
#define ra(_type, _name, _len)_type _name[_len]; f(_i, _len)    cin >> _name[_len];
#define mp make_pair
#define re return
#define takedown re 0;
#define fi first
#define se second
#ifdef FairlyLocal
    #define deb cerr
#else
    #define deb GetCE :(
#endif
#define pb push_back
#define fill(_a, _n) memset(_a, _n, sizeof(_a))
#define all(_v) _v.begin(), _v.end()
#define vi std::vector<int>

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;

#ifndef FairlyLocal
  class fastio {
  public:
    fastio() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    }
  } __fastio;
#endif

map<ll, ll> go(int n){
	map<ll, ll> m[100];
	m[0][-n] = 1;
	ll cc = 0;
  map<ll, ll> ans;
  ans[-(n - 1) / 2]++;
  int ok = 1;
  while(ok){
    ok = 0;
		for(auto it : m[cc]){
			ll w = -it.fi;
      // deb << w << ' ' << it.se << '\n';
      if((w - 1) / 2 > 0) {
        ok = 1;
        m[cc + 1][-(w - 1) / 2] += it.se;
        ans[-((w - 1) / 2 - 1) / 2] += it.se;
      }
      if(w / 2 > 0) {
        m[cc + 1][-w / 2] += it.se;
        ans[-(w / 2 - 1) / 2] += it.se;
        ok = 1;
      }
    }
		cc++;
	}
  // deb << '\n';
  // for(auto j : ans) deb << -j.fi << ' ' << j.se << '\n';
  re ans;
}

int n, k;

int solve() {
  int pos = 1, sz = 0;
  auto aa = go(n);
  // deb << aa.size() << '\n';
  for(auto it : aa) {
    // deb << it.fi << ' ' << it.se << '\n';
    if(it.se >= k) {
      sz = -it.fi;
      break;
    }
    // deb << k << '\n';
    k -= it.se;
  }
  // deb << sz << ' ' << k << '\n';
  int cursz = (n - 1) / 2;
  while(true) {
    deb << cursz << ' ' << sz << ' ' << pos << ' ' << k << ' ' <<  '\n';
    if(cursz == sz) {
      re pos + sz + k;
    }
    auto a1 = go((cursz - 1) / 2);
    auto a2 = go(cursz / 2);
    if(a1[-sz] >= k) {
      if(cursz == 2) pos++;
      cursz = (cursz - 1) / 2;
    }
    else {
      pos += cursz + 1;
      k -= a1[-sz];
      cursz = cursz / 2;
    }
  }
  re -1;
}

int main(){
  #ifdef FairlyLocal
    freopen(".input", "r", stdin);
    freopen(".output", "w", stdout);
    freopen(".debug", "w", stderr);
  #endif
  cin >> n >> k;
  if(k == 1) {
    cout << 1;
    re 0;
  }
  if(k == 2) {
    cout << n;
    re 0;
  }
  k -= 2;
  n -= 2;
  cout << solve();
}


#include <bits/stdc++.h>

#define f(_i, _n) for (auto _i = 0; _i < _n; _i++)
#define F(_i, _k, _n) for (auto _i = _k; _i < _n; _i++)
#define fr(_i, _k, _n) for (auto _i = _k; _i < _n; _i++)
#define r(_t, _n)     _t _n;     cin >> _n;
#define ra(_type, _name, _len)_type _name[_len]; f(_i, _len)    cin >> _name[_len];
#define mp make_pair
#define re return
#define takedown re 0;
#define fi first
#define se second
#ifdef FairlyLocal
    #define deb cerr
#else
    #define deb GetCE :(
#endif
#define pb push_back
#define fill(_a, _n) memset(_a, _n, sizeof(_a))
#define all(_v) _v.begin(), _v.end()
#define vi std::vector<int>

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;

#ifndef FairlyLocal
  class fastio {
  public:
    fastio() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    }
  } __fastio;
#endif

map<ll, ll> go(ll n){
	map<ll, ll> m[100];
	m[0][-n] = 1;
	ll cc = 0;
  map<ll, ll> ans;
  ans[-(n - 1) / 2]++;
  int ok = 1;
  while(ok){
    ok = 0;
		for(auto it : m[cc]){
			ll w = -it.fi;
      // deb << w << ' ' << it.se << '\n';
      if((w - 1) / 2 > 0) {
        ok = 1;
        m[cc + 1][-(w - 1) / 2] += it.se;
        ans[-((w - 1) / 2 - 1) / 2] += it.se;
      }
      if(w / 2 > 0) {
        m[cc + 1][-w / 2] += it.se;
        ans[-(w / 2 - 1) / 2] += it.se;
        ok = 1;
      }
    }
		cc++;
	}
  // deb << '\n';
  // for(auto j : ans) deb << -j.fi << ' ' << j.se << '\n';
  re ans;
}

ll n, k;

ll solve() {
  ll pos = 1, sz = 0;
  auto aa = go(n);
  // deb << aa.size() << '\n';
  for(auto it : aa) {
    deb << k << ' ' << it.se << '\n';
    if(it.se >= k) {
      sz = -it.fi;
      break;
    }
    // deb << k << '\n';
    k -= it.se;
  }
  // deb << sz << ' ' << k << '\n';
  ll cursz = (n - 1) / 2;
  ll size = n;
  while(true) {
    if(cursz == sz) {
      re pos + sz + k;
    }
    auto a1 = go((size - 1) / 2);
    auto a2 = go(size / 2);
    // deb << cursz << ' ' << size << ' ' << pos << ' ' << k << ' ' << a1[-sz] << ' ' << a2[-sz] <<  '\n';
    if(a1[-sz] >= k) {
      if(cursz == 2) pos++;
      size = cursz;
      cursz = (cursz - 1) / 2;
    }
    else {
      pos += cursz + 1;
      k -= a1[-sz];
      size = size / 2;
      cursz = (cursz - 1) / 2;
    }
  }
  re -1;
}

int main(){
  #ifdef FairlyLocal
    freopen(".input", "r", stdin);
    freopen(".output", "w", stdout);
    freopen(".debug", "w", stderr);
  #endif
  cin >> n >> k;
  if(k == 1) {
    cout << 1;
    re 0;
  }
  if(k == 2) {
    cout << n;
    re 0;
  }
  k -= 2;
  n -= 2;
  cout << solve();
}


#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }

#define sqr(x) ((x) * (x))
using namespace std;

const int MN = 100111;

struct Str {
    int typ, x;
} a[MN];
int n, m;
long long best, cnt;

bool operator < (const Str& a, const Str& b) {
    if (a.x != b.x) return a.x < b.x;
    return a.typ < b.typ;
}

Str reverse(const Str& a) {
    Str res;
    res.typ = 1 - a.typ;
    res.x = n - a.x;
    return res;
}

void update(long long val, long long c) {
    if (!c) return ;
    if (val > best) {
        best = val;
        cnt = c;
    }
    else if (val == best) {
        cnt += c;
    }
}

int count(int typ) {
    int res = 0;
    FOR(i,1,m)
        if (a[i].typ == typ) ++res;
    return res;
}

long long c3(long long n) {
    return n * (n-1LL) * (n-2LL) / 6;
}

long long c2(long long n) {
    return n * (n-1LL) / 2;
}

void case1() {
    // Case 1: X, Y, Z  (x <= y <= z)
    // Sum = H(X, Y) + H(Y, Z) + H(Z, X)
    //     = 2z - 2x
    // Thus, choose max z and min x
    int nn = -1, ln = -1;
    FOR(i,1,m)
        if (a[i].typ == 0) {
            if (nn < 0) nn = a[i].x; else nn = min(nn, a[i].x);
            if (ln < 0) ln = a[i].x; else ln = max(ln, a[i].x);
        }
    if (nn == ln) {
        update(0, c3(count(0)));
    }
    else {
        long long cnt_min = 0, cnt_max = 0, cnt_mid = 0;
        FOR(i,1,m)
            if (a[i].typ == 0) {
                if (a[i].x == nn) cnt_min++;
                else if (a[i].x == ln) cnt_max++;
                else cnt_mid++;
            }
        update(2*ln - 2*nn,
                cnt_min * cnt_max * cnt_mid
                + c2(cnt_min) * cnt_max
                + cnt_min * c2(cnt_max)
                );
    }
}

void case2() {
    // Case 2: _X, Y, Z
    // Sum = H(_X, Y) + H(Y, Z) + H(_X, Z)
    //     = 2n + 2x - 2y
    long long y = -1, cnt_y = 0, cnt_all = 0;
    FORD(i,m,1) {
        if (a[i].typ == 0) {
            if (a[i].x == y) ++cnt_y;
            else {
                y = a[i].x;
                cnt_y = 1;
            }
            ++cnt_all;
        }
        else {
            update(2*n + 2*a[i].x - 2*y,
                    c2(cnt_y)
                    + cnt_y * (cnt_all - cnt_y)
                  );
        }
    }
}

#define left left_
#define right right_
long long left[MN], right[MN];
void case3() {
    // Case 3: X, _Y, Z
    // Sum = H(X, _Y) + H(_Y, Z) + H(X, Z)
    //     = 2n
    FOR(i,1,m) {
        left[i] = left[i-1];
        if (a[i].typ == 0) ++left[i];
    }
    right[n+1] = 0;
    FORD(i,m,1) {
        right[i] = right[i+1];
        if (a[i].typ == 0) ++right[i];
    }
    FOR(i,2,m-1)
        if (a[i].typ == 1) {
            update(2*n, left[i-1] * right[i+1]);
        }
}

void case4() {
    // Case 4: X, Y, _Z
    // Sum = 2n + 2y - 2z
    long long y = -1, cnt_y = 0, cnt_all = 0;
    FOR(i,1,m) {
        if (a[i].typ == 0) {
            if (a[i].x == y) ++cnt_y;
            else {
                y = a[i].x;
                cnt_y = 1;
            }
            cnt_all++;
        }
        else {
            update(2*n + 2*y - 2*a[i].x,
                    c2(cnt_y)
                    + cnt_y * (cnt_all - cnt_y));
        }
    }
}

void solve() {
    sort(a+1, a+m+1);
    if (!count(0)) return ;
    // Let X = 0^x 1^(n-x)
    // Let _X = 1^x 0^(n-x)
    case1();
    if (!count(1)) return ;
    case2();
    case3();
    case4();
}

int main() {
    ios :: sync_with_stdio(false);
    cout << (fixed) << setprecision(9);
    while (cin >> n >> m) {
        FOR(i,1,m) cin >> a[i].typ >> a[i].x;
        best = -1, cnt = 0;
        solve();
        FOR(i,1,m) a[i] = reverse(a[i]);
        solve();

//        DEBUG(best);
        cout << cnt << endl;
    }
    return 0;
}


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

int setbit(int mask, int bit, int value);
int getbit(int mask, int bit);
ll cnt(int i, int msk_type, int msk_change, int mode);
ll fn(int i, int msk_type, int msk_change, int mode);
int bin_search(int t, int val);
ll get_ham(int small, int big, int msk_type);
void pre_process();
int check(int msk_type);
int check(int msk_type, int msk_change);
ll C(ll x, ll y);

pair<int, int> zero_one[MAX], one_zero[MAX];
int a = 0, b = 0;
int n, l, indx[MAX], diff_indx;
const ll inf = 9999999999;
ll dp[MAX][8][8][2], cnt_dp[MAX][8][8][2];
int bin_ans[MAX], bin_ans2[MAX];

int main()
{
    scanf("%d %d", &l, &n);
    for(int i = 0; i < n; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);

        if(x == 0)
            zero_one[a++].first = y;
        else
            one_zero[b++].first = y;
    }

    pre_process();

    ll ans = 0;
    for(int i = 0; i < 8; i++)
        ans = max(ans, fn(0, i, 0, 0));

    ll ret = 0;
    for(int i = 0; i < 8; i++)
        if(ans == fn(0, i, 0, 0) && check(i))
            ret += cnt(0, i, 0, 0);

    printf("%lld\n", ret);

    return 0;
}

int check(int msk_type, int msk_change)
{
    int a = getbit(msk_type, 0);
    int b = getbit(msk_type, 1);
    int c = getbit(msk_type, 2);

    if(getbit(msk_change, 0)) a = !a;
    if(getbit(msk_change, 1)) b = !b;
    if(getbit(msk_change, 2)) c = !c;

    if(a == b && getbit(msk_change, 0) == 0 && getbit(msk_change, 1) == 1)return 0;
    if(a == c && getbit(msk_change, 0) == 0 && getbit(msk_change, 2) == 1)return 0;
    if(b == c && getbit(msk_change, 1) == 0 && getbit(msk_change, 2) == 1)return 0;

    return 1;
}

int check(int msk_type)
{
    int a = getbit(msk_type, 0);
    int b = getbit(msk_type, 1);
    int c = getbit(msk_type, 2);

    if(a > b || a > c) return 0;

    if(b > c) return 0;

    return 1;
}

ll cnt(int i, int msk_type, int msk_change, int mode)
{
    if(check(msk_type, msk_change) == 0) return 0;
    if(i == diff_indx) return msk_change == 7 ? 1 : 0;

    ll &ret = cnt_dp[i][msk_type][msk_change][mode];
    if(ret != -1)
        return ret;

    ret = 0;

    int nxt = indx[i + 1], curr = indx[i];
    ll g = fn(i, msk_type, msk_change, mode);  ///get max_value

    ///don't change
    if(i < diff_indx - 1)
    {
        if(g == fn(i + 1,  msk_type, msk_change, 0) + get_ham(curr, nxt, msk_type))
            ret += cnt(i + 1, msk_type, msk_change, 0), p++;
    }
    else
    {
        if(g == fn(i + 1, msk_type, msk_change, 0))
            ret += cnt(i + 1, msk_type, msk_change, 0), p++;
    }

    if(mode == 0)
    {
        int v1 = bin_ans[i];
        int v2 = bin_ans2[i];

        for(int x = 1; x < 8; x++)
        {
            int new_msk_type = msk_type;
            int new_msk_change = msk_change;
            int flag = 1, u1 = 0, u2 = 0;

            for(int j = 0; j < 3 && flag; j++)
            {
                if(getbit(x, j) == 1)
                {
                    int t = getbit(msk_type, j);
                    if(t == 0)
                        u1++;
                    else
                        u2++;

                    if(getbit(msk_change, j) == 1 || (t == 0 && v1 == -1) || (t == 1 && v2 == -1))
                        flag = 0;
                    new_msk_type = setbit(new_msk_type, j, !t);
                    new_msk_change = setbit(new_msk_change, j, 1);
                }
            }

            if(u1 > v1 || u2 > v2)
                flag = 0;

            ll fac = C((ll)v1, (ll)u1) * C((ll)v2, (ll)u2);

            if(flag && fn(i, new_msk_type, new_msk_change, 1) == g)
                fn(i, new_msk_type, new_msk_change, 1) == g;
        }
    }

    return ret;
}

ll fn(int i, int msk_type, int msk_change, int mode)
{
    if(i == diff_indx)return msk_change == 7 ? 0 : -inf;

    ll &ret = dp[i][msk_type][msk_change][mode];
    if(ret != -1)
        return ret;

    ret = -inf;

    int nxt = indx[i + 1], curr = indx[i];

    ///don't change
    if(i < diff_indx - 1)
        ret = max(ret, fn(i + 1, msk_type, msk_change, 0) + get_ham(curr, nxt, msk_type));
    else
        ret = max(ret, fn(i + 1, msk_type, msk_change, 0));

    if(mode == 0)
    {
        int v1 = bin_ans[i];
        int v2 = bin_ans2[i];

        for(int x = 1; x < 8; x++)
        {
            int new_msk_type = msk_type;
            int new_msk_change = msk_change;
            int flag = 1, u1 = 0, u2 = 0;

            for(int j = 0; j < 3 && flag; j++)
            {
                if(getbit(x, j) == 1)
                {
                    int t = getbit(msk_type, j);
                    if(t == 0)
                        u1++;
                    else
                        u2++;
                    if(getbit(msk_change, j) == 1 || (t == 0 && v1 == -1) || (t == 1 && v2 == -1))
                        flag = 0;
                    new_msk_type = setbit(new_msk_type, j, !t);
                    new_msk_change = setbit(new_msk_change, j, 1);
                }
            }

            if(u1 > v1 || u2 > v2)
                flag = 0;

            if(flag)
                ret = max(ret, fn(i, new_msk_type, new_msk_change, 1));
        }
    }

    return ret;
}

void pre_process()
{
    memset(dp, -1, sizeof(dp));
    memset(cnt_dp, -1, sizeof(cnt_dp));

    sort(zero_one, zero_one + a);
    sort(one_zero, one_zero + b);

    int k = 0;
    for(int i = 0; i < max(a, b); i++)
    {
        if(i < a)indx[k++] = zero_one[i].first;
        if(i < b)indx[k++] = one_zero[i].first;
    }

    indx[k++] = 0, indx[k++] = l;
    sort(indx, indx + k);

    int j = 0, prev = -1, i = 0, tmp = 0;

    while(i < k)
    {
        if(prev != indx[i])
        {
            indx[j++] = indx[i];
            prev = indx[i];
        }

        i++;
    }

    diff_indx = j;

    j = 0, prev = zero_one[0].first, i = 0, tmp = 0;
    while(i < a)
    {
        if(prev != zero_one[i].first)
        {
            zero_one[j].first = prev, zero_one[j++].second = tmp;
            tmp = 1;
            prev = zero_one[i].first;

        }
        else
            tmp++;

        i++;
    }

    zero_one[j].first = prev, zero_one[j++].second = tmp;
    a = j;

    j = 0, prev = one_zero[0].first, i = 0, tmp = 0;
    while(i < b)
    {
        if(prev != one_zero[i].first)
        {
            one_zero[j].first = prev, one_zero[j++].second = tmp;
            tmp = 1;
            prev = one_zero[i].first;

        }
        else
            tmp++;

        i++;
    }

    one_zero[j].first = prev, one_zero[j++].second = tmp;
    b = j;

    for(int i = 0; i < diff_indx; i++)
    {
        bin_ans[i] = bin_search(0, indx[i]);
        bin_ans2[i] = bin_search(1, indx[i]);
    }
}

int setbit(int mask, int bit, int value)
{
    if(value == 1)
        return(mask | (1 << bit));
    else
        return (mask&~(1 << bit));
}

int getbit(int mask, int bit)
{
    mask = (mask >> bit) & 1;
    return (mask == 1);
}

ll get_ham(int small, int big, int msk_type)
{
    ll ret = 0;
    int diff = big - small;

    if(getbit(msk_type, 0) != getbit(msk_type, 1)) ///a b
        ret += diff;

    if(getbit(msk_type, 1) != getbit(msk_type, 2)) ///b c
        ret += diff;

    if(getbit(msk_type, 0) != getbit(msk_type, 2)) ///a c
        ret += diff;

    return ret;
}

int bin_search(int t, int val)
{
    int big = (t == 0 ? a : b);
    int small = 0, mid;
    pair<int, int> *arr = (t == 0 ? zero_one : one_zero);

    while(big >= small)
    {
        mid = (small + big) / 2;

        if(arr[mid].first == val)
            return arr[mid].second;

        if(arr[mid].first > val)
            big = mid - 1;
        else
            small = mid + 1;
    }

    return 0;
}

ll C(ll x, ll y)
{
    if(x == 0 || y == 0)
        return 1;

    if(y == 1) return x;

    if(y == 2) return (ll)(x * (x - 1)) / 2;

    if(y == 3) return (ll)(x * (x - 1) * (x - 2)) / 6;
}


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1034567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	int N,M;
	cin >> N >> M;
	map<int,int> C[2];
	int T[] ={0,0};
	for(int i =0; i < M; i++) {
		int t,a;
		cin >> t >> a;
		T[t]++;
		C[t][a] +=1;}

	int ansV =0;
	long long ansP =0;

	// rovnaky typ
	for(int i =0; i < 2; i++) if(!C[i].empty()) {
		int a =2*(C[i].rbegin()->ff-C[i].begin()->ff);
		int x =C[i].begin()->ss, y =C[i].rbegin()->ss;
		long long p =0;
		if(a == 0) p =1LL*x*(x-1)*(x-2)/6;
		else p =x*(x-1)/2*y+y*(y-1)*x/2+x*y*(M-x-y);
		if(ansV < a) {
			ansV =a;
			ansP =0;}
		if(ansV == a) ansP +=p;}

	// rozne typy
	// da sa 2N?
	for(int i =0; i < 2; i++) if(!C[i].empty()) {
		int a =C[i].begin()->ff, b =C[i].rbegin()->ff;
		if(a == b && a == N && N == 1000000000 && k == 0 && C[1].size() > 5 && C[1].rbegin()->ff == N) cout << "aha\n";
		bool win =false;
		ALL_THE(C[1-i],it) if(it->ff >= a && it->ff <= b)
			win =true;

		if(!win) {
			auto it =C[1-i].lower_bound(b), jt =C[1-i].lower_bound(a);
			if(jt != C[1-i].begin()) {
				jt--;
				if(2*N-2*(a-jt->ff) > ansV) {
					ansV =2*N-2*(a-jt->ff);
					ansP =0;}
				if(2*N-2*(a-jt->ff) == ansV) {
					ansP +=1LL*(jt->ss)*(C[i].begin()->ss)*(C[i].begin()->ss-1)/2;
					ansP +=1LL*(jt->ss)*(C[i].begin()->ss)*(T[i]-C[i].begin()->ss);}
				}
			if(it != C[1-i].end()) {
				if(2*N-2*(it->ff-b) > ansV) {
					ansV =2*N-2*(it->ff-b);
					ansP =0;}
				if(2*N-2*(it->ff-b) == ansV) {
					ansP +=1LL*(it->ss)*(C[i].rbegin()->ss)*(C[i].rbegin()->ss-1)/2;
					ansP +=1LL*(it->ss)*(C[i].rbegin()->ss)*(T[i]-C[i].rbegin()->ss);}
				}
			}

		else {
			if(2*N > ansV) {
				ansV =2*N;
				ansP =0;}
			auto it =C[i].begin();
			int p =0;
			ALL_THE(C[1-i],jt) {
				while(it != C[i].end() && it->ff < jt->ff) {
					p +=it->ss;
					it++;}
				if(it != C[i].end() && it->ff == jt->ff)
					ansP +=1LL*(it->ss*(T[i]-p-it->ss)+it->ss*(it->ss-1)/2)*jt->ss;
				ansP +=1LL*p*(T[i]-p)*jt->ss;}
			}
		}

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

// look at my code
// my code is amazing