#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int n;
long long op[2] = {1, mod - 1};
long long ans, fac[1001000], iv[1001000], pw[1001000], res;
long long power(long long x, long long y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) ret = ret * x % mod;
  return ret;
}
long long C(int x, int y) { return fac[x] * iv[y] % mod * iv[x - y] % mod; }
int main() {
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;
  iv[n] = power(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) iv[i] = iv[i + 1] * (i + 1) % mod;
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 3 % mod;
  for (int i = 1; i <= n; i++)
    (ans += op[(i + 1) & 1] * C(n, i) % mod *
            power(3, i + 1ll * n * (n - i) % (mod - 1)) % mod * 2) %= mod;
  for (int i = 0; i < n; i++)
    ans += op[(i + 1) & 1] * C(n, i) % mod * 3 % mod *
           (power(1 - pw[i] + mod, n) - power(mod - pw[i], n) + mod) % mod;
  printf("%lld\n", ans % mod);
  return 0;
}
