#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
template <typename T>
void update(T &x, T y, T mod) {
  x = x + y > mod ? x + y - mod : x + y;
}
template <typename T>
void multi(T &x, T y, T mod) {
  x = 1ll * x * y % mod;
}
const int INF = (1ll << 30);
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = 0;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  x = f ? x : -x;
}
template <typename T>
void write(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 1e6 + 7;
const int mod = 998244353;
long long n, ans, ret, fac[N], invfac[N], pw[N];
inline long long qpow(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ret;
}
inline void init(long long NN) {
  fac[0] = 1;
  for (long long i = 1; i <= NN; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
  invfac[NN] = qpow(fac[NN], mod - 2);
  for (long long i = NN - 1; i >= 0; i--)
    invfac[i] = 1ll * invfac[i + 1] * (i + 1) % mod;
}
inline long long C(long long n, long long m) {
  return 1ll * fac[n] * invfac[n - m] % mod * invfac[m] % mod;
}
signed main() {
  read(n);
  init(1e6);
  for (long long i = 1; i <= n; i++)
    ans = (ans + (1ll * (qpow(-1, i + 1) + mod) % mod * (C(n, i) % mod) + mod) %
                     mod * qpow(3, i % (mod - 1)) % mod *
                     qpow(3, 1ll * n * (n - i) % (mod - 1) % mod) % mod) %
          mod;
  ans = 2ll * ans % mod;
  ret = 0;
  for (long long i = 1; i <= n; i++) {
    long long res = (long long)C(n, i) *
                    qpow(3, (mod - 1 - (long long)i * n % (mod - 1))) % mod;
    res = (long long)res * (qpow(1 - qpow(3, i - n + mod - 1) + mod, n) - 1) %
          mod;
    if (i & 1)
      ret = (ret - res % mod + mod) % mod;
    else
      ret = (ret + res % mod + mod) % mod;
  }
  ret = 1ll * ret * qpow(3, ((long long)n * n + 1) % (mod - 1)) % mod;
  printf("%lld\n", 1ll * (ans - ret + mod) % mod);
  return 0;
}
