#include <bits/stdc++.h>
const int N = 1000005;
const long long mod = 998244353;
long long fac[N], _3[N], _3N[N], inv[N];
long long qp(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
inline long long C(int n, int m) {
  return (fac[n] * inv[n - m] % mod) * inv[m] % mod;
}
int main() {
  int n;
  scanf("%d", &n);
  fac[0] = _3[0] = _3N[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    _3[i] = _3[i - 1] * 3 % mod;
  }
  inv[n] = qp(fac[n], mod - 2);
  long long _3n = _3[n];
  for (int i = n - 1; i >= 0; i--) {
    inv[i] = inv[i + 1] * (i + 1) % mod;
    _3N[n - i] = _3N[n - i - 1] * _3n % mod;
  }
  long long res = (_3N[n] - qp(_3n - 3, n) + mod) % mod;
  for (int i = 1, f = 1; i <= n; i++, f = -f) {
    res = (res +
           f *
               (C(n, i) *
                ((3 * qp(_3[n - i] - 1, n) + (_3[i] - 3) * _3N[n - i] % mod +
                  mod) %
                 mod) %
                mod) %
               mod +
           mod) %
          mod;
  }
  printf("%I64d\n", res);
}
