#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long a = 0, b = 1;
  int c = getchar();
  while (!isdigit(c)) {
    if (c == '-') b = -b;
    c = getchar();
  }
  while (isdigit(c)) {
    a = a * 10 + c - '0';
    c = getchar();
  }
  return a * b;
}
const long long md = 998244353;
const long long N = 1e6 + 3;
long long ksm(long long x, long long n) {
  if (n == 0) return 1;
  x %= md;
  long long ans = ksm(x, n / 2);
  ans = ans * ans % md;
  if (n & 1) ans = ans * x % md;
  return ans;
}
long long n;
long long fac[N], finv[N];
long long ans;
int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % md;
  }
  finv[n] = ksm(fac[n], md - 2);
  for (int i = n; i >= 1; i--) {
    finv[i - 1] = finv[i] * i % md;
  }
  ans = (ksm(3, n * n) - ksm(ksm(3, n) - 3, n)) % md;
  for (int i = 1, p = 1; i <= n; i++, p = -p) {
    long long tot = 3 * ksm(ksm(3, n - i) - 1, n) % md;
    (tot += (ksm(3, i) - 3) * ksm(3, n * (n - i))) %= md;
    tot = tot * fac[n] % md * finv[i] % md * finv[n - i] % md;
    (ans += p * tot) %= md;
  }
  if (ans < 0) ans += md;
  printf("%lld\n", ans);
  ;
  ;
  return 0;
}
