#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MOD = 998244353;
inline void ADD(int& x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
}
inline void DEC(int& x, int y) {
  x -= y;
  if (x < 0) x += MOD;
}
inline int add(int x, int y) { return x + y < MOD ? x + y : x + y - MOD; }
inline int dec(int x, int y) { return x < y ? x - y + MOD : x - y; }
long long q_pow(long long a, long long b, long long p = MOD) {
  long long ret = 1;
  for (; b; a = a * a % p, b >>= 1)
    if (b & 1) ret = ret * a % p;
  return ret;
}
long long q_inv(long long x, long long p = MOD) { return q_pow(x, p - 2, p); }
int fac[MAXN], ifac[MAXN];
void build(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD;
  ifac[n] = q_inv(fac[n]);
  for (int i = n - 1; i >= 0; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
}
int binom(int n, int m) {
  return 1ll * fac[n] * ifac[m] % MOD * ifac[n - m] % MOD;
}
int N;
int main() {
  scanf("%d", &N);
  int ans = 0;
  build(N);
  int tmp = 0;
  for (int i = 1; i <= N; ++i) {
    int cur = 1ll * binom(N, i) * q_pow(3, 1ll * N * (N - i) + i) % MOD;
    if ((i - 1) & 1)
      DEC(tmp, cur);
    else
      ADD(tmp, cur);
  }
  ADD(ans, 2 * tmp % MOD);
  tmp = 0;
  for (int i = 1; i <= N; ++i) {
    int cur =
        1ll * binom(N, i) *
        dec(q_pow(dec(q_pow(3, N - i), 1), N), q_pow(3, 1ll * N * (N - i))) %
        MOD;
    if (i & 1)
      DEC(tmp, cur);
    else
      ADD(tmp, cur);
  }
  tmp = 3ll * tmp % MOD;
  DEC(ans, tmp);
  printf("%d", ans);
  return 0;
}
