#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void read(Tp &x) {
  static char c;
  static bool neg;
  x = 0, c = getchar(), neg = false;
  for (; !isdigit(c); c = getchar()) {
    if (c == '-') {
      neg = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (neg) {
    x = -x;
  }
}
const int N = 1e6 + 5;
const int MOD = 998244353;
const int PHI = MOD - 1;
inline int power(int base, int exp) {
  int res = 1;
  while (exp > 0) {
    if (exp & 1) {
      res = (long long)res * base % MOD;
    }
    base = (long long)base * base % MOD;
    exp >>= 1;
  }
  return res;
}
inline int inv(int x) { return power(x, MOD - 2); }
int fac[N], facInv[N], pow3[N];
inline void init() {
  fac[0] = facInv[0] = pow3[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = (long long)fac[i - 1] * i % MOD;
  }
  for (int i = 1; i < N; ++i) {
    pow3[i] = (long long)pow3[i - 1] * 3 % MOD;
  }
  facInv[N - 1] = inv(fac[N - 1]);
  for (int i = N - 2; i >= 1; --i) {
    facInv[i] = (long long)facInv[i + 1] * (i + 1) % MOD;
  }
}
inline int C(int n, int m) {
  if (n < m) {
    return 0;
  } else {
    return (long long)fac[n] * facInv[n - m] % MOD * facInv[m] % MOD;
  }
}
int n;
inline int inc(int &x, const int &y) {
  x += y;
  if (x >= MOD) {
    x -= MOD;
  } else if (x < 0) {
    x += MOD;
  }
  return x;
}
inline int dec(int &x, const int &y) {
  x -= y;
  if (x >= MOD) {
    x -= MOD;
  } else if (x < 0) {
    x += MOD;
  }
  return x;
}
inline int sum(const int &x, const int &y) {
  int res = x + y;
  if (res >= MOD) {
    res -= MOD;
  } else if (res < 0) {
    res += MOD;
  }
  return res;
}
inline int calc(int n) {
  int res = power(3, (long long)n * n % PHI),
      temp = power(sum(power(3, n), -3), n), per;
  dec(res, temp);
  temp = 0;
  for (int i = 1; i <= n; ++i) {
    per = (long long)C(n, i) *
          sum(3LL * power(sum(pow3[n - i], -1), n) % MOD,
              (long long)sum(pow3[i], -3) * power(pow3[n], n - i) % MOD) %
          MOD;
    if (i % 2 == 1) {
      dec(temp, per);
    } else {
      inc(temp, per);
    }
  }
  dec(res, temp);
  return res;
}
int main() {
  init();
  read(n);
  printf("%d\n", calc(n));
}
