#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
const long long mod = 998244353;
long long C[MAXN], inv[MAXN];
inline long long fpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = a * res % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  long long N, ans0 = 0, ans1 = 0;
  scanf("%I64d", &N);
  inv[1] = 1;
  for (int i = 2; i <= N; ++i) inv[i] = mod - mod / i * inv[mod % i] % mod;
  C[0] = 1;
  for (int i = 0; i < N; ++i)
    C[i + 1] = C[i] * (N - i) % mod * inv[i + 1] % mod;
  for (int i = 1; i <= N; ++i) {
    if (i & 1) {
      ans0 = (ans0 + C[i] * fpow(3, (i + N * (N - i)) % (mod - 1)) % mod) % mod;
    } else {
      ans0 =
          (mod + ans0 - C[i] * fpow(3, (i + N * (N - i)) % (mod - 1)) % mod) %
          mod;
    }
  }
  ans0 <<= 1;
  ans1 = 0;
  for (int i = 1; i <= N; ++i) {
    if (i & 1) {
      ans1 += C[i] *
              (mod + fpow(fpow(3, N - i) - 1, N) - fpow(fpow(3, N - i), N)) %
              mod;
      ans1 %= mod;
    } else {
      ans1 -= C[i] *
              (mod + fpow(fpow(3, N - i) - 1, N) - fpow(fpow(3, N - i), N)) %
              mod;
      ans1 = (mod + ans1) % mod;
    }
  }
  printf("%I64d", (ans0 + ans1 * 3) % mod);
  return 0;
}
