#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int mo = 998244353;
long long n, C[N], ans;
long long ksm(long long n, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * n % mo;
    n = n * n % mo;
    k >>= 1;
  }
  return res;
}
signed main() {
  scanf("%lld", &n);
  C[0] = 1;
  for (register int i = 1; i <= n; ++i)
    C[i] = C[i - 1] * (n - i + 1) % mo * ksm(i, mo - 2) % mo;
  ans = 0;
  for (register int i = 0; i <= n - 1; ++i) {
    long long s = ksm(3, i), t1 = ksm(1 - s, n), t2 = ksm(-1 * s, n),
              d = (C[i] * (t1 - t2) % mo + mo) % mo;
    if (i & 1)
      ans += d;
    else
      ans -= d;
    ans = (ans % mo + mo) % mo;
  }
  ans = ans * 3 % mo;
  for (register int i = 1; i <= n; ++i) {
    long long d = ksm(3, i) * ksm(3, (n - i) * n) % mo;
    d = d * C[i] % mo;
    if (i & 1)
      ans += 2 * d;
    else
      ans -= 2 * d;
    ans = (ans % mo + mo) % mo;
  }
  printf("%lld", ans);
  return 0;
}
