#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #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 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") #ifndef LIBRA_OTHER_INT128_H_ #define LIBRA_OTHER_INT128_H_ #include #include constexpr unsigned __int128 toUInt128(const char *s) { unsigned __int128 x = 0; for (; *s; ++s) x = x * 10 + (*s - '0'); return x; } constexpr __int128 toInt128(const char *s) { if (*s == '-') return -toInt128(s + 1); __int128 x = 0; for (; *s; ++s) x = x * 10 + (*s - '0'); return x; } unsigned __int128 inUInt128() { static char buf[41]; scanf("%s", buf); return toUInt128(buf); } __int128 inInt128() { static char buf[41]; scanf("%s", buf); return toInt128(buf); } void out(unsigned __int128 x) { static char buf[41]; int len = 0; do { buf[len++] = '0' + static_cast(x % 10); } while (x /= 10); for (int i = len; --i >= 0; ) putchar(buf[i]); } void out(__int128 x) { if (x < 0) { putchar('-'); out(-static_cast(x)); } else { out(static_cast(x)); } } std::ostream &operator<<(std::ostream &os, unsigned __int128 x) { static char buf[41]; int len = 0; do { buf[len++] = '0' + static_cast(x % 10); } while (x /= 10); for (int i = len; --i >= 0; ) os << buf[i]; return os; } std::ostream &operator<<(std::ostream &os, __int128 x) { if (x < 0) { os << '-' << -static_cast(x); } else { os << static_cast(x); } return os; } #endif // LIBRA_OTHER_INT128_H_ #include #ifdef LOCAL mt19937_64 rng(58); #else mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #endif using U = unsigned __int128; constexpr U M = (((U)1) << 107) - 1; U add(U a, U b) { return ((a += b) >= M) ? (a - M) : a; } U mul(U a, U b) { U c = 0; for (int i = 0; i < 6; ++i) { c += a * (b & ((1<<18)-1)); (a <<= 18) %= M; b >>= 18; } return c % M; } using Pair = pair; Pair mul(Pair f, Pair g) { return Pair(mul(f.first, g.first), add(f.second, mul(f.first, g.second))); } Pair power(Pair f, Int e) { Pair g(1, 0); for (; ; f = mul(f, f)) { if (e & 1) g = mul(g, f); if (!(e >>= 1)) return g; } } char buf[200'010]; int K; vector S; vector T; U BASE; Pair solve() { Pair ret(1, 0); for (int k = 0; k < K; ++k) { Pair p(1, 0); for (const char s : S[k]) { p = mul(p, Pair(BASE, s)); } p = power(p, T[k]); ret = mul(ret, p); } //cerr<