#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353, N = 1e6 + 10;
long long ans = 0, n;
long long fac[N], inv[N];
inline long long exp(long long a, long long b) {
  long long res = (a != 0);
  while (b) {
    if (b & 1) {
      res = res * a % Mod;
    }
    a = a * a % Mod;
    b >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % Mod;
  }
  inv[n] = exp(fac[n], Mod - 2);
  for (int i = n - 1; i >= 1; i--) {
    inv[i] = inv[i + 1] * (i + 1) % Mod;
  }
  ans = exp(3, n * n + 1);
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    res = res + (i % 2 == 0 ? 1ll : -1ll) * fac[n] * inv[i] % Mod * inv[n - i] %
                    Mod * exp(exp(3, n * i), Mod - 2) % Mod *
                    (exp(Mod + 1 - exp(exp(3, n - i), Mod - 2), n) - 1) % Mod;
    if (res > Mod) res -= Mod;
    if (res < 0) res += Mod;
  }
  ans = ans * res % Mod;
  for (int i = 1; i <= n; i++) {
    ans = ans + (i % 2 == 0 ? 2ll : -2ll) * fac[n] * inv[i] % Mod * inv[n - i] %
                    Mod * exp(3, n * n - n * i + i) % Mod;
    if (ans > Mod) ans -= Mod;
    if (ans < 0) ans += Mod;
  }
  cout << Mod - ans << '\n';
  return 0;
}
