#include <bits/stdc++.h>
const int M = 1000005;
const int MOD = 998244353;
long long read() {
  long long x = 0, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') f = -1;
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
long long n, m, k, ans, fac[M], inv[M];
void init(long long n) {
  inv[0] = inv[1] = fac[0] = 1;
  for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % MOD;
  for (long long i = 2; i <= n; i++)
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
  for (long long i = 2; i <= n; i++) inv[i] = inv[i - 1] * inv[i] % MOD;
}
long long C(long long n, long long m) {
  if (m < 0 || n < m) return 0;
  return fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
long long qkpow(long long a, long long b) {
  long long r = 1;
  while (b > 0) {
    if (b & 1) r = r * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return r;
}
signed main() {
  init(1000000);
  n = read();
  long long i3 = qkpow(3, MOD - 2);
  for (long long i = 1; i <= n; i++) {
    long long f = ((n + i + 1) % 2 ? -1 : 1) * qkpow(i3, n * i) * C(n, i) % MOD;
    ans += f * (qkpow(qkpow(i3, n - i) - 1, n) + qkpow(-1, n + 1));
    ans = (ans % MOD + MOD) % MOD;
  }
  ans = ans * qkpow(3, n * n + 1) % MOD;
  for (long long i = 1; i <= n; i++) {
    if (i & 1)
      ans += 2 * qkpow(3, (n - i) * n + i) * C(n, i);
    else
      ans -= 2 * qkpow(3, (n - i) * n + i) * C(n, i);
    ans = (ans % MOD + MOD) % MOD;
  }
  printf("%lld\n", ans);
}
