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