#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1000100;
long long f[N], fi[N];
long long pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (long long)ans * a % mod;
    a = (long long)a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long comb(int n, int k) {
  return (long long)f[n] * fi[n - k] % mod * fi[k] % mod;
}
int main() {
  f[0] = 1;
  for (int i = 1; i < N; i++) f[i] = (long long)f[i - 1] * i % mod;
  fi[N - 1] = pw(f[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--) fi[i] = (long long)fi[i + 1] * (i + 1) % mod;
  long long n;
  scanf("%lld", &n);
  long long oneC = 0;
  for (int i = 0; i <= n; i++) {
    long long sum = (long long)comb(n, i) * pw(pw(3, n - i) + mod - 1, n) % mod;
    if (i & 1)
      oneC += sum;
    else
      oneC -= sum;
    oneC = (oneC + mod) % mod;
  }
  oneC = (oneC + pw(pw(3, n), n)) % mod;
  long long twoC = 0;
  for (int i = 2; i <= n; i++) {
    long long sum = (long long)(pw(2, i) + mod - 2) * comb(n, i) % mod;
    sum = (long long)sum * pw(pw(3, n - i), n) % mod;
    if (i & 1)
      twoC -= sum;
    else
      twoC += sum;
  }
  twoC = twoC * 2 % mod;
  long long threeC = 0;
  for (int i = 3; i <= n; i++) {
    long long aux = (pw(3, i) - 3LL * pw(2, i) + 3 + 3LL * mod) % mod;
    long long sum =
        (long long)aux * comb(n, i) % mod * pw(pw(3, n - i), n) % mod;
    if (i & 1)
      threeC += sum;
    else
      threeC -= sum;
  }
  threeC = threeC * 2 % mod;
  long long ans = 3LL * oneC - 3LL * twoC + threeC + 3LL * mod;
  printf("%lld\n", ans % mod);
  return 0;
}
