#include <bits/stdc++.h>
using namespace std;
const int N = 1000005, mo = 998244353;
int n, ans, Fac[N], Inv[N], FInv[N];
int C(int n, int m) { return 1ll * Fac[n] * FInv[m] % mo * FInv[n - m] % mo; }
int quick(int x, long long y) {
  int s = 1;
  for (long long i = 1; i <= y; i <<= 1, x = (long long)x * x % mo)
    if (i & y) s = (long long)s * x % mo;
  return s;
}
int main() {
  scanf("%d", &n);
  Inv[1] = 1;
  for (int i = 2; i <= n; i++)
    Inv[i] = (long long)Inv[mo % i] * (mo - mo / i) % mo;
  Fac[0] = FInv[0] = 1;
  for (int i = 1; i <= n; i++)
    Fac[i] = (long long)Fac[i - 1] * i % mo,
    FInv[i] = (long long)FInv[i - 1] * Inv[i] % mo;
  for (int i = 1, sig = 1; i <= n; i++, sig = -sig)
    ans = (ans + 2ll * sig * C(n, i) * quick(3, 1ll * n * (n - i) + i)) % mo;
  for (int i = 0, sig = -1; i < n; i++, sig = -sig) {
    ans = (ans + 3ll * sig * C(n, i) % mo *
                     (quick(mo + 1 - quick(3, i), n) -
                      (n & 1 ? -1 : 1) * quick(3, 1ll * i * n))) %
          mo;
  }
  if (ans < 0) ans += mo;
  printf("%d\n", ans);
  return 0;
}
