#include <bits/stdc++.h>
using namespace std;
const int p = 998244353;
int n, f[1000102], finv[1000102], inv[1000102];
inline int ksm(int d, int k) {
  int ret = 1;
  while (k) {
    if (k & 1) ret = 1ll * ret * d % p;
    d = 1ll * d * d % p;
    k >>= 1;
  }
  return ret;
}
inline int C(int nn, int mm) {
  if (nn < mm) return 0;
  return 1ll * f[nn] * finv[mm] % p * finv[nn - mm] % p;
}
int main() {
  f[0] = finv[0] = f[1] = finv[1] = inv[1] = 1;
  for (int i = 2; i <= 1000000; i++) {
    inv[i] = 1ll * (p - p / i) * inv[p % i] % p;
    f[i] = 1ll * f[i - 1] * i % p;
    finv[i] = 1ll * finv[i - 1] * inv[i] % p;
  }
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int te =
        1ll * ksm(3, i) * ksm(3, 1ll * n * (n - i) % (p - 1)) % p * C(n, i) % p;
    if (i & 1)
      ans = (ans + te) % p;
    else
      ans = (ans - te + p) % p;
  }
  ans = 2ll * ans % p;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int tsu = 0;
    int i3 = ksm(3, i);
    i3 = (p - i3) % p;
    tsu = (ksm((i3 + 1) % p, n) - ksm(i3, n) + p) % p;
    tsu = 1ll * tsu * C(n, i) % p;
    if (i & 1)
      sum = (sum - tsu + p) % p;
    else
      sum = (sum + tsu) % p;
  }
  sum = 1ll * sum * (p - 3) % p;
  ans = (ans + sum) % p;
  printf("%d\n", ans);
}
