#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, zqm = 998244353;
int fac[N], finv[N], n;
int qp(int base, int exp) {
  base %= zqm, exp %= (zqm - 1);
  int ret = 1;
  for (; exp; exp /= 2, base = (base * 1ll * base) % zqm)
    if (exp & 1) ret = (ret * 1ll * base) % zqm;
  return ret;
}
int c(int n, int m) {
  return n < m ? 0 : fac[n] * 1ll * finv[m] % zqm * 1ll * finv[n - m] % zqm;
}
int main() {
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = (fac[i - 1] * 1ll * i) % zqm;
  finv[n] = qp(fac[n], zqm - 2);
  for (int i = n - 1; i >= 0; --i)
    finv[i] = (finv[i + 1] * 1ll * (i + 1)) % zqm;
  int ans = 0;
  for (int i = 1, p3 = qp(3, n - 1); i <= n;
       ++i, p3 = (p3 * 332748118ll) % zqm) {
    if (i & 1)
      ans = (ans + c(n, i) * 1ll * (qp(p3 - 1, n) - qp(p3, n)) % zqm) % zqm;
    else
      ans = (ans - c(n, i) * 1ll * (qp(p3 - 1, n) - qp(p3, n)) % zqm) % zqm;
  }
  ans = (ans * 3ll) % zqm;
  for (int i = 1, p3 = 3; i <= n; ++i, p3 = (p3 * 3ll) % zqm) {
    if (i & 1)
      ans = (ans + 2ll * c(n, i) * p3 % zqm *
                       qp(3, (n * 1ll * (n - i)) % (zqm - 1)) % zqm) %
            zqm;
    else
      ans = (ans - 2ll * c(n, i) * p3 % zqm *
                       qp(3, (n * 1ll * (n - i)) % (zqm - 1)) % zqm) %
            zqm;
  }
  printf("%d\n", (ans + zqm) % zqm);
  return 0;
}
