#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
inline long long readll() {
  long long f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
template <class T>
inline void chmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void chmin(T &a, T b) {
  if (a > b) a = b;
}
inline void swap(int &a, int &b) {
  int c = a;
  a = b;
  b = c;
}
using namespace std;
const int N = 1e6 + 10;
const int P = 998244353;
inline int qpow(int a, int b) {
  int q = 1;
  while (b) {
    if (b & 1) q = 1ll * q * a % P;
    a = 1ll * a * a % P;
    b >>= 1;
  }
  return q;
}
int n, inv[N], invf[N], fac[N];
inline int C(int n, int m) {
  if (n < m) return 0;
  return 1ll * fac[n] * invf[m] % P * invf[n - m] % P;
}
inline int inc(int a, int b) {
  a += b;
  if (a >= P) a -= P;
  return a;
}
inline int dec(int a, int b) {
  a -= b;
  if (a < 0) a += P;
  return a;
}
int f[N];
int main() {
  n = read();
  inv[1] = 1;
  for (int i = (2); i <= (n); ++i) inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
  *fac = 1;
  for (int i = (1); i <= (n); ++i) fac[i] = 1ll * fac[i - 1] * i % P;
  *invf = 1;
  for (int i = (1); i <= (n); ++i) invf[i] = 1ll * invf[i - 1] * inv[i] % P;
  for (int i = (1); i <= (n); ++i) f[i] = qpow(dec(qpow(3, n), 3), n);
  int all = dec(qpow(3, 1ll * n * n % 998244352), f[n]);
  for (int i = (1); i <= (n); ++i) {
    int ret = 1ll * C(n, i) *
              (1ll * 3 * (qpow(dec(qpow(3, n - i), 1), n)) % P +
               1ll * dec(qpow(3, i), 3) *
                   qpow(3, (1ll * n * (n - i) % 998244352)) % P) %
              P;
    if (i & 1)
      all = inc(all, ret);
    else
      all = dec(all, ret);
  }
  printf("%d\n", all);
  return 0;
}
