#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 100;
const long long mod = 998244353;
long long n, n2, fact[MAXN], invfact[MAXN];
long long qpow(long long a, long long k) {
  long long ans = 1, tmp = a;
  while (k) {
    if (k & 1) ans = ans * tmp % mod;
    tmp = tmp * tmp % mod;
    k >>= 1;
  }
  return ans;
}
int main() {
  scanf("%I64d", &n);
  fact[1] = 1;
  for (int i = 2; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
  invfact[n] = qpow(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1) % mod;
  n2 = n * n;
  long long ans = qpow(3, n2);
  long long sub = qpow(qpow(3, n) - 3, n);
  long long xishu = -1;
  for (int i = 1; i <= n; i++) {
    long long tmp = fact[n] * invfact[i] % mod * invfact[n - i] % mod;
    long long other = (3ll * qpow(qpow(3, n - i) - 1, n) % mod +
                       (qpow(3, i) - 3) * qpow(3, (n - i) * n) % mod) %
                      mod;
    tmp = (tmp * other) % mod;
    sub = ((sub + tmp * xishu) + mod) % mod;
    xishu *= -1;
  }
  ans = ((ans - sub) % mod + mod) % mod;
  printf("%I64d\n", ans);
  return 0;
}
