1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <bits/stdc++.h> #define LL long long #define PII pair<LL, LL> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while (c < '0' || '9' < c) { if (c == '-') flag = 1; c = getchar(); } while ('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if (flag) res = -res; } template <class T, class... ARC> inline void read(T &res, ARC &...com) { read(res), read(com...); } template <class T> void write(T res) { if (res < 0) putchar('-'), res = -res; if (res > 9) write(res / 10); putchar(res % 10 + '0'); } template <> inline void write(char c) { putchar(c); } template <> inline void write(char *s) { while (*s) putchar(*s++); } template <class T, class... ARC> inline void write(T res, ARC... com) { write(res), write(com...); } const int N = 3e6 + 5, mod = 998244353, inv2 = (mod + 1) >> 1; int n, m; inline int qpow(int x, int k) { int res = 1; while(k) { if(k & 1) res = 1ll * res * x % mod; k >>= 1; x = 1ll * x * x % mod; } return res; } int ans; int fac[N], inv[N]; inline void init() { fac[0] = 1; for(int i = 1;i < N;i ++) fac[i] = 1ll * i * fac[i - 1] % mod; inv[0] = inv[1] = 1; for(int i = 2;i < N;i ++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for(int i = 2;i < N;i ++) inv[i] = 1ll * inv[i] * inv[i - 1] % mod; } inline int C(int x, int y) { if(y < 0 || x < y) return 0; return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; } int main() { init(); read(n); for(int i = 1, res;i <= n;i ++) { res = qpow(qpow(3, n - i) - 1, n) - qpow(3, 1ll * n * (n - i) % (mod - 1)); res = 1ll * res * C(n, i) % mod * 3 % mod; if(!(i & 1)) res = -res; ans = (ans + res) % mod; } for(int i = 1, flag;i <= n;i ++) { flag = (i & 1) ? 1 : -1; ans = (ans + 2ll * flag * C(n, i) % mod * qpow(3, i) % mod * qpow(3, (1ll * n * n - 1ll * n * i) % (mod - 1))) % mod; } ans = (ans + mod) % mod; write(ans); return 0; }
|