#include <bits/stdc++.h>
const int maxn = 1e6 + 5;
const int mod = 998244353;
long long n, c[maxn], ans;
long long fastPow(long long a, long long x) {
  long long ret = 1, base = a;
  while (x) {
    if (x & 1) ret = ret * base % mod;
    x >>= 1;
    base = (base * base) % mod;
  }
  return (ret % mod + mod) % mod;
}
int main() {
  scanf("%lld", &n);
  c[0] = 1;
  for (int i = 1; i <= n; i++) {
    c[i] = c[i - 1] * (n - i + 1) % mod * fastPow(i, mod - 2) % mod;
  }
  for (int i = 0; i < n; i++) {
    long long s = fastPow(3, i);
    long long t1 = fastPow(1 - s, n), t2 = fastPow(-s, n);
    long long d = (c[i] * (t1 - t2) % mod + mod) % mod;
    if (i & 1)
      ans += d;
    else
      ans -= d;
    ans = (ans % mod + mod) % mod;
  }
  ans = (ans * 3) % mod;
  for (int i = 1; i <= n; i++) {
    long long d = fastPow(3, i) * fastPow(3, n * (n - i)) % mod;
    d = c[i] * d % mod;
    if (i & 1)
      ans += 2 * d;
    else
      ans -= 2 * d;
    ans = (ans % mod + mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
