#include <bits/stdc++.h>
const int MOD = 998244353;
int n;
long long pows[1000005];
long long powmod(long long b, int e) {
  if (e == 1) return b;
  if (e & 1) return (powmod((b * b) % MOD, e / 2) * b) % MOD;
  return powmod((b * b) % MOD, e / 2);
}
long long fac[1000005];
long long invfac[1000005];
long long choose(int a, int b) {
  if (b < 0 || b > a) return 0;
  return (((fac[a] * invfac[b]) % MOD) * invfac[a - b]) % MOD;
}
long long h0, h1, h2;
long long strip;
int main() {
  scanf("%d", &n);
  pows[0] = 1;
  fac[0] = 1;
  invfac[0] = 1;
  for (int i = 1; i <= n; i++) {
    pows[i] = (pows[i - 1] * 3) % MOD;
    fac[i] = (fac[i - 1] * i) % MOD;
    invfac[i] = (invfac[i - 1] * powmod(i, MOD - 2)) % MOD;
  }
  h0 = (powmod(pows[n], n) + MOD - powmod((pows[n] + MOD - 3) % MOD, n)) % MOD;
  h1 = 0;
  for (int k = 1; k <= n; k++) {
    strip = (powmod(pows[n - k], n) + MOD - powmod(pows[n - k] - 1, n)) % MOD;
    h2 = (choose(n, k) * strip) % MOD;
    if (k & 1)
      h1 += h2;
    else
      h1 += MOD - h2;
  }
  h1 %= MOD;
  printf("%I64d\n", (h0 + h0 + (MOD - h1) * 3) % MOD);
}
