#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1000005;
long long p[N];
long long qpow(long long a, long long b) {
  if (b == 0) return 1;
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long inv[N], fac[N], fnv[N], n;
void init() {
  p[0] = 1;
  p[1] = 3;
  for (int i = 2; i <= n; i++) p[i] = p[i - 1] * 3 % mod;
  inv[1] = 1;
  fac[0] = fnv[0] = 1;
  for (int i = 2; i <= n; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    fnv[i] = fnv[i - 1] * inv[i] % mod;
  }
}
long long c(long long a, long long b) {
  if (b > a) return 0;
  return fac[a] * fnv[b] % mod * fnv[a - b] % mod;
}
int main() {
  scanf("%lld", &n);
  init();
  long long ans = (qpow(3, n * n) - qpow(p[n] - 3, n)) % mod;
  for (int i = 1; i <= n; i++) {
    long long ret = c(n, i) * (p[i] - 3) % mod * qpow(3, n * (n - i)) % mod;
    ret = (ret + 3 * c(n, i) % mod * qpow(p[n - i] - 1, n) % mod) % mod;
    if (i & 1)
      ans = (ans + ret) % mod;
    else
      ans = (ans - ret + mod) % mod;
  }
  printf("%lld\n", (ans + mod) % mod);
}
