#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 1e2, mod = 998244353;
int fac[maxn], inv[maxn], ivf[maxn];
inline void init() {
  fac[0] = fac[1] = 1;
  for (int i = 2; i < maxn; i++) fac[i] = fac[i - 1] * 1ll * i % mod;
  inv[0] = inv[1] = 1;
  for (int i = 2; i < maxn; i++)
    inv[i] = inv[mod % i] * 1ll * (mod - mod / i) % mod;
  ivf[0] = ivf[1] = 1;
  for (int i = 2; i < maxn; i++) ivf[i] = ivf[i - 1] * 1ll * inv[i] % mod;
}
inline int C(int n, int m) {
  return m > n ? 0 : fac[n] * 1ll * ivf[m] % mod * ivf[n - m] % mod;
}
inline int qpow(int x, long long k) {
  int res = 1;
  while (k) {
    if (k & 1) res = res * 1ll * x % mod;
    x = x * 1ll * x % mod, k >>= 1;
  }
  return res;
}
inline int h(int x, int y, int n) {
  return qpow(3, n * 1ll * n - (x + y) * 1ll * n + x + y) * 1ll * C(n, x + y) %
         mod;
}
inline int solve(int n) {
  int ans = 0;
  for (int x = 1; x <= n; x++) {
    (ans += ((x & 1) ? -1 : 1) * C(n, x) * 1ll * qpow(inv[3], x * 1ll * n) %
            mod * (qpow(1 - qpow(inv[3], n - x), n) - 1) % mod) %= mod;
  }
  ans = ans * 1ll * qpow(3, n * 1ll * n + 1) % mod;
  for (int i = 0; i <= n; i++) (ans += ((i & 1) ? -1 : 1) * h(i, 0, n)) %= mod;
  for (int i = 1; i <= n; i++) (ans += ((i & 1) ? -1 : 1) * h(0, i, n)) %= mod;
  return ans;
}
int main() {
  init();
  int n;
  cin >> n;
  cout << ((qpow(3, n * 1ll * n) - solve(n)) % mod + mod) % mod << endl;
}
