#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n, ans, tmp;
long long C[1000005];
long long ksm(long long aa, long long bb) {
  long long ans = 1;
  while (bb) {
    if (bb & 1) ans = (ans * aa) % mod;
    bb >>= 1;
    aa = (aa * aa) % mod;
  }
  return ans;
}
int main() {
  scanf("%lld", &n);
  C[0] = 1;
  long long ff = 1;
  for (long long i = 1; i <= n; ++i) {
    C[i] = ((C[i - 1] * (n - i + 1)) % mod * ksm(i, mod - 2) % mod) % mod;
    ans = (ans + C[i] * 2 % mod * ksm(3, (n - i) * n + i) % mod * ff % mod +
           mod) %
          mod;
    ff = -ff;
  }
  ff = -1;
  for (int i = 0; i < n; ++i) {
    long long tmp =
        (ksm((ksm(3, i) - 1 + mod) % mod, n) % mod - ksm(ksm(3, i), n) + mod) %
        mod;
    if (n % 2 == 1) tmp = mod - tmp;
    ans = (ans + 3 * C[i] % mod * ff % mod * tmp % mod + mod) % mod;
    ff = -ff;
  }
  printf("%lld", ans);
  return 0;
}
