#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const vector &as); template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") //////////////////////////////////////////////////////////////////////////////// // Barrett struct ModInt { static unsigned M; static unsigned long long NEG_INV_M; static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; } unsigned x; ModInt() : x(0U) {} ModInt(unsigned x_) : x(x_ % M) {} ModInt(unsigned long long x_) : x(x_ % M) {} ModInt(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { const unsigned long long y = static_cast(x) * a.x; const unsigned long long q = static_cast((static_cast(NEG_INV_M) * y) >> 64); const unsigned long long r = y - M * q; x = r - M * (r >= M); return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; unsigned ModInt::M; unsigned long long ModInt::NEG_INV_M; // !!!Use ModInt::setM!!! //////////////////////////////////////////////////////////////////////////////// using Mint = ModInt; // point add, rectangle sum template struct Bit2d { vector xs; vector> yxs; vector> yss; int m; vector ns; vector> bit; Bit2d() {} void book(X x, Y y) { xs.push_back(x); yxs.emplace_back(y, x); } void build() { sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); m = xs.size(); yss.assign(m, {}); sort(yxs.begin(), yxs.end()); for (const auto &yx : yxs) { const X x = yx.second; const Y y = yx.first; const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin(); assert(a < m); assert(xs[a] == x); for (int u = a; u < m; u |= u + 1) yss[u].push_back(y); } ns.assign(m, 0); bit.assign(m, {}); for (int u = 0; u < m; ++u) { yss[u].erase(unique(yss[u].begin(), yss[u].end()), yss[u].end()); ns[u] = yss[u].size(); bit[u].assign(ns[u], 0); } } void add(X x, Y y, T val) { const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin(); assert(a < m); assert(xs[a] == x); for (int u = a; u < m; u |= u + 1) { const int b = lower_bound(yss[u].begin(), yss[u].end(), y) - yss[u].begin(); assert(b < ns[u]); assert(yss[u][b] == y); for (int v = b; v < ns[u]; v |= v + 1) bit[u][v] += val; } } T get(X x0, X x1, Y y0, Y y1) const { const int a0 = lower_bound(xs.begin(), xs.end(), x0) - xs.begin(); const int a1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin(); T ret = 0; for (int u = a0; u; u &= u - 1) ret -= get(u - 1, y0, y1); for (int u = a1; u; u &= u - 1) ret += get(u - 1, y0, y1); return ret; } private: T get(int u, Y y0, Y y1) const { T ret = 0; const int b0 = lower_bound(yss[u].begin(), yss[u].end(), y0) - yss[u].begin(); const int b1 = lower_bound(yss[u].begin(), yss[u].end(), y1) - yss[u].begin(); for (int v = b0; v; v &= v - 1) ret -= bit[u][v - 1]; for (int v = b1; v; v &= v - 1) ret += bit[u][v - 1]; return ret; } }; // T: monoid representing information of an interval. // T() should return the identity. // T(S s) should represent a single element of the array. // T::push(T &l, T &r) should push the lazy update. // T::pull(const T &l, const T &r) should pull two intervals. template struct SegmentTreeRange { int logN, n; vector ts; SegmentTreeRange() : logN(0), n(0) {} explicit SegmentTreeRange(int n_) { for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {} ts.resize(n << 1); } template explicit SegmentTreeRange(const vector &ss) { const int n_ = ss.size(); for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {} ts.resize(n << 1); for (int i = 0; i < n_; ++i) at(i) = T(ss[i]); build(); } T &at(int i) { return ts[n + i]; } void build() { for (int u = n; --u; ) pull(u); } inline void push(int u) { ts[u].push(ts[u << 1], ts[u << 1 | 1]); } inline void pull(int u) { ts[u].pull(ts[u << 1], ts[u << 1 | 1]); } // Applies T::f(args...) to [a, b). template void ch(int a, int b, F f, Args &&... args) { assert(0 <= a); assert(a <= b); assert(b <= n); if (a == b) return; a += n; b += n; for (int h = logN; h; --h) { const int aa = a >> h, bb = b >> h; if (aa == bb) { if ((aa << h) != a || (bb << h) != b) push(aa); } else { if ((aa << h) != a) push(aa); if ((bb << h) != b) push(bb); } } for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) { if (aa & 1) (ts[aa++].*f)(args...); if (bb & 1) (ts[--bb].*f)(args...); } for (int h = 1; h <= logN; ++h) { const int aa = a >> h, bb = b >> h; if (aa == bb) { if ((aa << h) != a || (bb << h) != b) pull(aa); } else { if ((aa << h) != a) pull(aa); if ((bb << h) != b) pull(bb); } } } // Calculates the product for [a, b). T get(int a, int b) { assert(0 <= a); assert(a <= b); assert(b <= n); if (a == b) return T(); a += n; b += n; for (int h = logN; h; --h) { const int aa = a >> h, bb = b >> h; if (aa == bb) { if ((aa << h) != a || (bb << h) != b) push(aa); } else { if ((aa << h) != a) push(aa); if ((bb << h) != b) push(bb); } } T prodL, prodR, t; for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) { if (aa & 1) { t.pull(prodL, ts[aa++]); prodL = t; } if (bb & 1) { t.pull(ts[--bb], prodR); prodR = t; } } t.pull(prodL, prodR); return t; } // Calculates T::f(args...) of a monoid type for [a, b). // op(-, -) should calculate the product. // e() should return the identity. template #if __cplusplus >= 201402L auto #else decltype((std::declval().*F())()) #endif get(int a, int b, Op op, E e, F f, Args &&... args) { assert(0 <= a); assert(a <= b); assert(b <= n); if (a == b) return e(); a += n; b += n; for (int h = logN; h; --h) { const int aa = a >> h, bb = b >> h; if (aa == bb) { if ((aa << h) != a || (bb << h) != b) push(aa); } else { if ((aa << h) != a) push(aa); if ((bb << h) != b) push(bb); } } auto prodL = e(), prodR = e(); for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) { if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...)); if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR); } return op(prodL, prodR); } // Find min b s.t. T::f(args...) returns true, // when called for the partition of [a, b) from left to right. // Returns n + 1 if there is no such b. template int findRight(int a, F f, Args &&... args) { assert(0 <= a); assert(a <= n); if ((T().*f)(args...)) return a; if (a == n) return n + 1; a += n; for (int h = logN; h; --h) push(a >> h); for (; ; a >>= 1) if (a & 1) { if ((ts[a].*f)(args...)) { for (; a < n; ) { push(a); if (!(ts[a <<= 1].*f)(args...)) ++a; } return a - n + 1; } ++a; if (!(a & (a - 1))) return n + 1; } } // Find max a s.t. T::f(args...) returns true, // when called for the partition of [a, b) from right to left. // Returns -1 if there is no such a. template int findLeft(int b, F f, Args &&... args) { assert(0 <= b); assert(b <= n); if ((T().*f)(args...)) return b; if (b == 0) return -1; b += n; for (int h = logN; h; --h) push((b - 1) >> h); for (; ; b >>= 1) if ((b & 1) || b == 2) { if ((ts[b - 1].*f)(args...)) { for (; b <= n; ) { push(b - 1); if (!(ts[(b <<= 1) - 1].*f)(args...)) --b; } return b - n - 1; } --b; if (!(b & (b - 1))) return -1; } } }; //////////////////////////////////////////////////////////////////////////////// struct Node { Mint sum, lz; Node() : sum(0), lz(1) {} Node(Mint val) : sum(val), lz(1) {} void push(Node &l, Node &r) { if (lz != 1) { l.mul(lz); r.mul(lz); lz = 1; } } void pull(const Node &l, const Node &r) { sum = l.sum + r.sum; } void mul(Mint val) { sum *= val; lz *= val; } // leaf void add(Mint val) { sum += val; } }; int N, MO; vector P[3], invP[3]; Mint slow() { vector early(N, 0); for (int u = 0; u < N; ++u) { for (int p = 0; p < N; ++p) { bool ok = true; for (int h = 0; h < 3; ++h) ok = ok && (invP[h][p] < invP[h][u]); if (ok) ++early[u]; } } cerr<<"[slow] early = "< vis(N, 0); vis[r] = 1; for (int h = 0; h < 3; ++h) { vector us; for (int i = 0; i < invP[h][r]; ++i) { const int u = P[h][i]; bool ok = true; for (int hh = 0; hh < 3; ++hh) if (h != hh) ok = ok && (invP[hh][r] < invP[hh][u]); if (ok) { assert(!vis[u]); vis[u] = 1; us.push_back(u); } } const int usLen = us.size(); us.push_back(r); vector dp(usLen + 1, 0); dp[0] += 1; for (int i = 0; i < usLen; ++i) for (int j = i + 1; j <= usLen; ++j) { bool ok = true; for (int hh = 0; hh < 3; ++hh) if (h != hh) ok = ok && (invP[hh][us[j]] < invP[hh][us[i]]); if (ok) { Mint coef = 1; for (int k = i + 1; k < j; ++k) coef *= early[us[k]]; dp[j] += dp[i] * coef; } } way *= dp[usLen]; } for (int u = 0; u < N; ++u) if (!vis[u]) way *= early[u]; if(way)cerr<<"[slow] r = "<= 0; ) { scanf("%d", &P[h][i]); --P[h][i]; } } for (int h = 0; h < 3; ++h) { invP[h].assign(N, -1); for (int i = 0; i < N; ++i) invP[h][P[h][i]] = i; } for (int h = 0; h < 3; ++h) { for (int i = 0; i < N; ++i) P[h][i] = invP[0][P[h][i]]; } for (int h = 0; h < 3; ++h) { invP[h].assign(N, -1); for (int i = 0; i < N; ++i) invP[h][P[h][i]] = i; // cerr<<"P["<