#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long MAX = 1e6 + 7;
long long quick_pow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1LL) ret = ret * x % MOD;
    y >>= 1LL;
    x = x * x % MOD;
  }
  return ret;
}
long long fac[MAX];
long long inv[MAX];
long long pow3[MAX];
void pre() {
  fac[0] = 1;
  for (register long long i = 1; i <= 1000000; i++) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  inv[1000000] = quick_pow(fac[1000000], MOD - 2);
  for (long long i = 999999; i >= 0; i--) {
    inv[i] = inv[i + 1] * (i + 1) % MOD;
  }
  pow3[0] = 1;
  for (long long i = 1; i <= 1000000; i++) {
    pow3[i] = pow3[i - 1] * 3 % MOD;
  }
}
long long getC(long long x, long long y) {
  if (x < y) {
    return 0;
  }
  return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
signed main() {
  long long N;
  cin >> N;
  pre();
  long long ans = 0;
  long long mode = 1;
  for (long long i = 1; i <= N; i++) {
    ans = (ans + 2 * getC(N, i) * mode % MOD * pow3[i] % MOD *
                     quick_pow(3, N * (N - i)) % MOD) %
          MOD;
    mode = (mode * (-1) + MOD) % MOD;
  }
  long long fuck = quick_pow(3, N * N + 1);
  long long INV = quick_pow(3, MOD - 2);
  mode = MOD - 1;
  for (long long i = 1; i <= N; i++) {
    long long tmp =
        getC(N, i) * quick_pow(INV, i * N) % MOD * mode % MOD *
        ((quick_pow(1 - quick_pow(INV, N - i), N) - 1 + MOD) % MOD) % MOD;
    ans = (ans - fuck * tmp % MOD + MOD) % MOD;
    mode = (mode * (-1) + MOD) % MOD;
  }
  cout << ans << endl;
}
