#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
const long long mod = 998244353;
long long c[maxn];
long long inv[maxn];
long long pow3[maxn];
long long pow_mod(long long x, long long k) {
  long long res = 1, cur = x;
  while (k) {
    if (k & 1) {
      res = res * cur % mod;
    }
    cur = cur * cur % mod;
    k >>= 1;
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  inv[1] = 1;
  for (int i = 2; i <= n; ++i) {
    inv[i] = (mod - mod / i) * inv[mod % i] % mod;
  }
  c[0] = pow3[0] = 1;
  for (int i = 1; i <= n; ++i) {
    c[i] = c[i - 1] * (n - i + 1) % mod * inv[i] % mod;
    pow3[i] = pow3[i - 1] * 3 % mod;
  }
  long long ans1 = 0;
  for (int i = 1; i <= n; ++i) {
    long long sig = i & 1 ? 1 : -1;
    ans1 = (ans1 + c[i] * sig * pow3[i] % mod * pow_mod(pow3[n], n - i)) % mod;
  }
  ans1 = ((ans1 + mod) << 1) % mod;
  long long ans2 = 0;
  for (int i = 0; i < n; ++i) {
    long long sig = i & 1 ? 1 : -1;
    long long tmp = mod - pow3[i];
    ans2 = (ans2 + c[i] * sig * (pow_mod(1 + tmp, n) - pow_mod(tmp, n))) % mod;
  }
  ans2 = (ans2 + mod) * 3 % mod;
  printf("%lld\n", (ans1 + ans2) % mod);
  return 0;
}
